Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What the hell is ActiveSupport::Concern?

What the hell is ActiveSupport::Concern?

A short talk about ActiveSupport::Concern.

Aaron Patterson

December 04, 2012
Tweet

More Decks by Aaron Patterson

Other Decks in Programming

Transcript

  1. Class Methods module MyEnumerable def my_first end module ClassMethods def

    my_new end end def self.included(klass) klass.extend ClassMethods end end
  2. Normal Include module MyEnumerable def my_first end end class Aaron

    p singleton_methods # => [] include MyEnumerable p singleton_methods # => [] end
  3. Normal Extend module MyEnumerable def my_first end end class Aaron

    p singleton_methods # => [] extend MyEnumerable p singleton_methods # => [:my_first] end
  4. Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def

    self.included klass klass.extend ClassMethods end end
  5. Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def

    self.included klass klass.extend ClassMethods @foo = 10 end end
  6. Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def

    self.included klass klass.extend ClassMethods @foo = 10 end end self == MyEnumerable
  7. Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def

    self.included klass klass.extend ClassMethods klass.class_eval do @foo = 10 end end end
  8. Class Method Calls class Foo < ActiveRecord::Base scope :foo, ->

    { ... } scope :bar, -> { ... } scope :baz, -> { ... } # other methods end
  9. Module Extraction module Findable def self.included(klass) klass.scope :foo, -> {

    ... } klass.scope :bar, -> { ... } klass.scope :baz, -> { ... } end end
  10. Module Extraction module Findable def self.included(klass) klass.scope :foo, -> {

    ... } klass.scope :bar, -> { ... } klass.scope :baz, -> { ... } end end
  11. class_eval module Findable def self.included(klass) klass.class_eval do scope :foo, ->

    { ... } scope :bar, -> { ... } scope :baz, -> { ... } end end end
  12. Concerned module Findable extend ActiveSupport::Concern included do scope :foo, ->

    { ... } scope :bar, -> { ... } scope :baz, -> { ... } end end
  13. Two Modules module Foo module ClassMethods def some_method end #

    .... end end module Bar def self.included klass klass.some_method end end
  14. Try to Fix module Foo module ClassMethods def some_method end

    end end module Bar include Foo def self.included klass klass.some_method end end
  15. Try to Fix module Foo module ClassMethods def some_method end

    end end module Bar include Foo def self.included klass klass.some_method end end