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

アプリケーションを作るときに考える25のこと

 アプリケーションを作るときに考える25のこと

2019-03-22 Rails Developers Meetup 2019
https://railsdm.github.io/

Takafumi ONAKA

March 22, 2019
Tweet

More Decks by Takafumi ONAKA

Other Decks in Technology

Transcript

  1. աڈͷൃද w 3BJMT%FWFMPQFST.FFUVQ w ;ͭ͏ͷ3BJMTΞϓϦέʔγϣϯ։ൃ w 3BJMT%FWFMPQFST.FFUVQ -5  w

    34QFD͙͠͞ w 3BJMT%FWFMPQFST.FFUVQ w ʮࣾ಺πʔϧ࡞੒αʔΫϧʯ׆ಈه࿥ ࢿྉฆࣦ😢
  2. ͸ͯͳͱ3VCZ w 1FSMͷձࣾͩͱੈؒʹࢥΘΕ͍ͯΔؾ͕͢Δ w ࣄ࣮ͱͯ͠͸ׂ͙Β͍ʁ XFCϑϩϯτεϚϗ΋͋ΔͷͰ  w 4DBMB (P

    1ZUIPO౳ʑ΋ΨοπϦ࢖͍ͬͯΔ w ྫ͑͹.BDLFSFM΍ɺ͸ͯϒͷόοΫΤϯυ͸4DBMBͩ͠ɺ.BDLFSFM BHFOU౳ʑͰ(P͕͋Γ·͢ w ͍͔ʹͯ͠զʑ͸೥΋ͷͷ1FSMϓϩμΫτΛ4DBMBͰϦϓϨʔε͔ͨ͠ 4DBMB.BUTVSJ એ఻  w ΫϥΠΞϯτ͸4XJGU ,PUMJO 5ZQF4DSJQU 3FBDU/BUJWF
  3. ػೳͱHFN w Ϛϧνςφϯτ BQBSUNFOU  w Ϣʔβೝূ PNOJBVUI EFWJTF 

    w ϑΝΠϧΞοϓϩʔυ BDUJWF@TUPSBHF  w ه๏ʴ4ZOUBY)JHIMJHIU SFEDBSQFU SPVHF  w ฤूཤྺ QBQFS@USBJM EJ ff Z  w ϖʔδωʔτ LBNJOBSJ  w λά BDUTBTUBHHBCMFPO  w ݕࡧ SBOTBDL
  4. Ձ஋͕͋Δ͕೉͍͠ w ͢Δͱࠓ͸΋ͪΖΜʮ3FBDU࢖͑ʯͱݴ͏࿩ʹͳΓ·͢ΑͶ w "1*ʹಛԽͨ͠όοΫΤϯυͱɺ8FCϑϩϯτΤϯυΛ෼ׂ͠ ࣮ͯ૷͠Α͏ɺ(SBQI2- "QPMMP 3FBDUͰ΍͍ͬͯ͘Μͩʂ όοΫΤϯυ͸"1*ಛԽͩ͠HPMBOHͰߦͧ͘ʂ w

    ͢Δͱࠓ౓͸ྫ͑͹Ϣʔβೝূ΍؅ཧը໘ͷ࡞੒͕೉͘͠ͳΔɻ ϩάΠϯ࣌ʹ+85ฦͯ͠MPDBMTUPSBHFʹอଘ͢ΔΑ͏ͳ DPPLJFͷ࠶࣮૷ͱ͔ɺ؅ཧը໘༻͸FOUSZQPJOUΛ෼͚ͨϏϧυ Λ͠ͳ͚Ε͹ͳΒͳ͍ͱ͔ɺ୯ʹ%#ͷςʔϒϧͷσʔλΛදࣔ ͍͚ͨͩ͠ͳͷʹԿͰ͜Μͳ໘౗ͳ͜ͱ΍ͬͯΔͷΈ͍ͨͳɻ
  5. ֓ཁ঺հ w (FOFSBUPS w $POTPMF w .JHSBUJPO w 34QFD w

    ͜Ε͚ͩ෼͔Ε͹%#͔Β)5.-ฦ͢࡞ۀ͸Ͱ͖Δͱࢥ͏
  6. 4MJN w 1FSMͷ55ͩͱԋࢉ݁ՌΛඳը͢Δ͔Ͳ͏͔͕෼͔ΓͮΒ͍ w 3BJMT͸AA͕͋Δͱ͖͚ͩඳը͢Δྑ͍ϧʔϧ ੲ͸ҧͬͨ  w ࣾ಺ʹ)5.-4MJNཱ͓͍ͯͯͨΑ w

    2ʮ֮͑Δͷ೉͘͠ͳ͍ʁʯ w લ৬ͩͱि͔͔ؒΒͣʹΈΜͳ׳ΕͯͨΑ w ཌिͷ;Γ͔͑ΓͰʮ4MJN࠷ߴʯͬͯ,FFQ͕ग़ͯ͘Δײ͡
  7. &3#͕ݟ௚͞Εͭͭ͋Δ w 3FBDUͷོ੝Ͱ4MJN)BNM+49ͷॻ͖׵͕͑සൟʹൃੜ w ʮͦ΋ͦ΋)5.- FSC Ͱॻ͍͍ͯΕ͹͜Μͳۤ࿑͸ʜʜʯ w ೔໨ʹ࿩ͨ͠<ٕज़બ୒ͷํ਑>Λࢥ͍ग़ͯ͠ʂ w

    ೉͍͠ʮਖ਼͍͠)5.-Λ؆୯ʹॻ͍ͯฦ͢͜ͱʯʹ஫ྗد༩͠ ͍ͯΔͷ͕4MJN)BNM w +49͸%0.πϦʔ͕յΕ͍ͯͨΒΤϥʔʹͳΔͷͰɺਖ਼͍͠ )5.-Λॻ͘͜ͱ΋؆୯ʹ͍ͯ͠Δ -JOUཁΒͣ
  8. QBHZ w "33FMBUJPOΛҾ਺ʹͱͬͯɺϖʔδωʔτ༻ͷΦϒδΣΫτͱɺMJNJU  P ff TFU͕ޮ͍ͨ"33FMBUJPOΛฦ͠·͢ w ϖʔδωʔτ͸ϓϨθϯςʔγϣϯϩδοΫͳͷͰ w

    Λ࡞ΔͨΊ͚ͩʹϞσϧʹϝιουΛ଍͢ͷ͸͓͔͍͠ɻϖʔδωʔτઐ ༻ͷΦϒδΣΫτΛ࡞Ε͹े෼ɺͱ͍͏ߟ͑ํɻ࣮૷΋ͱͯ΋γϯϓϧɻ @pagy, @records = pagy(Product.some_scope) « First ‹ Prev ... 2 3 4 5 6 7 8 9 10 ... Next › Last »
  9. LBNJOBSJͷXJUIPVU@DPVOU w QBHFϝιουΛݺͿͱɺMJNJU P ff TFUΛ෇͚ͨ"33FMBUJPOΛฦ͠ ͯ͘ΔଞʹɺFYUFOEJOHͰ໭Γ஋ͷ"33FMBUJPOΛ֦ு͢Δ w ͜ͷ֦ு͕UPUBM@QBHFͱ͔QFSͱ͔ͷϝιουΛ͍࣋ͬͯΔ w

    XJUIPVU@DPVOU͸ɺߋʹ֦ுΛॏͶͯɺQBHFͰੜ͑ͨϝιουΛ্ ॻ͖͢Δ w -*.*5 Ͱ౤͛ɺOFYU@QBHF͸݁Ռ͕QFS@QBHF ͔݅͋ͬͨ w "33FMBUJPOͱ͍͏஗ԆൃՐΦϒδΣΫτ͕͋Δͱ࢖͍΍͍͢
  10. ਓʹ஫໨͢Δͱ໘ന͍ w DBSSJFSXBWFΛ࡞ͬͨ!KOJDLMBT͕SF fi MFΛ࡞ͬͨ ͨͿΜ  w 3VCZ fi

    MFVQMPBET UBLF w SF fi MFʹ݁ߏίϯτϦϏϡʔτ͍ͯ͠Δ!KBOLP͸TISJOFͷ࡞ऀ ͩ͠JNBHF@QSPDFTTJOHͷ࡞ऀͰ΋͋Δ
  11. ࡟আϑϥά w ͦ͜ͰEJTDBSE w EFGBVMU@TDPQF͸ೖΕͣɺͨͩTDPQFΛఏڙ͢Δ͚ͩ w ͜Ε͸࿙Ε·͘ΔͷͰ͸ʜʜʁ ໊ਪཧ  ʮEFGBVMU@TDPQFΛ࢖͏ͳʯ͕ఆઆʹͳ͍ͬͨͤͰੈք͕࿪ΜͰΔ

    User.first # SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1 User.kept.first # SELECT `users`.* FROM `users` WHERE `users`.`discarded_at` IS NULL
 ORDER BY `users`.`id` ASC LIMIT 1
  12. ࡟আϩά w ΞϓϦέʔγϣϯ͕γϯϓϧʹͳΔ w ෺ཧ࡟আ͍ͯ͠ΔͷͰؾʹ͢Δ͜ͱ͕ͳ͍ w 6/*26&੍໿Ͱ΋֎෦Ωʔ੍໿Ͱ΋ͲΜͱདྷ͍ w 3%#ͷ༰ྔΛؾʹ͢Δ͜ͱ͕ݮΔ w

    ࡟আτϥϯβΫγϣϯΛදݱ͠΍͍͢ w ϩάΛసૹͯ͠Α͏΍͘ݕࡧՄೳʹͳΔͷͰɺऔΓग़ͦ͏ͱࢥ͏ͱ࡟আޙ ਺෼଴ͨͳ͍ͱ͍͚ͳ͍ͷ͕σϝϦοτ
  13. HFNΛ୳͢ w [VOJPOTUPSF3VCZͰ(JU)VCݕࡧ w IUUQTHJUIVCDPNTFBSDI M3VCZR[VOJPOTUPSFUZQF$PEF w શ෦ݟͨ ͙݅Β͍ͩͬͨͷͰ࿙Ε͸͋Δ 

    w CP ffi OHFN͕Ϥαι΢ʂʂ w IUUQTHJUIVCDPNIFZDBSTUFOCP ff i O w 4UBS ౰࣌ ɺ࠷ऴίϛοτ೥લ͚ͩͲ࡞Γ͕៉ྷͰಡΈ΍͍͢
  14. ఆ൪ͷHFN w SFEDBSQFUͱLSBNEPXOͷڧͳΜ͚ͩͲɺ࠷ۙ ͜͜೥͙Β͍  DPNNPONBSLFSͱ͍͏ͷ͕ڧ͘ͳ͖ͬͯͨ w ໊લͷ௨Γɺ͜Ε͸$PNNPO.BSLʹରԠ͍ͯ͠Δ w DNBSL

    $PNNPO.BSL༻ͷ$ϥΠϒϥϦ ͷGPSLͰ͋ΔDNBSL HGNͷXSBQQFSɺͭ·Γ(JU)VC'MBWPSFE.BSLEPXOʹରԠ w ͱ͜ΖͰLSBNEPXO +FLZMMͷதͰ࢖ΘΕ͍ͯΔ ͸"UUSJCVUF-JTU %F fi OJUJPOT͕ؾ࣋ͪѱͯ͘޷͖ͳͷͰɺ͜Ε͸͜ΕͰྲྀߦͬͯཉ͠ ͍͚Ͳɺࠓ͔Β࡞ΔͳΒDPNNPONBSLFSʹ͓ͯ͘͠ͷ͕Ϥαι΢
  15. )5.-1JQFMJOF w ʮ5SFF͕Ͳ͏͍͏OPEFͩͬͨΒ fi MUFSΛ࣮ߦ͢Δ͔ʯͱ͍͏৚݅ Ͱม׵ର৅ΛߜΔ͜ͱ͕Ͱ͖Δ w )5.-5SFF͔Β)5.-5SFF΁ͷม׵ͳͷͰɺద༻ॱͰ)5.-͕ յΕΔ͜ͱ͕ແ͍ w

    ద༻લޙ͕ͦΕͧΕ)5.-5SFFͳͷͰςετ͕ॻ͖΍͍͢ w ͱྑ͍͜ͱͣ͘Ί͚ͩͲɺͦ΋ͦ΋೉͍͜͠ͱΛ͍ͯ͠ΔͷͰɺಠ ࣗه๏Λ૿΍͢ͱ͖͸֮ޛͯ͠ྟΈ·͠ΐ͏
  16. "DUJWF.PEFM%JSUZ # ੜ੒ (or औಘ) ௚ޙ͸ changed? ͸ false u

    = User.create(name: "Taro") u.changed? #=> false # มߋ͢Δͱ มߋͨ͠಺༰͕෼͔Δ u.name = "Jiro" u.changed? #=> true u.changes #=> {"name"=>["Taro", "Jiro"]} # อଘ͢Δͱมߋ಺༰͸ΫϦΞ͞ΕΔ u.save! u.changed? #=> false u.changes #=> {}
  17. ؂ࠪϩάͷ࡞Γํ class RequestStore def initialize(app) @app = app end def

    call(env) before = Thread.current[:_rack_request] Thread.current[:_rack_request] = Rack::Request.new(env) @app.call(env) ensure Thread.current[:_rack_request] = before end end
  18. ؂ࠪϩάͷ࡞Γํ class User < ApplicationRecord before_save :store_changes def store_changes #

    Thread.current[:_rack_request] ͰࢀরͰ͖Δɻ # গ͠ߟ͑ͳ͍ͱ console ΍ job ౳ɺrack Λ࢖Θͳ͍อଘ࣌ʹࠔΔͷͰ஫ҙ end end
  19. ࠩ෼දࣔ w EJ ff FSHFN w NBSLEJ ff HFN w

    .BSLEPXOΛม׵ͨ͠)5.-ಉ࢜ͷࠩ෼Λੜ੒͢Δ2JJUB w Ͱ2JJUBͷฤूཤྺΛ·Δͬͱ࠶ݱՄೳͰ͢ w ฤूཤྺ͕ݟ΍͘͢ͳΓ·ͨ͠2JJUB#MPH