Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
入門 名前
Search
Fujimura Daisuke
March 23, 2019
Programming
23
13k
入門 名前
Fujimura Daisuke
March 23, 2019
Tweet
Share
More Decks by Fujimura Daisuke
See All by Fujimura Daisuke
庭と負債
fujimura
4
1.1k
AIの時代で我々はどのようにコードを書くのか
fujimura
3
790
SaaSを作るという仕事について
fujimura
12
5.7k
一文字エイリアスのすすめ
fujimura
0
320
現役CTOが語る!RubyKaigiの楽しみ方
fujimura
0
1.2k
いかにして文系新卒エンジニアが「大きな問い」を大事にするCTOになったのか
fujimura
2
670
Kaigi on Rails 2022 - 既存Railsアプリ攻略法 CTOが見ること・やること・考えること
fujimura
14
3.9k
SimpleDelegator活用のご提案
fujimura
0
1.5k
それPostgreSQLでできるよ @ Rails Developer Meetup 2018 Day 1
fujimura
10
3.9k
Other Decks in Programming
See All in Programming
Symfony Mapper Component
soyuka
1
160
React CompilerとFine Grained Reactivityと宣言的UIのこれから / The next chapter of declarative UI
ssssota
7
3k
AWS AppSyncを用いた GraphQL APIの開発について - NIFTY Tech Talk #22
niftycorp
PRO
0
110
Jakarta EE meets AI
ivargrimstad
0
960
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
10
3.7k
Missing parts when designing and implementing Android UI
ericksli
0
370
急成長期の品質とスピードを両立するフロントエンド技術基盤
soarteclab
0
480
競技プログラミングで 基礎体力を身につけよう / You can get basic skills through competitive programming
mdstoy
0
130
TypeScript でバックもやるって実際どう? 実運用で困ったこと3選
yuichiro_serita
17
7.4k
チームにとって最適なスキルアップ施策とは何か/what-is-the-best-skill-up-approach-for-team
nobuoooo
0
160
イマのCSSでできる インタラクション最前線 + CSS最新情報
clockmaker
5
3.8k
layerx_20241129.pdf
kyoheig3
2
190
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.4k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
The Cost Of JavaScript in 2023
addyosmani
45
6.9k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Being A Developer After 40
akosma
87
590k
For a Future-Friendly Web
brad_frost
175
9.4k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Producing Creativity
orderedlist
PRO
341
39k
Side Projects
sachag
452
42k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Transcript
ೖ ໊લ Rails Developer Meetup 2019 ౻ଜେհ
ࣗݾհ • ౻ଜେհ • גࣜձࣾϚνϚν • Rails ྺ 10
• https://twitter.com/ffu_ • https://github.com/fujimura
͜ͷൃදʹ͍ͭͯ • ౻ଜ໊લʹͩ͜ΘΓ͕͋Δ • ໊લʹ͍ͭͯͷৄ͍͠આ໌͕ͳ͍ͱৗʑࢥ͍ͬͯͨ • Rails͚ͩͲɺ໊લେࣄͩ͠ɺͦΖͦΖߟ͑Λ·ͱΊ͍ͨͳͱࢥͬͨ • ͪͳΈʹ WEB+DB
Press Ͱ໊લʹ͍ͭͯͷهࣄΛॻ͍͍ͯΔ
໊લͱ • ϓϩάϥϚʔ໊લΛຖ͍ͬͯΔ • ॏཁͳ͜ͱΘ͔͍ͬͯΔ • ͚Ͳɺ໊ͦͦલͬͯԿʁඞཁͳͷʁͳΜͰॏཁͳͷʁྑ໊͍લͷج४ ʁ • ࣮Α͘Θ͔Βͳ͍
͓͠ͳ͕͖ • ໊લศར • ໊લͷΈ • ͳ໊ͥલॏཁͳͷ͔ • ྑ໊͍લͱ •
࣮ ѱ໊͍લ
໊લศར • ·໊ͣલ͕͍͔ʹศརͳπʔϧͰ͋Δ͔Λ֬ೝ͠·͢
໊લΛΘͣʹϓϩάϥϜΛॻ͍ͯΈΑ͏ • ໊લͳ͠ͰϓϩάϥϜॻ͚Δ • Lazy K ͱ͍͏ϓϩάϥϛϯάݴޠͰૉΛੜ͢ΔίʔυʢͷҰ෦ʣ • ͭΒ͍ʂ (SII(S(K(S(S(K(SII(S(S(KS)(S(K(S(KS)))(S(K(S(S(KS)(SS(S(S(KS)K))(KK)))))
(S(S(KS)(S(KK)(S(KS)(S(S(KS)(S(KK)(S(KS)(S(S(KS)(S(KK)(SII))) (K(SI(KK)))))))(K(S(K(S(S(KS)(S(K(SI))(S(KK)(S(K(S(S(KS)K)(S(S(KS)K)I) ...
໊લΛͬͯϓϩάϥϜΛॻ͍ͯΈΑ͏ • Ruby ͱ͍͏ϓϩάϥϛϯάݴޠͰૉΛੜ͢Δίʔυ • ಡΊΔʂ • ཧղͰ͖Δʂ require 'prime'
Prime.each
ͦͦɺͳΜͷͨΊʹ໊લΛ͚ͭΔͷ • ୯ʹศར͔ͩΒɺΑΓਂ͍త͕͋Δ • ໊લͷҙຯΛ͔ͭͬͯɺࡉ෦Λཧղͤͣ࠶ར༻͢ΔͨΊ • ࠷ऴతʹɺੜ࢈ੑͷͨΊ
࠶ར༻ʹΑΔੜ࢈ੑͷ্ʮٕज़ʯͷࠜװ • զʑͳͥ Ruby on Rails ͕ಈ͘ͷ͔Βͳ͍͚Ͳ͑Δ • RailsɺMRIɺσʔλϕʔεɺͦͷଞϛυϧΣΞɺCPU… •
ࡉ෦Λཧղͤͣʹ࠶ར༻Ͱ͖Δ͜ͱ͕ٕज़ͷఆٛͷͻͱͭ • ͜ΕͰਓྨͷੜ࢈ੑ্͕͖ͬͯͨ • ৠؾػؔɺిؾɺίϯϐϡʔλʔ • ϓϩάϥϜͦͷҰͭ
໊લศར ·ͱΊ • ໊લ͕͋Δͱɺ࣮Λಡ·ͳͯ͘ϓϩάϥϜΛ࠶ར༻Ͱ͖Δ • ࠶ར༻Ͱ͖Δͱɺੜ࢈ੑ্͕͕Δ • ศརͲ͜ΖͰͳׂ͍ΛՌ͍ͨͯ͠Δ
໊લͷ͘͠Έ • ໊લ͕͍͔ʹϓϩάϥϛϯάͱ͍͏ٕज़ʹͱͬͯॏཁͳπʔϧͰ͋Δ͔ Θ͔Γ·ͨ͠ • ࠓ໊લͷΈΛ֬ೝ͠·͢
໊લͬͯͳʹ • Կ͔ʹ͍ͭͨϥϕϧͷ͜ͱ ΓΜ͝
໊લͷҙຯͬͯͳʹ • ϥϕϧ͕ରΛʮࢦࣔʯ͍ͯ͠Δ͜ͱ • ʮΓΜ͝ʯͬͯݴ༿ͷҙຯʁΓΜ͝ͷ͜ͱͰ͠ΐɺͱ͍͏ͷΛ͘͠ݴͬ ͨܗ ΓΜ͝
ϓϩάϥϜʹ͓͚Δ໊લͬͯͳʹ • ॲཧʹ͍ͭͨϥϕϧͷ͜ͱ • ϝιουͷྫ find_or_initialze_by model = Model.find(params[:id]) unless
model Model.new(params end
ϓϩάϥϜʹ͓͚Δ໊લͷҙຯͬͯͳʹ • ͦͷݴ༿͕ࢦࣔ͢͠ରʁ • ڍಈʁ • ͳΜ͔ײతʹҧ͍ͦ͏ • ֬ೝ͠·͢
ରΛࢦࣔ͢Δ͜ͱͩͬͨΒ • ʮ౻ଜେհʯͷҙຯɺ͜͜Ͱ͍ͬͯΔਓؒɺͱͳΔ • ʮΓΜ͝ʯͷҙຯɺੈքʹଘࡏ͢ΔΓΜ͝ͷશͯɺͱͳΔ • ※ ͜͜ΒΜͰࢦࣔઆʹແཧ͕͋Δ͜ͱ͕Θ͔ΔɻখઆͷதͷΓΜ͝ͱ ͔ •
find_or_initialize_by ͷରԿͳΜͩʁ • ͏·͍͔͘ͳͦ͏
ڍಈͩͬͨΒ • ActiveRecord::Relation#find_or_initialize_by params Λ ड͚औͬͯΫΤϦΛΈཱͯͯϨίʔυΛ୳ͯ͠…ɻ • ͜Εڍಈɻ͋ΔछϓϩάϥϜʢ࣮ʣͷҙຯͱ͍͑ͦ͏Ͱ͋Δ ͕ɺͦΕʹ͚ͨϥϕϧͱͯ͠ͷ໊લͷҙຯͰͳ͍
• ͜Εͩͱத͕Θ͔Δ͜ͱ͕ҙຯ͕Θ͔Δ͜ͱʹͳͬͯ͠·͏ • ͏·͍͔͘ͳͦ͏
ײతʹΘ͔͍ͬͯΔ • ࢦࣔͩΊɺڍಈͩΊ • ͔͠͠զʑActiveRecord::Relation#find_or_initialize_by ͱ͍͏ϝιου໊ͷҙຯ͕Θ͔Δ • ʮݟ͚ͭΔ͔ɺͳ͔ͬͨΒ࡞Δʯ • ײతʹ͜͏͍͏ʮৼΔ͍ͷઆ໌ʯ͕ҙຯͰ͋Δɺͱࢥ͍ͬͯΔͷ
Ͱ • ※ ͍͑ͯΕΦοέʔɺͱ͍͏ʮҙຯͷ༻આʯ͕ϕʔε
ϓϩάϥϜʹ͓͚Δ໊લͷҙຯ = ৼΔ͍ͷઆ໌ • find_or_initialize_by ͷҙຯʮݟ͚ͭΔ͔ɺͳ͔ͬͨΒ࡞Δʯ • ҙຯ = ৼΔ͍ͷઆ໌
• ͬ͘͠Γ͘Δʂ
໊લͷ͘͠Έ ·ͱΊ • ϓϩάϥϜʹ͓͍ͯɺ • ໊લॲཧʹ͍ͭͨϥϕϧ • ໊લͷҙຯɺৼΔ͍ͷઆ໌
ͳΜͰ໊લॏཁͳͷ • ໊લ͕ศརͳͷΘ͔Γ·ͨ͠ • ΈΘ͔͖ͬͯͨ • Ͱɺ໊લ͕ʮѱ͍ʯͱԿ͕ى͜Δͷʁઆ໌͠·͢
ѱ໊͍લͰى͖Δ͜ͱ • ཧղͷίετ͕ߴ͍ • צҧ͍Ͱόά͕ൃੜ͢Δ • => ੜ࢈ੑ͕Լ͕Δ • ςΫϊϩδʔͷత࠶ར༻ʹΑΔੜ࢈ੑͷ্ɻͦΕΛѱ໊͍લ͛
Δ • ʮΑ͍ʯ໊લॏཁ
͡Ό͋ɺԿ͕Α໊͍લͳͷ • ʰίʔυɾίϯϓϦʔτʱͰௐͯΈ·ͨ͠ • ʮ໋໊ʹ͓͍ͯ࠷ॏཁͳߟྀ͖͢ɺม͕දݱ͢ΔͷΛਖ਼֬ʹ શʹݴ͍ද͍ͯ͠Δ͜ͱʯ • McConnell, Steve. Code
Complete (Kindle Location 6490). Microsoft Press. Kindle Edition) ༁ஶऀ • ʮਖ਼֬ʯͰʮશʯͳ໊લͬͯͲ͏͍͏ঢ়ଶͰ͔͢ʁ => ॻ͍ͯͳ͍ • Կ͕ྑ໊͍લͳͷ͔ॻ͍ͯͳ͍ͷͰɺߟ͑ͯΈ·ͨ͠
Α໊͍લ = ໊લͷҙຯͱ࣮ࡍͷڍಈ͕߹͍ͬͯΔͷ • ཁɺݴ͍ͬͯΔ͜ͱͱ͍ͬͯΔ͜ͱ͕Ұக͍ͯ͠Δ໊લ • ͦΓΌͦ͏ͩΑͶɺͱ͍͏ఆٛͳΜͰ͕͢ɺࡉ͔͘ཧղ͢ΔՁ͕͋Δ • ৄ͘͠આ໌͠·͢
໊લͷҙຯͱڍಈ͕߹͍ͬͯͳ͍ͱ • ໊લͷҙຯ͕ڍಈʹରͯ͠ڱ͗͢Δ • ݴ͍ͬͯΔ͜ͱΛ͍ͬͯͳ͍ • ໊લͷҙຯ͕ڍಈʹରͯ͗͢͠Δ • ͍ͬͯΔ͜ͱΛݴ͍ͬͯͳ͍ •
ͷ͍ͣΕ͔
ύλʔϯ 1: ໊લͷҙຯ͕ڱ͗͢Δ • ʮެ։Λ࠶ઃఆʯͬͯҙຯͳͷʹɺ࣌ࠁΛ࠶ઃఆͰ͖ͯ͠·͏ • ͬͯΔ͜ͱ໊͕લͷΘΓʹ͍ class Article def
reset_publish_date(date) update!(publish_at: date) end end
ύλʔϯ 2: ໊લͷҙຯ͕͗͢Δ • check ͨ݁͠ՌͲ͏ͳΔͷʁ • login ͍ͯ͠Δͷ͕ਖ਼ʁ͍ͯ͠ͳ͍ͷ͕ਖ਼ʁ •
໊લ͔ΒΘ͔Βͳ͍ • check ͷҙຯ͕͗͢Δͷ͕ݪҼ before_action :check_login private def check_login unless current_user redirect_to login_path end; end
Α໊͍લͷ͏Ұͭͷج४ γϯϓϧ͞ • ࠷ίϯύΫτͰత֬ͳදݱΛ͏͠ • ੈͷதʹ employee ͱ͍͏ݴ༿͕͋Δ def person_who_is_hired_by_company
def empolyee
͜͜·Ͱͷ·ͱΊ • Α໊͍લ = ໊લͷҙຯͱ࣮ࡍͷڍಈ͕߹͍ͬͯΔɺγϯϓϧͳ໊લ • ͗ͨ͢Γڱ͗ͨ͢Γ͢Δͱѱ໊͍લʹͳΔ
࣮ ѱ໊͍લ • ۩ମྫΛ͓ಧ͚͠·͢
User#check_paid class User def check_paid # ... end end
User#check_paid ͱ͍͏໊લ͔Βߟ͑ΒΕΔڍಈ • ࢧ͍͕ࡁΜ͔ͩΛ Boolean Ͱฦ͢ • ࢧ͍͕ࡁΜͰ͍ͳ͍߹Ϣʔβʔʹ self.errors ΛՃ͢Δ
• ࢧ͍͕ࡁΜͰ͍ͳ͍߹ྫ֎Λు͘ • check ͷҙຯ͕͗ͯ͢ɺͲΕ͔Θ͔Βͳ͍
User#check_paid ղମ • ͦΕͧΕԼهͷ༷ʹ࣮͞Ε͍ͯΕɺΉ͜ͱͳ͍ͣ • check Λ͏ͷΛΊΔ͚ͩͰ໊લ͕ྑ͘ͳΓ·͢ class User #
ࢧ͍͕ࡁΜ͔ͩΛ Boolean Ͱฦ͢ def paid? # ... end # ࢧ͍͕ࡁΜͰ͍ͳ͍߹ϢʔβʔʹΤϥʔใΛՃ͢Δ def add_error_if_not_paid # ... end end
Notification#user (1) • ௨Λड৴͢ΔϢʔβʔͷ໊લΛ'user'ͱͨ͠ class Notification belongs_to :user end
Notification#user (2) • ʮ͕ࣗૹͬͨ௨Λফ͢ʯͨΊʹɺԼهͷΑ͏ͳίʔυΛॻ͍ͨ • Notification#user ͕ड৴ऀͳͷ͔ૹ৴ऀͳͷ͔ɺ໊લ͔ΒΘ͔Βͳ͍ sent_notifications = Notification.where(user:
current_user) # user = ड৴ͨ͠ਓͳͷͰɺࣗͷ௨͕ফ͑Δ sent_notifications.delete_all
Notification#user (3) • 'recipient'ͱ͓͚ͯ͠ɺ໊લ͔Βड৴ऀͰ͋Δ͜ͱ͕Θ͔ΔͷͰɺ͜ͷ Α͏ͳࣄނى͖ͳ͍ • ໘ͰׂʹԠ໊ͨ͡લΛ͚ͭΔΑ͏ʹ͠·͠ΐ͏ # ໊લ͔Β໌Β͔ʹม sent_notifications
= Notification.where( recipient: current_user )
༨ஊ • ͔͜͜Β໊લʹ·ͭΘΔ༨ஊͰ͢
UserRegistrationService ͷ’Service'ͬͯඞཁʁ UserRegistrationService.new.perform! UserRegistration.new.perform! # Service͍Βͳ͘ͳ͍ʁ
໊લͷωλݩ • schema.org • Google, Microsoft, Yahoo, Yandex ͕࡞ͬͨΠϯλʔωοτ༻ͷޠኮू •
ςʔϒϧઃܭΜͩ࣌͜͜ΛݟΕ݁ߏࡌ͍ͬͯΔ
ࣙॻ.app • ໊લͷͨΊʹ࠷ॏཁͳπʔϧࣙॻ • Mac OS X ͷ ࣙॻ.app ਆ
• ͳΜͱӳӳࣙయɺྨޠࣙయɺӳࣙయɺӳࣙయ͕ɺશ෦ແྉͰೖ͍ͬͯΔ • ʮڥઃఆʯ͔ΒՃͰ͖ΔͷͰɺ·ͩͷํ͍·͙͢Ճ͠Α͏