Upgrade to Pro — share decks privately, control downloads, hide ads and more …

フォームオブジェクトとの向き合い方/Grow Form Objects up

フォームオブジェクトとの向き合い方/Grow Form Objects up

Talk at Rails DM Day3 extreme

See also https://railsdm.herokuapp.com/issues/78

MOROHASHI Kyosuke

July 14, 2018
Tweet

More Decks by MOROHASHI Kyosuke

Other Decks in Programming

Transcript

  1. ‣ 7 Patterns to Refactor Fat ActiveRecord Models ‣ https://codeclimate.com/blog/7-ways-to-decompose-fat-

    activerecord-models/ ‣Realworld Domain Model on Rails ‣ https://speakerdeck.com/joker1007/realworld-domain- model-on-rails ͍ͨ΁ΜͩΑͶɺɺɺͱ͍͏ͷ͕஌ΒΕ͖ͯͨ
  2. ‣ ίϯτϩʔϥʹͯparams͔Β஋ΛऔΓग़͠ɺHash΍
 จࣈྻͱͯ͠ѻ͏ɻ ‣ ࣮ଶ͕ AC::Parameters Ͱ΋ Hash ͷΑ͏ʹѻ͏ɻ৺ͷ໨ͰܕΛݟΔ ‣

    HTTP༝དྷͷσʔλ΋ɺrequest͔ΒऔΓग़͓ͯ͘͠ɻ ‣ #user_agent ͱ͔ɺΫοΩʔͷ஋ͱ͔΋ ೖྗΛ͸͖ͬΓ  
  3. ‣ ͜ͷ”෼͚ΔͨΊͷ૚”΁ͷೖྗ͸POROʹͳΔͷͰɺςε τ͔Β؆୯ʹݺͼग़ͤΔɻ ‣ PORO: Plain Old Ruby Object =

    ී௨ͷΦϒδΣΫτ ‣ ؆୯ʹݺͼग़ͤΔͷͰɺଞͷςετͰͷσʔληοτΞο ϓʹ΋࢖͑Δɻ ςετՄೳ  
  4. email = '[email protected]' form = UserRegistrationForm.new(email: email) form.call token =

    extract_token_from_sent_email(AM::Base.deliveries.last) expect(token).not_to be_nil expect(UserRegistration.find_by(email: email, uuid: token)).to be_present ྫ1: ೖྗ͞ΕͨϝΞυΛอଘͯ֬͠ೝURLΛϝʔϧ͢Δͷͷςετ  
  5. > ӬଓԽͷͨΊʹαʔϏε૚Λઃ͚͍ͨ   email = '[email protected]' form = UserRegistrationForm.new(email:

    email) form.call token = extract_token_from_sent_email(AM::Base.deliveries.last) expect(token).not_to be_nil expect(UserRegistration.find_by(email: email, uuid: token)).to be_present ‣ ಺෦ʹ΋͏1ϨΠϠઃ͚ͯ΋ɺϓϨθϯςʔγϣϯͱͷΠϯ λʔϑΣʔε͸มΘΒͣ҆ఆ͍ͯ͠Δɻ
  6. > Pub/SubͰૄ݁߹ʙ   UserRegistrationForm.new(email: email).call + OurJob.execute_all! token =

    extract_token_from_sent_email(AM::Base.deliveries.last) ‣ ΠϯλʔϑΣʔε͸҆ఆͨ͠··ɺΞʔΩςΫνϟมߋʹ ඞཁͳमਖ਼͚ͩΛೖΕΕ͹Α͍ɻ
  7. ‣ A: ͲͬͪͰ΋͍͍ ‣ ࣗಈςετ͕͋ͬͯϦϑΝΫλϦϯά͠΍͘͢ͳ͍ͬͯΕ͹okɻ ‣ ʮೖྗ஋ΛͱΓ·ͱΊͯόϦσʔγϣϯ·ͰΛ͢Δ૚ʯͱɺ
 ʮͦͷvalidͳೖྗ஋ͰॲཧΛ͢Δ૚ʯʹ෼ׂͯ͠΋͍͍Μ͡Όͳ͍͔ͳɻ ‣ ࠷ॳʹখ͘͞࡞Δͱ͖UIͱ͚ۙͮͨ΄͏͕͍͍Μ͡Όͳ͍͔ͳ

    ‣ ʮϑΥʔϜʯͬͯͭ·ΓɺαʔϏεΛఏڙ͢Δଆ͕ར༻ऀʹظ଴͢Δ
 ߦಈͦͷ΋ͷͩΑͶɻ ‣ ͦ͜ʹԊͬͨ΋ͷΛ࡞Δͷ͕ૉ௚͡Όͳ͍͔ͳɻ αʔϏεϨΠϠ͡Όͳͯ͘ϑΥʔϜΦϒδΣΫτਪ͠ͳͷ͸ͳͥ?  
  8. ‣ Ϗϡʔ / ίϯτϩʔϥ / AR::B ͷࡾ૬ߏ଄Ͱ·͔ͳ͑ͳͦ͏ ͳ৔߹ɺബ͍૚Λ΋͏Ұ૚࡞ͬͯΈΔͱΑ͍ɻ ‣ ίϯτϩʔϥ͕ѻ͏Webͷೖྗ஋ͱɺϩδοΫʹඞཁͳ


    ೖྗ஋ͷڥքͱͳΔ͜ͱΛҙࣝ͢Δɻ ‣ φΠʔϒ͔ͭςετ͕ॻ͖΍͍͢ϨΠϠ͔Β͸͡Ίͯɺ
 ൓෮͠ͳ͕ΒมԽ͍͚ͯ͠ΔΑ͏ʹ! ·ͱΊ