Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
8
12k
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
500
Joyful user foundation restructuring with Ruby
moro
9
19k
Other Decks in Programming
See All in Programming
AWS AppSyncを用いた GraphQL APIの開発について - NIFTY Tech Talk #22
niftycorp
PRO
0
110
似たもの同士のPerlとPHP
uzulla
1
110
as(型アサーション)を書く前にできること
marokanatani
10
3k
Cursorでアプリケーションの追加開発や保守をどこまでできるか試したら得るものが多かった話
drumnistnakano
0
240
新卒研修で作ったアプリのご紹介
mkryo
0
210
N.E.X.T LEVEL
pluu
2
230
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
120
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.2k
Leverage LLMs in Java with LangChain4j and Quarkus
hollycummins
0
110
Vapor Revolution
kazupon
2
2.4k
DevTools extensions で 独自の DevTool を開発する | FlutterKaigi 2024
kokiyoshida
0
290
Welcome JSConf.jp 2024
yosuke_furukawa
PRO
0
2.9k
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
Writing Fast Ruby
sferik
627
61k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Raft: Consensus for Rubyists
vanstee
136
6.7k
Facilitating Awesome Meetings
lara
50
6.1k
Done Done
chrislema
181
16k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
The Cost Of JavaScript in 2023
addyosmani
45
6.9k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
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ͨͷ͍͠