入門 名前

入門 名前

Ceda558ac47bd7af518c12a51614664a?s=128

Fujimura Daisuke

March 23, 2019
Tweet

Transcript

  1. ೖ໳ ໊લ Rails Developer Meetup 2019 ౻ଜେհ

  2. ࣗݾ঺հ • ౻ଜେհ • גࣜձࣾϚνϚν • Rails ྺ 10 ೥

    • https://twitter.com/ffu_ • https://github.com/fujimura
  3. ͜ͷൃදʹ͍ͭͯ • ౻ଜ͸໊લʹͩ͜ΘΓ͕͋Δ • ໊લʹ͍ͭͯͷৄ͍͠આ໌͕ͳ͍ͱৗʑࢥ͍ͬͯͨ • Rails͚ͩͲɺ໊લେࣄͩ͠ɺͦΖͦΖߟ͑Λ·ͱΊ͍ͨͳͱࢥͬͨ • ͪͳΈʹ WEB+DB

    Press Ͱ໊લʹ͍ͭͯͷهࣄΛॻ͍͍ͯΔ
  4. ໊લͱ͸ • ϓϩάϥϚʔ͸໊લΛຖ೔࢖͍ͬͯΔ • ॏཁͳ͜ͱ͸Θ͔͍ͬͯΔ • ͚Ͳɺͦ΋ͦ΋໊લͬͯԿʁඞཁͳͷʁͳΜͰॏཁͳͷʁྑ໊͍લͷج४ ͸ʁ • ࣮͸Α͘Θ͔Βͳ͍

  5. ͓͠ͳ͕͖ • ໊લ͸ศར • ໊લͷ࢓૊Έ • ͳ໊ͥલ͸ॏཁͳͷ͔ • ྑ໊͍લͱ͸ •

    ࣮࿥ ѱ໊͍લ
  6. ໊લ͸ศར • ·ͣ͸໊લ͕͍͔ʹศརͳπʔϧͰ͋Δ͔Λ֬ೝ͠·͢

  7. ໊લΛ࢖ΘͣʹϓϩάϥϜΛॻ͍ͯΈΑ͏ • ໊લͳ͠Ͱ΋ϓϩάϥϜ͸ॻ͚Δ • 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) ...
  8. ໊લΛ࢖ͬͯϓϩάϥϜΛॻ͍ͯΈΑ͏ • Ruby ͱ͍͏ϓϩάϥϛϯάݴޠͰૉ਺Λੜ੒͢Δίʔυ • ಡΊΔʂ • ཧղͰ͖Δʂ require 'prime'

    Prime.each
  9. ͦ΋ͦ΋ɺͳΜͷͨΊʹ໊લΛ͚ͭΔͷ • ୯ʹศར͔ͩΒɺΑΓ΋ਂ͍໨త͕͋Δ • ໊લͷҙຯΛ͔ͭͬͯɺࡉ෦Λཧղͤͣ࠶ར༻͢ΔͨΊ • ࠷ऴతʹ͸ɺੜ࢈ੑͷͨΊ

  10. ࠶ར༻ʹΑΔੜ࢈ੑͷ޲্͸ʮٕज़ʯͷࠜװ • զʑ͸ͳͥ Ruby on Rails ͕ಈ͘ͷ͔஌Βͳ͍͚Ͳ࢖͑Δ • RailsɺMRIɺσʔλϕʔεɺͦͷଞϛυϧ΢ΣΞɺCPU… •

    ࡉ෦Λཧղͤͣʹ࠶ར༻Ͱ͖Δ͜ͱ͕ٕज़ͷఆٛͷͻͱͭ • ͜ΕͰਓྨͷੜ࢈ੑ͸্͕͖ͬͯͨ • ৠؾػؔɺిؾɺίϯϐϡʔλʔ • ϓϩάϥϜ΋ͦͷҰͭ
  11. ໊લ͸ศར ·ͱΊ • ໊લ͕͋Δͱɺ࣮૷Λಡ·ͳͯ͘΋ϓϩάϥϜΛ࠶ར༻Ͱ͖Δ • ࠶ར༻Ͱ͖Δͱɺੜ࢈ੑ্͕͕Δ • ศརͲ͜ΖͰ͸ͳ͍໾ׂΛՌ͍ͨͯ͠Δ

  12. ໊લͷ͘͠Έ • ໊લ͕͍͔ʹϓϩάϥϛϯάͱ͍͏ٕज़ʹͱͬͯॏཁͳπʔϧͰ͋Δ͔͸ Θ͔Γ·ͨ͠ • ࠓ౓͸໊લͷ࢓૊ΈΛ֬ೝ͠·͢

  13. ໊લͬͯͳʹ • Կ͔ʹ͍ͭͨϥϕϧͷ͜ͱ ΓΜ͝

  14. ໊લͷҙຯͬͯͳʹ • ϥϕϧ͕ର৅Λʮࢦࣔʯ͍ͯ͠Δ͜ͱ • ʮΓΜ͝ʯͬͯݴ༿ͷҙຯʁΓΜ͝ͷ͜ͱͰ͠ΐɺͱ͍͏ͷΛ೉͘͠ݴͬ ͨܗ ΓΜ͝

  15. ϓϩάϥϜʹ͓͚Δ໊લͬͯͳʹ • ॲཧʹ͍ͭͨϥϕϧͷ͜ͱ • ϝιουͷྫ find_or_initialze_by model = Model.find(params[:id]) unless

    model Model.new(params end
  16. ϓϩάϥϜʹ͓͚Δ໊લͷҙຯͬͯͳʹ • ͦͷݴ༿͕ࢦࣔ͢͠ର৅ʁ • ڍಈʁ • ͳΜ͔௚ײతʹҧ͍ͦ͏ • ֬ೝ͠·͢

  17. ର৅Λࢦࣔ͢Δ͜ͱͩͬͨΒ • ʮ౻ଜେհʯͷҙຯ͸ɺ͜͜Ͱ஻͍ͬͯΔਓؒɺͱͳΔ • ʮΓΜ͝ʯͷҙຯ͸ɺੈքʹଘࡏ͢ΔΓΜ͝ͷશͯɺͱͳΔ • ※ ͜͜Β΁ΜͰࢦࣔઆʹແཧ͕͋Δ͜ͱ͕Θ͔ΔɻখઆͷதͷΓΜ͝ͱ ͔ •

    find_or_initialize_by ͷର৅͸ԿͳΜͩʁ • ͏·͍͔͘ͳͦ͏
  18. ڍಈͩͬͨΒ • ActiveRecord::Relation#find_or_initialize_by ͸ params Λ ड͚औͬͯΫΤϦΛ૊ΈཱͯͯϨίʔυΛ୳ͯ͠…ɻ • ͜Ε͸ڍಈɻ͋ΔछϓϩάϥϜʢ࣮૷ʣͷҙຯͱ͸͍͑ͦ͏Ͱ͸͋Δ ͕ɺͦΕʹ෇͚ͨϥϕϧͱͯ͠ͷ໊લͷҙຯͰ͸ͳ͍

    • ͜Εͩͱத਎͕Θ͔Δ͜ͱ͕ҙຯ͕Θ͔Δ͜ͱʹͳͬͯ͠·͏ • ͏·͍͔͘ͳͦ͏
  19. ௚ײతʹ͸Θ͔͍ͬͯΔ • ࢦࣔ΋ͩΊɺڍಈ΋ͩΊ • ͔͠͠զʑ͸ActiveRecord::Relation#find_or_initialize_by ͱ͍͏ϝιου໊ͷҙຯ͕Θ͔Δ • ʮݟ͚ͭΔ͔ɺͳ͔ͬͨΒ࡞Δʯ • ௚ײతʹ͸͜͏͍͏ʮৼΔ෣͍ͷઆ໌ʯ͕ҙຯͰ͋Δɺͱࢥ͍ͬͯΔͷ

    Ͱ͸ • ※ ࢖͍͑ͯΕ͹Φοέʔɺͱ͍͏ʮҙຯͷ࢖༻આʯ͕ϕʔε
  20. ϓϩάϥϜʹ͓͚Δ໊લͷҙຯ = ৼΔ෣͍ͷઆ໌ • find_or_initialize_by ͷҙຯ͸ʮݟ͚ͭΔ͔ɺͳ͔ͬͨΒ࡞Δʯ • ҙຯ = ৼΔ෣͍ͷઆ໌

    • ͬ͘͠Γ͘Δʂ
  21. ໊લͷ͘͠Έ ·ͱΊ • ϓϩάϥϜʹ͓͍ͯɺ • ໊લ͸ॲཧʹ͍ͭͨϥϕϧ • ໊લͷҙຯ͸ɺৼΔ෣͍ͷઆ໌

  22. ͳΜͰ໊લ͸ॏཁͳͷ • ໊લ͕ศརͳͷ͸Θ͔Γ·ͨ͠ • ࢓૊Έ΋Θ͔͖ͬͯͨ • Ͱɺ໊લ͕ʮѱ͍ʯͱԿ͕ى͜Δͷʁઆ໌͠·͢

  23. ѱ໊͍લͰى͖Δ͜ͱ • ཧղͷίετ͕ߴ͍ • צҧ͍Ͱόά͕ൃੜ͢Δ • => ੜ࢈ੑ͕Լ͕Δ • ςΫϊϩδʔͷ໨త͸࠶ར༻ʹΑΔੜ࢈ੑͷ޲্ɻͦΕΛѱ໊͍લ͸๦͛

    Δ • ʮΑ͍ʯ໊લॏཁ
  24. ͡Ό͋ɺԿ͕Α໊͍લͳͷ • ʰίʔυɾίϯϓϦʔτʱͰௐ΂ͯΈ·ͨ͠ • ʮ໋໊ʹ͓͍ͯ࠷΋ॏཁͳߟྀ͢΂͖఺͸ɺม਺͕දݱ͢Δ΋ͷΛਖ਼֬ʹ ׬શʹݴ͍ද͍ͯ͠Δ͜ͱʯ • McConnell, Steve. Code

    Complete (Kindle Location 6490). Microsoft Press. Kindle Edition) ຋༁͸ஶऀ • ʮਖ਼֬ʯͰʮ׬શʯͳ໊લͬͯͲ͏͍͏ঢ়ଶͰ͔͢ʁ => ॻ͍ͯͳ͍ • Կ͕ྑ໊͍લͳͷ͔ॻ͍ͯͳ͍ͷͰɺߟ͑ͯΈ·ͨ͠
  25. Α໊͍લ = ໊લͷҙຯͱ࣮ࡍͷڍಈ͕߹͍ͬͯΔ΋ͷ • ཁ͸ɺݴ͍ͬͯΔ͜ͱͱ΍͍ͬͯΔ͜ͱ͕Ұக͍ͯ͠Δ໊લ • ͦΓΌͦ͏ͩΑͶɺͱ͍͏ఆٛͳΜͰ͕͢ɺࡉ͔͘ཧղ͢ΔՁ஋͕͋Δ • ৄ͘͠આ໌͠·͢

  26. ໊લͷҙຯͱڍಈ͕߹͍ͬͯͳ͍ͱ͸ • ໊લͷҙຯ͕ڍಈʹରͯ͠ڱ͗͢Δ • ݴ͍ͬͯΔ͜ͱΛ΍͍ͬͯͳ͍ • ໊લͷҙຯ͕ڍಈʹରͯ͠޿͗͢Δ • ΍͍ͬͯΔ͜ͱΛݴ͍ͬͯͳ͍ •

    ͷ͍ͣΕ͔
  27. ύλʔϯ 1: ໊લͷҙຯ͕ڱ͗͢Δ • ʮެ։೔Λ࠶ઃఆʯͬͯҙຯͳͷʹɺ࣌ࠁΛ࠶ઃఆͰ͖ͯ͠·͏ • ΍ͬͯΔ͜ͱ໊͕લͷΘΓʹ޿͍ class Article def

    reset_publish_date(date) update!(publish_at: date) end end
  28. ύλʔϯ 2: ໊લͷҙຯ͕޿͗͢Δ • check ͨ݁͠ՌͲ͏ͳΔͷʁ • login ͍ͯ͠Δͷ͕ਖ਼ʁ͍ͯ͠ͳ͍ͷ͕ਖ਼ʁ •

    ໊લ͔Β͸Θ͔Βͳ͍ • check ͷҙຯ͕޿͗͢Δͷ͕ݪҼ before_action :check_login private def check_login unless current_user redirect_to login_path end; end
  29. Α໊͍લͷ΋͏Ұͭͷج४ γϯϓϧ͞ • ࠷΋ίϯύΫτͰత֬ͳදݱΛ࢖͏΂͠ • ੈͷதʹ͸ employee ͱ͍͏ݴ༿͕͋Δ def person_who_is_hired_by_company

    def empolyee
  30. ͜͜·Ͱͷ·ͱΊ • Α໊͍લ = ໊લͷҙຯͱ࣮ࡍͷڍಈ͕߹͍ͬͯΔɺγϯϓϧͳ໊લ • ޿͗ͨ͢Γڱ͗ͨ͢Γ͢Δͱѱ໊͍લʹͳΔ

  31. ࣮࿥ ѱ໊͍લ • ۩ମྫΛ͓ಧ͚͠·͢

  32. User#check_paid class User def check_paid # ... end end

  33. User#check_paid ͱ͍͏໊લ͔Βߟ͑ΒΕΔڍಈ • ࢧ෷͍͕ࡁΜ͔ͩΛ Boolean Ͱฦ͢ • ࢧ෷͍͕ࡁΜͰ͍ͳ͍৔߹Ϣʔβʔʹ self.errors Λ௥Ճ͢Δ

    • ࢧ෷͍͕ࡁΜͰ͍ͳ͍৔߹͸ྫ֎Λు͘ • check ͷҙຯ͕޿͗ͯ͢ɺͲΕ͔Θ͔Βͳ͍
  34. User#check_paid ղମ • ͦΕͧΕԼهͷ༷ʹ࣮૷͞Ε͍ͯΕ͹ɺ೰Ή͜ͱ͸ͳ͍͸ͣ • check Λ࢖͏ͷΛ΍ΊΔ͚ͩͰ໊લ͕ྑ͘ͳΓ·͢ class User #

    ࢧ෷͍͕ࡁΜ͔ͩΛ Boolean Ͱฦ͢ def paid? # ... end # ࢧ෷͍͕ࡁΜͰ͍ͳ͍৔߹ϢʔβʔʹΤϥʔ৘ใΛ௥Ճ͢Δ def add_error_if_not_paid # ... end end
  35. Notification#user (1) • ௨஌Λड৴͢ΔϢʔβʔͷ໊લΛ'user'ͱͨ͠ class Notification belongs_to :user end

  36. Notification#user (2) • ʮࣗ෼͕ૹͬͨ௨஌Λফ͢ʯͨΊʹɺԼهͷΑ͏ͳίʔυΛॻ͍ͨ • Notification#user ͕ड৴ऀͳͷ͔ૹ৴ऀͳͷ͔͸ɺ໊લ͔Β͸Θ͔Βͳ͍ sent_notifications = Notification.where(user:

    current_user) # user = ड৴ͨ͠ਓͳͷͰɺࣗ෼ͷ௨஌͕ফ͑Δ sent_notifications.delete_all
  37. Notification#user (3) • 'recipient'ͱ͓͚ͯ͠͹ɺ໊લ͔Βड৴ऀͰ͋Δ͜ͱ͕Θ͔ΔͷͰɺ͜ͷ Α͏ͳࣄނ͸ى͖ͳ͍ • ໘౗Ͱ΋໾ׂʹԠ໊ͨ͡લΛ͚ͭΔΑ͏ʹ͠·͠ΐ͏ # ໊લ͔Β໌Β͔ʹม sent_notifications

    = Notification.where( recipient: current_user )
  38. ༨ஊ • ͔͜͜Β͸໊લʹ·ͭΘΔ༨ஊͰ͢

  39. UserRegistrationService ͷ’Service'ͬͯඞཁʁ UserRegistrationService.new.perform! UserRegistration.new.perform! # Service͍Βͳ͘ͳ͍ʁ

  40. ໊લͷωλݩ • schema.org • Google, Microsoft, Yahoo, Yandex ͕࡞ͬͨΠϯλʔωοτ༻ͷޠኮू •

    ςʔϒϧઃܭ೰Μͩ࣌͸͜͜ΛݟΕ͹݁ߏࡌ͍ͬͯΔ
  41. ࣙॻ.app • ໊લͷͨΊʹ࠷΋ॏཁͳπʔϧ͸ࣙॻ • Mac OS X ͷ ࣙॻ.app ͸ਆ

    • ͳΜͱӳӳࣙయɺྨޠࣙయɺӳ࿨ࣙయɺ࿨ӳࣙయ͕ɺશ෦ແྉͰೖ͍ͬͯΔ • ʮ؀ڥઃఆʯ͔Β௥ՃͰ͖ΔͷͰɺ·ͩͷํ͸͍·͙͢௥Ճ͠Α͏