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
Speeding up Instance Variables in Ruby 3.3
tenderlove
2
340
[Feature #20425] Speeding up delegate methods
tenderlove
3
210
RailsConf 2023
tenderlove
29
970
Don't @ Me! Faster Instance Variables with Object Shapes
tenderlove
1
410
RailsConf 2022 Keynote
tenderlove
2
500
Some Assembly Required
tenderlove
1
520
HexDevs 2021
tenderlove
1
390
Compacting GC for MRI
tenderlove
60
4.5k
But At What Cost?
tenderlove
9
14k
Other Decks in Programming
See All in Programming
Rubyでつくるパケットキャプチャツール
ydah
0
170
月刊 競技プログラミングをお仕事に役立てるには
terryu16
1
1.2k
php-conference-japan-2024
tasuku43
0
430
return文におけるstd::moveについて
onihusube
1
1.4k
歴史と現在から考えるスケーラブルなソフトウェア開発のプラクティス
i10416
0
300
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
1.3k
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
6
700
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.2k
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
550
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
1.4k
Androidアプリの One Experience リリース
nein37
0
1.2k
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
180
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
51
7.3k
The Invisible Side of Design
smashingmag
299
50k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Learning to Love Humans: Emotional Interface Design
aarron
274
40k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Code Reviewing Like a Champion
maltzj
521
39k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
500
Music & Morning Musume
bryan
46
6.3k
Building Adaptive Systems
keathley
38
2.4k
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
Site-Speed That Sticks
csswizardry
3
270
A Philosophy of Restraint
colly
203
16k
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!