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
670
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
6
10k
Simplicity on Rails -- RDB, REST and Ruby
moro
14
16k
ちょうどよい 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.8k
フォームオブジェクトとの向き合い方/Grow Form Objects up
moro
1
1.9k
チームによるいきいきとしたソフトウェア開発/an-alive-team-grows-software
moro
3
3.3k
Web-E2E-Testing-from-Ruby
moro
5
500
Joyful user foundation restructuring with Ruby
moro
9
19k
Other Decks in Programming
See All in Programming
Outline View in SwiftUI
1024jp
1
340
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
2
170
C++でシェーダを書く
fadis
6
4.1k
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
1k
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
250
Functional Event Sourcing using Sekiban
tomohisa
0
100
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
Ethereum_.pdf
nekomatu
0
470
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Done Done
chrislema
181
16k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Become a Pro
speakerdeck
PRO
25
5k
Documentation Writing (for coders)
carmenintech
65
4.4k
Happy Clients
brianwarren
98
6.7k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
A Philosophy of Restraint
colly
203
16k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Practical Orchestrator
shlominoach
186
10k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
The Language of Interfaces
destraynor
154
24k
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ͨͷ͍͠