andela-amagana
12/3/2017 - 3:33 PM

Active support concern

This snippet shows how to use ActiveSupport::Concern module to expose methods as either instance methods or class methods.

require 'active_support/concern'

module Bar
  extend ActiveSupport::Concern

  included do
    def an_instance_method
      puts 'I am an instance method'
    end
  end

  class_methods do
    def a_class_method
      puts 'I am a class method'
    end
  end
end

class Foo
  include Bar
end

Foo.a_class_method

instance_of_foo = Foo.new
instance_of_foo.an_instance_method