Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
What the hell is ActiveSupport::Concern?
Search
Aaron Patterson
December 04, 2012
Programming
5
270
What the hell is ActiveSupport::Concern?
A short talk about ActiveSupport::Concern.
Aaron Patterson
December 04, 2012
Tweet
Share
More Decks by Aaron Patterson
See All by Aaron Patterson
[Feature #20425] Speeding up delegate methods
tenderlove
1
33
RailsConf 2023
tenderlove
9
580
Don't @ Me! Faster Instance Variables with Object Shapes
tenderlove
0
200
RailsConf 2022 Keynote
tenderlove
1
350
Some Assembly Required
tenderlove
0
400
HexDevs 2021
tenderlove
0
300
Compacting GC for MRI
tenderlove
59
4.3k
But At What Cost?
tenderlove
8
14k
Compacting GC in MRI v2
tenderlove
1
610
Other Decks in Programming
See All in Programming
GoでParserを書く
karupanerura
3
390
Adding Security to Microcontroller Ruby
sylph01
1
160
TypeScriptとGraphQLで実現する 型安全なAPI実装 / TSKaigi 2024
hokaccha
5
2.8k
地方こそサーバーレス、その意義に迫るサーバーレスPHP / Serverless PHP: The Rural Areas, and Why Serverless PHP Matters
seike460
PRO
2
110
CQRS meets modern Java
simas
PRO
2
480
TypeScript Custom GitHub Action Development Tips
peaceiris
2
500
株式会社ゼネテック
genetec
0
120
Good first issues of TypeProf
mame
4
590
An adventure of Happy Eyeballs
coe401_
1
230
[RubyKaigi 2024] Ruby Mixology 101: adding shots of PHP, Elixir, and more
palkan
0
130
Exploring the Implementation of “t.Run”, “t.Parallel”, and “t.Cleanup”
akarin
1
160
mb_trim関数を作りました
youkidearitai
PRO
1
230
Featured
See All Featured
Creatively Recalculating Your Daily Design Routine
revolveconf
211
11k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
How GitHub (no longer) Works
holman
305
140k
How to Ace a Technical Interview
jacobian
273
22k
Infographics Made Easy
chrislema
238
18k
GraphQLとの向き合い方2022年版
quramy
33
13k
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
Scaling GitHub
holman
457
140k
Imperfection Machines: The Place of Print at Facebook
scottboms
261
12k
WebSockets: Embracing the real-time Web
robhawkes
59
7k
Designing on Purpose - Digital PM Summit 2013
jponch
111
6.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
117
18k
Transcript
Thanks!
Hark
Substantial
What the hell is ActiveSupport::Concern?
I have no idea.
U Concerned, Bro?
Aaron Patterson @tenderlove
Lead Pun Architect
Substantial, Thanks
None
Gorbachev Puff-Puff Thunderhorse
None
None
None
None
None
None
Rails Core ~ 3 years (I think)
Confession
What’s in a name?
Classes ❤ File ❤ Hash ❤ Set ❤ Array
Modules ❤ Enumerable ❤ Comparable
AS::Concern
“Concern” is a noun
“Concern” is a noun
“Son, We’re Concerned”
Grown Man.
Stay Classy Change Your Self Track Dependents
Stay Classy.
Class Methods module MyEnumerable def my_first end module ClassMethods def
my_new end end def self.included(klass) klass.extend ClassMethods end end
Including class Aaron include MyEnumerable end p Aaron.respond_to?(:my_new) # =>
true p Aaron.new.respond_to?(:my_first) # => true
With Concern module MyEnumerable extend ActiveSupport::Concern def my_first end module
ClassMethods def my_new end end end
Including class Aaron include MyEnumerable end p Aaron.respond_to?(:my_new) # =>
true p Aaron.new.respond_to?(:my_first) # => true
Be Careful!
Normal Include module MyEnumerable def my_first end end class Aaron
p singleton_methods # => [] include MyEnumerable p singleton_methods # => [] end
Normal Extend module MyEnumerable def my_first end end class Aaron
p singleton_methods # => [] extend MyEnumerable p singleton_methods # => [:my_first] end
Users May Be Surprised
Do I need this?
Are you coupled?
Change Your Self.
Setting up ivars
Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def
self.included klass klass.extend ClassMethods end end
Warning! class Aaron include MyEnumerable p foo end
Warning! class Aaron include MyEnumerable p foo end @foo not
initialized
Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def
self.included klass klass.extend ClassMethods @foo = 10 end end
Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def
self.included klass klass.extend ClassMethods @foo = 10 end end self == MyEnumerable
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
Working Code class Aaron include MyEnumerable p foo # =>
10 end
With Concern module MyEnumerable extend ActiveSupport::Concern module ClassMethods attr_accessor :foo
end included do @foo = 10 end end self == included class
Isolating Setup
Class Method Calls class Foo < ActiveRecord::Base scope :foo, ->
{ ... } scope :bar, -> { ... } scope :baz, -> { ... } # other methods end
Module Extraction module Findable def self.included(klass) klass.scope :foo, -> {
... } klass.scope :bar, -> { ... } klass.scope :baz, -> { ... } end end
Module Extraction module Findable def self.included(klass) klass.scope :foo, -> {
... } klass.scope :bar, -> { ... } klass.scope :baz, -> { ... } end end
class_eval module Findable def self.included(klass) klass.class_eval do scope :foo, ->
{ ... } scope :bar, -> { ... } scope :baz, -> { ... } end end end
Concerned module Findable extend ActiveSupport::Concern included do scope :foo, ->
{ ... } scope :bar, -> { ... } scope :baz, -> { ... } end end
Track Your Dependents.
Two Modules module Foo module ClassMethods def some_method end #
.... end end module Bar def self.included klass klass.some_method end end
Bar depends on Foo
Usage class Aaron include Foo include Bar end
Usage - BOOM! class Aaron include Bar include Foo end
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
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
Fix with class_eval module Bar def self.included klass klass.class_eval do
include Foo end klass.some_method end end
Concerned module Bar extend ActiveSupport::Concern include Foo included do some_method
end end
Concerned module Bar extend ActiveSupport::Concern include Foo included do some_method
end end NOT Ruby’s Include
When should I be concerned?
You probably shouldn’t be.
Can you simplify?
Many Dependencies
THANKS!