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

My patches for Rails 5

My patches for Rails 5

東急Ruby会議10 LT #tqrk10

08d5432a5bc31e6d9edec87b94cb1db1?s=128

Takashi Kokubun

May 29, 2016
Tweet

Transcript

  1. My patches for Rails 5 @k0kubun TokyuRubyձٞ10

  2. ࣗݾ঺հ • Twitter, GitHub: @k0kubun • Cookpad • ٕज़෦։ൃج൫άϧʔϓ •

    RailsͷϑΝϯ • ίϛοτ਺͸7
  3. ࠓ೔࿩͢͜ͱ • ๻͕ Rails 5.0 ʹ͍ΕͨҎԼͷύονʹؔͯ͠ࢥ͏ ͜ͱΛ࿩͠·͢ • #22722 Use

    CGI.escapeHTML for html escape • #18109 Allow joins to be unscoped • #22125 Alias left_joins to left_outer_joins
  4. Use CGI.escapeHTML for html escape https://github.com/rails/rails/pull/22722

  5. #22722 Use CGI.escapeHTML for html escape

  6. #22722 Use CGI.escapeHTML for html escape • erbͷHTMLΤεέʔϓʹgsubͷݺͼग़͠Ͱ͸ ͳ͘ CGI.escapeHTML

    Λ࢖͏Α͏ʹ͢Δౕ • Ruby 2.3 Ͱ CGI.escapeHTML Λߴ଎Խͨ͠
  7. CGI.escapeHTMLͷߴ଎Խύον

  8. • ݩ͸୯ͳΔ gsub(/[&"'><]/, { '&' => '&amp;', .. }) •

    ͜ΕΛ͔ͳΓ۪௚ʹCʹॻ͖௚͚ͨͩ͠ • UTF-8ؚΉASCIIޓ׵ͷΤϯίʔσΟϯάͰ࠷େ 7ഒߴ଎Խ • ext/cgi/escape ര஀ CGI.escapeHTMLͷߴ଎Խύον
  9. ͳͥHTMLΤεέʔϓΛߴ଎Խ ͔ͨ͠ • RailsͷerbͷϨϯμϦϯάͰHTMLΤεέʔϓ͕Ϙ τϧωοΫͩͬͨ͠ɺଞʹ଎͘͠Α͏͕ͳ͍͔Β

  10. Ͳͷ͘Β͍มΘ͔ͬͨ • લఏ஌ࣝ: String࿈݁ͷݱࡏͷੑೳൺֱ TMJNͷDPNQJMFECFODIͷ)5.-Τεέʔϓ༗ޮ൛ JT    

         IBNMJU W TMJN W GBNM W FSVCJT W IBNM WCFUB https://travis-ci.org/k0kubun/hamlit/jobs/131694989
  11. Ͳͷ͘Β͍มΘ͔ͬͨ • ActionViewͰͷݩʑͷੑೳൺֱ https://gist.github.com/k0kubun/e9363d8359d9a9604c55bd23fb28c64d લͷϖʔδͷౕͷ"DUJPO7JFXԽ൛ FSCͷ$(*FTDBQF)5.-Խͳ͠ JT   

          IBNMJU W TMJN W GBNM W "75FNQMBUF )FMQFST&SVCJT IBNM WCFUB
  12. Ͳͷ͘Β͍มΘ͔ͬͨ • ActionViewͰͷerbͷCGI.escapeHTMLԽͷޮՌ https://gist.github.com/k0kubun/3ea0c0dba78ac2e832c8fbe5a104d04b લͷϖʔδͷౕͷ"DUJPO7JFXԽ൛ FSCͷ$(*FTDBQF)5.-Խ͋Γ JT   

          IBNMJU W TMJN W GBNM W "75FNQMBUF )FMQFST&SVCJT IBNM WCFUB
  13. RailsͰ࠷଎ͷςϯϓϨʔτΤϯδϯ • Hamlit ͔ σϑΥϧτͷ ActionView::Template::Handlers::Erubis • Ͳͬͪ΋๻͕଎ౕͨ͘͠ͳͷͰͲͬͪ࢖ͬͯ ͘Εͯ΋͍͍Ͱ͢

  14. Allow joins to be unscoped https://github.com/rails/rails/pull/18109

  15. #18109 Allow joins to be unscoped

  16. #18109 Allow joins to be unscoped • (join͢Δ)includes, eager_load, joins

    Ͱ unscopedΛ࢖ͬͯ default_scope ͕֎ͤΔΑ ͏ʹͳΔ • 2೥͝͠ͷϚʔδ
  17. default_scopeͷҋ • Ͳ͏΍ͬͨΒdefault_scopeͷόάमਖ਼ΛϚʔδ ͯ͠΋Β͑Δ͔૬ஊͯ͠Έͨ

  18. default_scopeͷҋ • default_scopeʹࢥ͍ೖΕͷ͋Δίϛολʔ͕ ͍ͳ͍ͷͰ࢖͏΂͖Ͱ͸ͳ͍

  19. • ࣮ࡍͣͬͱϚʔδ͞Εͳ͔ͬͨ default_scopeͷҋ ↑ݴٴͯ͠Δissue͕ผͷํ๏Ͱղܾ͞ΕͨͷͰηϧϑΫϩʔζ

  20. default_scopeͷҋ • ผͷϓϧϦͰ͸default_scopeʹݴٴ͠ͳ͍Α ͏ʹͨ͠ΒϚʔδ͞Εͨ

  21. default_scope͕֎ͤͳ͍ྫ • ͖ͬ͞ͷύον͕Ϛʔδ͞Εͯͳ͍ͷͰɺ

  22. default_scope͕֎ͤͳ͍ྫ • preloadͯ͠Δ͔Ͳ͏͔Ͱ݁Ռ͕มΘΔ

  23. Rails 5 ʹ΋࢒Δdefault_scopeͷόά • preload΍(join͠ͳ͍)includes͸ default_scope͕֎ͤͳ͍

  24. Rails 5 ʹ΋࢒Δdefault_scopeͷόά • Ͱ΋ࢿྉ࡞ͬͯͨΒ΍ͬͺ௚ͬͯཉ͘͠ͳͬ ͨͷͰࠓ೔ϓϧϦग़͠௚͠·ͨ͠

  25. Alias left_joins to left_outer_joins https://github.com/rails/rails/pull/22125

  26. #22125 Alias left_joins to left_outer_joins

  27. #22125 Alias left_joins to left_outer_joins • left_outer_joins ͷΤΠϦΞε left_joins Λ௥Ճ

    ͢Δ͚ͩ • ౰ͨΓલ͚ͩͲҒ͍ͷ͸๻ͷϓϧϦͰ͸ͳ͘ left_outer_joins Λ௥Ճ͢ΔํͷϓϧϦ • ·͋Ͱ΋left_outer_joinsͱ͔௕͗ͯ͢࢖Θͳ ͍ΑͶ
  28. ͦ΋ͦ΋ͳΜͰ left_outer_joins ͕ͦΜͳʹཉ͔͔ͬͨ͠ • ߜΓࠐΈͨΊͷLEFT OUTER JOINΛੜSQLΛ ࢖Θͣʹॻͨ͘ΊʹincludesΛ࢖͏ਓ͕͍Δ • eager

    loadͯ͠࢖͏Θ͚Ͱ΋ͳ͍Ϩίʔυͷ AR::BaseΠϯελϯε͕େྔʹ࡞ΒΕ஗͍ • ͦ͏͍͏༻్ͷϝιου͡Όͳ͍ͱϨϏϡʔ ͢ΔͱΠϯλʔωοτΛࠜڌʹ൓ൃ͞ΕΔ
  29. ͦ΋ͦ΋ͳΜͰ left_outer_joins ͕ͦΜͳʹཉ͔͔ͬͨ͠

  30. ͦ΋ͦ΋ͳΜͰ left_outer_joins ͕ͦΜͳʹཉ͔͔ͬͨ͠ • eager loading͕ෆཁͳΒRails 4ҎԼͰ͸ੜΫ ΤϦॻ͍ͨํ͕Ϛγͱࢥ͍ͬͯ·͢ • Rails

    5Ͱ΍ͬͱͦ͏͠ͳͯ͘Α͘ͳΔ
  31. ༨ஊ: ͦͷincludesຊ౰ʹ଎͘ ͳΔ͔֬ೝͯ͠Δʁ • N+1௵ͯ͠ΫΤϦ͚ͩ଎ͯ͘͠΋τʔλϧͰ଎͘ͳΔͱ͸ ݶΒͳ͍ • ಛʹhas_many associationΛeager loadingͯ͠AR::Base

    ͕ಉ࣌ʹେྔʹ࡞ΒΕΔ৔߹ • ʮincludes଍ͯ͠N+1ͳ͘͠·ͨ͠ʂʯΈ͍ͨͳϓϧϦɺ લޙͷ଎౓ࠩΛషͬͯ΄͍͠ • rails sͷϩάݟΔ͚ͩͰ΋Θ͔Δ
  32. ༨ஊ: ͦ΋ͦ΋Զ͸includesࣗମ ͕ݏ͍ͩ • ࢖͏ਓͷؾ͕࣋ͪશ͘Θ͔Βͳ͍ • includesΛݟ͚ͨͩͰ͸Ͳ͏͍͏ڍಈΛ͢Δͷ͔༧ଌͰ͖ͣ Մಡੑ͕௿͍ • JOIN͢Δͭ΋Γͳ͍ॴͰJOIN͞ΕͨΒΤϥʔʹͳͬͯཉ͍͠

    • includes(:foo).references(:foo)ͱ͔includes(:foo).joins(:foo) Έ͍ͨͳ৑௕ͳॻ͖ํ͕ؾʹͳΔ • eager_load(:foo)Ͱ͍͍͡ΌΜ
  33. ༨ஊ: ͦ΋ͦ΋Զ͸includesࣗମ ͕ݏ͍ͩ • Q: Recipe.includes(:steps).foo_scope.to_a ͸stepsͱJOIN͢Δ͔?

  34. ༨ஊ: ͦ΋ͦ΋Զ͸includesࣗମ ͕ݏ͍ͩ • Q: Recipe.includes(:steps).foo_scope.to_a ͸stepsͱJOIN͢Δ͔? • A: Θ͔Βͳ͍

    • foo_scopeͷఆٛʹΑΔ
  35. ·ͱΊ • Ruby 2.3 ͱ Rails 5.0 Λ࢖͏ͱerb͕଎͘ͳΔ • default_scope͸όάͬͯΔ͠ϝϯς͞Εͳ͍

    • includesͷ࢖͍ํΛݟ௚ͯ͠ཉ͍͠ • AR::Baseͷੜ੒ίετʹؾΛ͚ͭΑ͏