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
CSC509 Lecture 09
javiergs
PRO
0
140
React への依存を最小にするフロントエンド設計
takonda
5
1.1k
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
RubyLSPのマルチバイト文字対応
notfounds
0
120
Jakarta EE meets AI
ivargrimstad
0
240
Jakarta EE meets AI
ivargrimstad
0
620
CSC509 Lecture 13
javiergs
PRO
0
110
距離関数を極める! / SESSIONS 2024
gam0022
0
290
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
1
110
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.2k
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
KATA
mclloyd
29
14k
Building an army of robots
kneath
302
43k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Scaling GitHub
holman
458
140k
How STYLIGHT went responsive
nonsquared
95
5.2k
Unsuck your backbone
ammeep
668
57k
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ͨͷ͍͠