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
1
88
[Feature #20425] Speeding up delegate methods
tenderlove
3
140
RailsConf 2023
tenderlove
28
810
Don't @ Me! Faster Instance Variables with Object Shapes
tenderlove
1
240
RailsConf 2022 Keynote
tenderlove
2
400
Some Assembly Required
tenderlove
1
440
HexDevs 2021
tenderlove
1
340
Compacting GC for MRI
tenderlove
60
4.4k
But At What Cost?
tenderlove
9
14k
Other Decks in Programming
See All in Programming
What you can do with Ruby on WebAssembly
kateinoigakukun
0
170
Scala アプリケーションのビルドを改善してデプロイ時間を 1/4 にした話 | How I improved the build of my Scala application and reduced deployment time by 4x
nomadblacky
1
180
How to Break into Reading Open Source
kaspth
2
220
Hono・Prisma・AWSでGeoなAPI開発
nokonoko1203
5
680
私のEbitengineの第一歩
qt_luigi
0
450
What is Parser
yui_knk
9
4.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
0
120
Some more adventure of Happy Eyeballs
coe401_
2
190
Understand the mechanism! Let's do screenshots tests of Compose Previews with various variations / 仕組みから理解する!Composeプレビューを様々なバリエーションでスクリーンショットテストしよう
sumio
3
800
Perl 5 OOP機構30年史 - Perl 5's OOP Mechanism over the past 30 years
moznion
0
320
意外とフォントが大事だった話 / Font Issues on Internationalization
fumi23
0
110
Why Prism?
kddnewton
4
1.7k
Featured
See All Featured
Code Review Best Practice
trishagee
62
16k
In The Pink: A Labor of Love
frogandcode
139
22k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
230
17k
Why You Should Never Use an ORM
jnunemaker
PRO
53
8.9k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
278
13k
Ruby is Unlike a Banana
tanoku
96
11k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
326
21k
Rebuilding a faster, lazier Slack
samanthasiow
78
8.6k
Pencils Down: Stop Designing & Start Developing
hursman
119
11k
Side Projects
sachag
451
42k
Learning to Love Humans: Emotional Interface Design
aarron
270
40k
From Idea to $5000 a Month in 5 Months
shpigford
379
46k
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!