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
php-conference-japan-2024
tasuku43
0
330
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
510
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
命名をリントする
chiroruxx
1
420
Recoilを剥がしている話
kirik
5
6.8k
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
270
MCP with Cloudflare Workers
yusukebe
2
220
たのしいparse.y
ydah
3
120
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
230
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
Beyond ORM
77web
7
980
Featured
See All Featured
Music & Morning Musume
bryan
46
6.2k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Building Your Own Lightsaber
phodgson
103
6.1k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Why Our Code Smells
bkeepers
PRO
335
57k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Practical Orchestrator
shlominoach
186
10k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
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ͨͷ͍͠