Using the ActiveSupport :: Concern extension

I'm working through CodeSchool RubyBits and I came up with an exercise that I just don't understand: " Make sure the AtariLibrary class only includes the LibraryUtils module and lets ActiveSupport :: Concern take care of loading it. Then refactor the self.included method on LibraryUtils to use the included method .

module LibraryLoader

  extend ActiveSupport::Concern

  module ClassMethods
    def load_game_list
    end
  end
end

module LibraryUtils
  def self.included(base)
    base.load_game_list
  end
end

class AtariLibrary
  include LibraryLoader
  include LibraryUtils
end

      

Based on the solution (see below) it ActiveSupport::Concern

doesn't seem to care about loading dependencies - you need to include the LibraryLoader inside LibraryUtils.

Can you help me understand what is doing ActiveSupport::Concern

and why it needs to be called via extend

in both modules?

module LibraryLoader
  extend ActiveSupport::Concern

  module ClassMethods
    def load_game_list
    end
  end
end

module LibraryUtils
  extend ActiveSupport::Concern
  include LibraryLoader

  #result of refactoring the self.included method
  included do
    load_game_list
  end
end

class AtariLibrary
  include LibraryUtils
end

      

Thank!

+3


source to share


1 answer


When you call the ActiveSupport :: Concern extension, it will look for the internal module ClassMethods and extend your "host" class. It will then provide you with an "included" method that you can pass to the block:

included do some_function end



The included method will run in the context of the included class. If you have a module that requires functionality in another module, ActiveSupport :: Concern will take care of the dependencies for you.

+2


source







All Articles