$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Solid foundations for Rails apps
Search
Mariusz Lusiak
April 22, 2012
Programming
4
180
Solid foundations for Rails apps
A talk given at Railsberry, the European Rails Conference, on Apr 20, 2012.
Mariusz Lusiak
April 22, 2012
Tweet
Share
More Decks by Mariusz Lusiak
See All by Mariusz Lusiak
OOP in Rails applications
mariuszlusiak
7
300
Other Decks in Programming
See All in Programming
複数人でのCLI/Infrastructure as Codeの暮らしを良くする
shmokmt
5
2.3k
UIデザインに役立つ 2025年の最新CSS / The Latest CSS for UI Design 2025
clockmaker
18
7.4k
宅宅自以為的浪漫:跟 AI 一起為自己辦的研討會寫一個售票系統
eddie
0
500
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
690
これならできる!個人開発のすゝめ
tinykitten
PRO
0
100
組み合わせ爆発にのまれない - 責務分割 x テスト
halhorn
1
150
Canon EOS R50 V と R5 Mark II 購入でみえてきた最近のデジイチ VR180 事情、そして VR180 静止画に活路を見出すまで
karad
0
110
ローターアクトEクラブ アメリカンナイト:川端 柚菜 氏(Japan O.K. ローターアクトEクラブ 会長):2720 Japan O.K. ロータリーEクラブ2025年12月1日卓話
2720japanoke
0
730
안드로이드 9년차 개발자, 프론트엔드 주니어로 커리어 리셋하기
maryang
1
110
Go コードベースの構成と AI コンテキスト定義
andpad
0
120
JETLS.jl ─ A New Language Server for Julia
abap34
1
400
WebRTC、 綺麗に見るか滑らかに見るか
sublimer
1
160
Featured
See All Featured
Being A Developer After 40
akosma
91
590k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Code Review Best Practice
trishagee
74
19k
Git: the NoSQL Database
bkeepers
PRO
432
66k
The Language of Interfaces
destraynor
162
25k
Context Engineering - Making Every Token Count
addyosmani
9
510
Java REST API Framework Comparison - PWX 2021
mraible
34
9k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Practical Orchestrator
shlominoach
190
11k
The Pragmatic Product Professional
lauravandoore
37
7.1k
Six Lessons from altMBA
skipperchong
29
4.1k
Facilitating Awesome Meetings
lara
57
6.7k
Transcript
Solid foundations for Rails apps
Love
A sip of frustration
OOP
ISP SRP DRY DI Liskov Open/Closed Least surprise Tell Don’t
Ask Law of Demeter Convention over Configuration Domain Language Capture
ISP SRP DRY DI Liskov Open/Closed Least surprise Tell Don’t
Ask Law of Demeter Convention over Configuration Domain Language Capture
SRP
Change
class User attr_reader :name def bandwidth_usage(month) # Calculate bandwidth usage
end end
class User attr_reader :name end class BandwidthUsage def initialize(user) user
= @user end def by_month(month) # Calculate bandwidth usage end end
Indicators
class Shipment def update_destination(name, address) @first_name, @last_name = name.split(' ')
@address = address end def update_last_name(name) @last_name = name.split(' ').last end end
class Shipment def update_destination(name, address) @first_name, @last_name = extract_names(name) @address
= address end def update_last_name(name) @last_name = extract_names(name).last end private def extract_names(name) name.split(' ') end end
class NameParser def initialize(name) # ... end def first_name #
... end def last_name # ... end end
class Shipment def update_destination(name, address) name_parser = NameParser.new(name) @first_name =
name_parser.first_name @last_name = name_parser.last_name @address = address end def update_last_name(name) name_parser = NameParser.new(name) @last_name = name_parser.last_name end end
Rails vs SRP
Active Record
Data Mapper
Solutions for AR
class User < ActiveRecord::Base include Favourites include Pictures include Organizations
include Embeddable include SocialProfiles # ... end
None
class Post < ActiveRecord::Base include FigLeaf hide ActiveRecord::Base, ancestors: true,
except: [Object, :init_with, :new_record?, :errors, :valid?, :save] hide_singletons ActiveRecord::Calculations, ActiveRecord::FinderMethods, # ...
Yet another reason
Is it easier to add a method to an existing
class or create a new class with that method?
Domain Language Capture
Writers
Coders
Managers, Helpers etc.
All the layers
Communication
None
1 class Tag 2 3 # ... 4 5 end
None
None
1 class Tag 2 3 # ... 4 5 end
Confusion
Unify naming, stay in sync
DRY
Model attributes, Partials, Routes
WET?
None
Knowledge
Comments
# Concatenates the salt with the password salt + password
# Concatenates the salt with the password (password + salt).hash
class User attr_accessor :admin end
user.admin = true
Name the business process
class User attr_accessor :admin def grant_admin_privileges @admin = true end
end
class GrantsAdminPrivileges def initialize(user) @user = user end def execute
@user.admin = true end end
SRP DLC DRY
@mariuszlusiak