Call a class method with a multi level Module structure in Ruby

ruby, ruby-on-rails

Solution

This is why `ActiveSupport::Concern` was created.

module BasicFeatures
  extend ActiveSupport::Concern

  included do
    before_filter :require_user
  end

  def this_is_an_instance_method
    'foo'
  end

  module ClassMethods
    def this_is_a_class_method
      'bar'
    end
  end
end

class SomeClass
  include BasicFeatures
end

SomeClass.new.this_is_an_instance_method #=> 'foo'

You can also nest them — that is, create concerns that include concerns — and everything will work as expected. And here are the docs.

Problem

I have some modules to be included in my controller classes. These modules define `before_filter`: ``` module BasicFeatures def filter_method ... end def self.included(base) base.before_filter(:filter_method) ... end end module AdvancedFeatures include BasicFeatures ... end ``` And the classes: ``` class BasicController < ApplicationController include BasicFeatures end class AdvancedController < ApplicationController include AdvancedFeatures end ``` - When `BasicFeatures` module is included in `AdvancedFeatures` module, there are no `before_filter` methods in it. - The `AdvancedController` didn't get the `before_filter` call. I need both my controllers to get the `before_filter` without any code duplication. I don't know if I am using the best approach so, I'm open to any suggestion.

Original source