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
Is this Rails model?
Search
MOROHASHI Kyosuke
November 12, 2016
Programming
3
680
Is this Rails model?
MOROHASHI Kyosuke
November 12, 2016
Tweet
Share
More Decks by MOROHASHI Kyosuke
See All by MOROHASHI Kyosuke
Identifying User Idenity
moro
9
13k
Simplicity on Rails -- RDB, REST and Ruby
moro
14
17k
ちょうどよい Rails E2E テスト/enough-good-rails-e2e-test
moro
5
2.3k
フレームワークを作らない方法/How NOT to build frameworks
moro
4
1.1k
全体がいい感じになるために、私たちRailsをホームにするWeb技術者ができること/let-our-whole-system-grow
moro
2
1.9k
フォームオブジェクトとの向き合い方/Grow Form Objects up
moro
1
1.9k
チームによるいきいきとしたソフトウェア開発/an-alive-team-grows-software
moro
3
3.3k
Web-E2E-Testing-from-Ruby
moro
5
510
Joyful user foundation restructuring with Ruby
moro
9
19k
Other Decks in Programming
See All in Programming
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
CSC305 Lecture 26
javiergs
PRO
0
140
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
useSyncExternalStoreを使いまくる
ssssota
6
1.2k
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
260
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
780
From Translations to Multi Dimension Entities
alexanderschranz
2
130
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
180
선언형 UI에서의 상태관리
l2hyunwoo
0
180
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
Beyond ORM
77web
7
960
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
65
11k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Done Done
chrislema
181
16k
Automating Front-end Workflow
addyosmani
1366
200k
The Cost Of JavaScript in 2023
addyosmani
45
7k
4 Signs Your Business is Dying
shpigford
181
21k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Transcript
Is this Rails model? @2016-11-12 #ckpd_LT ٕज़෦ ։ൃج൫ @moro ॾڮګհ
‣ ུྺ ‣ 3BJMTᴈ໌ظ͔Β ΄Ͳ3BJMT͖ͬͯͨ ‣ ࠷͍ۙͬͯΔ͜ͱ ‣ 3BJMTΞϓϦͷઃܭࢧԉίʔυϨϏϡʔ ‣
΄͔ʹΞδϟΠϧ͓͡͞Μͱ͔ ‣ ͪΖΜࣗͰίʔυॻ͍ͯΔ
‣ 10ઓ͖ͬͯͨRubyͱRailsɺWebαʔϏεͷ ՁΛϢʔβʔʹಧ͚ΔϨʔϧʹͳ͍ͬͯΔ ‣ ϒϥβ͚: HTML, JS & CSS ‣
ΞϓϦ͚: JSON over HTTP ‣ ֤छσʔλετΞͷಡΈॻ͖ Why: ͜ͷ͕Ͳ͏ʹཱͪͦ͏͔
‣ ϒϥβ͚ʹΓΓॻ͘ ‣ ΞϓϦ͚ͷAPI࡞Δͷʹͦͦ͜͜ॻ͘ ‣ ผαʔϏεͰ࡞ͬͨσʔλΛϢʔβʹಧ͚ΔͨΊʹ ͪΐͬͱॻ͘ ‣ DWHͷूܭσʔλ ‣
ػցֶशͷྨ݁Ռ… ͜Ε͔Βͷ͍ΖΜͳΤϯδχΞʹͱͬͯͷRails
Is this Rails model? dେͳΓখͳΓ3BJMTΛ͞ΘΔͱ͖ͷΨΠυ
Before Rails
‣ Enterprise Java ‣ PofEAA, PofEAAಡॻձ ‣ Ruby Ұ෦ͰΘΕ͍͕ͯͨʮࣄͰ͏ݴޠʯ Ͱͳ͔ͬͨɻ
‣ 2001-04: tDiary ‣ 2002- kagemai Before Rails: ~2003
‣ จܥେֶੜ ‣ େखిػϝʔΧʔʹSEͱͯ͠ब৬ ‣ 100+ਓɺ(?)͕͔ΓͷϓϩδΣΫτʹଐ ‣ ϓϩδΣΫτͷޮԽπʔϧΛ࡞ΔͷʹRuby͞ΘΔ NF!#FGPSF3BJMT
‣ וాͷຊͰങͬͨ ‣ net/telnetͰSolarisʹϩάΠϯͯ͠ ‣ γΣϧεΫϦϓτ࣮ߦͯ͠ ‣ ग़དྷͨϑΝΠϧΛnet/ftpͰ࣋ͬͯ͘ΔεΫϦϓτʹ ‣
VisualuRubyͰUI͚ͭͯ ‣ exerbͰݻΊͯϙϯஔ͖
Rails was coming
‣ 2005-09: AgileWebDevelopment with Rails ‣ 2005-12: Rails 1.0
‣ 2007-12: Rails 2.0
‣ “Sustainable productivity for web-application development. ” ‣ 5ͰϒϩάΛ࡞Δ ‣
Opinionated framework: ओு͕ڧ͍ ‣ MVC, DRY… Rails was coming : 2004-2007
‣ At the heart of MVC (sniff) is what I
call Separated Presentation. ‣ In MVC, the domain element is referred to as the model. Model objects are completely ignorant of the UI. ‣ http://martinfowler.com/eaaDev/uiArchs.html MVC
‣ app/{views,controllers,models} ‣ app/models ʹ AcitveRecord::Base Λ ܧঝͨ͠”Ϟσϧ”ͷܗ͕Ͱ͖Δ Rails’ scaffold
MVC
‣ σʔλϕʔεͷςʔϒϧϏϡʔͷྻΛϥοϓ͠ɺ σʔλϕʔεΞΫηεΛΧϓηϧԽ͠ɺυϝΠϯϩδο ΫΛՃ͢Δ ‣ http://bliki-ja.github.io/pofeaa/ActiveRecord/ Active recordύλʔϯ
‣ ΞΫηαΛDBεΩʔϚ͔Βಈతʹఆٛ ‣ fkͰࢀরͰ͖Δςʔϒϧؒͷؔ࿈(association) Ruby͔Βѻ͍͍͢ ‣ (͔ΜͨΜʹ͏ʹ)SQLΛॻ͔ͳ͍͍ͯ͘ ActiveRecord: Rails default
ORM
‣ ຊRubyΧϯϑΝϨϯε2006 ͰͷDHHΩʔϊʔτ ‣ http://www.slideshare.net/vishnu/ discovering-a-world-of-resources-on-rails ‣ WebͷੈքCRUDʹநԽͰ͖ΔͷͰ? RESTful &
ActiveResource
‣ POST /groups/42/membership ‣ ͋ΘͤͯಡΈ͍ͨ: ָʑERDϨοεϯ ʮͰ͖͝ͱʯͷΤϯςΟςΟԽ
‣ Rails͕େϒʔϜʹ ‣ ʮϞσϧʯͱ͍͑ActiveRecord ‣ ग़དྷࣄΛΤϯςΟςΟԽ͢ΕRESTfulΠέͦ͏ ‣ ʮ͖Ε͍ͳRESTʯͱDBͷϨίʔυΛXML(!!)Ͱ ఏڙ͢Δ͜ͱ Rails
was coming : 2004-2007
me @ 2004 - 2007 ‣ Railsษڧձ@౦ژ ‣ લ৬
ESMʹస৬ ‣ ࣄͰRailsΛ͍࢝ΊΔ ‣ RSpec
RailsϒʔϜ
‣ 2010-08: Rails 3.0 ‣ Merb, Rack, Arel ‣ ·͞ʹ3rd
system
‣ ARʹϩδοΫΛॻ͔ͳ͍ͷ͕ྲྀߦΔ(?) ‣ ෳςʔϒϧͷϨίʔυΛಡΈॻ͖Ͱ͖ͳ͘ͳΔ ‣ ʮͦ͏ͩ! ίϯτϩʔϥʹॻ͜͏!!ʯ Anemic domain model
(υϝΠϯϞσϧශ݂)
‣ ίϯτϩʔϥʹϩδοΫ͕ॻ͔ΕΔ ‣ ڞ௨Խςετ͕͘͠ͳΔ ‣ MVCͱ Fat Controller
‣ HTTPܦ༝ͰϨίʔυΛ࿐ग़͢Δ͚ͩͰΓͳ͍ ‣ N+1 ‣ APIΫϥΠΞϯτͱαʔόͷີ݁߹Խ ‣ ΠϕϯτΤϯςΟςΟͷ٫ ‣ Anemic
REST ʮ͖Ε͍ͳRESTʯͱARes
‣ ͍ΖΜͳاۀͰΘΕΔΑ͏ʹͳͬͨ ‣ ελʔτΞοϓͳαʔϏεͰ ‣ ΠϯϋεͰ ‣ ٕज़తෛ࠴ΛෛͬͯαʔϏεΛ࢝ΊΔ/ҭͯΔ࣌ظ RailsϒʔϜ: 2008-2011
‣ RailsϨγϐϒοΫ, Rails 3Ϩγϐ ϒοΫ, ͡ΊΔCucumber!ͳͲ ग़൛ ‣ RailsͰςετΛॻ͖ͳ͕Βίʔυ Λॻ͍͍ͯͨɻ
me@ 2008-2011
Fat models & beyond: 2011-
‣ 2013-06: Rails 4.0 ‣ Bundler, StrongParameters, Better ActiveModel ‣
2016-08: Rails 5.0 ‣ ActiveJob, RailsAPI…
‣ ϩδοΫΛίϯτϩʔϥʹஔ͘ͱϝϯςφϯεੑ͕ Լ͕Δ ‣ ʮͦ͏ͩ! Ϟσϧʹॻ͜͏!!ʯ ‣ Ϟσϧ = ARϞσϧ
Fat Models
‣ UserRecipeͱ͍ͬͨαʔϏεͷओཁΤϯςΟςΟ ͕1,000+ߦ͘Β͍(Ժͳදݱ) ʹͳΔɻ ‣ όον͔Βsaveͨ࣌͠ʹൃՐ͢ΔίʔϧόοΫό Ϧσʔγϣϯɻ ‣ helper hell
or දࣔ༻ϩδοΫͷϞσϧՃɻ Fat Models
‣ ʮϨίʔυʯ͡Όͳ͍ϞσϧͷநϨΠϠ͕Ͱ͖ͨɻ ‣ Rails 4+ Ͱ͔ͳΓ͍͘͢ɻ ‣ FormObjectύλʔϯ ActiveModel::Model
‣ ϞσϧΫϥεԣஅతͳؔ৺͝ͱΛConcernʹநग़͠ ͨɻ ‣ app/{m,v,c}/concerns ‣ ࣮ଶmixinͳͷͰҙɻ AS::Concern
‣ AMo::ValidatorΛܧঝͨ͠όϦσʔγϣϯ ϩδοΫͷநग़ɻ ‣ app/validators/ Validatorͷ
‣ Draper ActiveDecorator ͕ ‣ දࣔ༻ϩδοΫΛϞσϧ͔Βͤ͞Δɻ ‣ helperͰ໊͋ͬͨલۭؒڞ༗ୀ۶ͳୈҰҾ ੍͕͞ΕΔɻ
‣ Ϟσϧͷ͕ݮΔɻ Decorator / Presenter
‣ 2-3ͭ΄ͲͷʮηοτͰѻ͏σʔλʯΛαʔϏεಛఆ ͷΫϥεͱͯ͠நग़ͨ͢͠ͷ ‣ ௨՟(୯Ґ, ֹۚ) ‣ ൣғ(։࢝, ऴྃ) ͳͲͳͲ
ValueObject (ΦϒδΣΫτ)
‣ ෳςʔϒϧͷૢ࡞ͦΕࣗମΛΦϒδΣΫτͱͯ͠ நग़͢Δɻ ‣ ͋Δը໘͔ΒͷΈ͏ϩδοΫΛɺڞ༗͞ΕΔAR Ϟσϧ͔Βͤ͞Δɻ ‣ cf: DCIͷӨڹ ‣
http://magazine.rubyist.net/?0041-RailsTheBadParts#l9 Interaction Object, Operation Object or Service Layer
‣ ϑϨʔϜϫʔΫͷ”͍͢͝”ΫϥεΛܧঝ͠ͳ͍ ʮ;ͭ͏ͷʯ(plain old)ͳΫϥεɻ ‣ ૉʹॲཧΛநग़ͯ͠OOP͢ΕΑ͍ɻ ‣ VOେ͜Ε ‣ αʔϏεΫϥεॏްͳΫϥεͳ͍ํ͕Αͦ͞͏?
PORO: Plain Old Ruby Object
‣ https://github.com/trailblazer/trailblazer ‣ ͱ͍͏Α͏ͳ͜ͱΛʮ͜͜Ζ͕͚ʯ͔Β࣮ʹͯ͠ ͍Δgem ‣ Opinionated ͳͷͰ·Δͬͱ࠾༻Ͱ͖Δ͔Ͳ͏͔ͱ ͔͘ɺࢀߟʹͳΓͦ͏ɻ Trailblazer
‣ ඇಉظ ‣ ෳDB (ਨׂ, ਫฏׂ, RW-Spliting, NoSQL) ‣ APIܦ༝ͷଞγεςϜ࿈ܞ
‣ JavaScriptͷ·͢·͢ͷߴԽ ‣ WebSocket … More & more new challengers!
‣ Fat C ΑΓϚγ͕ͩ Fat M ͭΒ͍ɻ ‣ AR͚͕ͩϞσϧ͡Όͳ͍ɻ ‣
VOServiceLayerMVCͰ͍͏ϞσϧͷҰछ ‣ ઐ༻ͷσΟϨΫτϦ΄͍͍ͬͯ ‣ Railsͷ্ͰͩͬͯOOPͰ͖Δ! ΦϨλνͷFatModelͱͷઓ͍͜Ε͔Βͩ
‣ Railsͱ͔RSpecͱ͔nिͷπϥϛΛຯΘ͍ͳ͕Β αʔϏε։ൃࢧԉ ‣ ίʔυϨϏϡʔͱ͔ ‣ GitHubͰͷιϑτΣΞ։ൃ͕ҰൠԽͨ͠ ‣ 2015 Cookpadʹస৬ͨ͠
‣ RubyRailsͷઃܭվળͱ͔ΞδϟΠϧ͓͡͞Μ me@ 2012-
Is this Rails model?
‣ ͨͿΜϞσϧͰ͢ɻ ‣ ͦͷ্Ͱׂ͍ͯ͘͠ͱ͍͍: ‣ දࣔϩδοΫΛdecoratorʹ ‣ VOΛ୳ͦ͏ ‣ ෳϞσϧΛ͞ΘΓ͚ͨΕαʔϏεϨΠϠநग़͢Δͱྑ͍͔?
Yes, maybe…
‣ ϢʔβʔʹՁΛಧ͚ΔͷʹඞཁͳϨʔϧͱͯ͠ ॻ͘ͱ͖ʹͨͷ͘͠ॻ͍ͯ΄͍͠ͳɻ ‣ ΨοπϦͱɺRailsͱOOઃܭপʹ·ͬͯॻ͘ͷ ͨͷ͍͠ͷͰҰॹʹͬͯ͘ΕΔਓͬͯ·͢ɻ ͳΜ͔ͩΜ͍ͩͬͯRailsͨͷ͍͠