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

Ruby on Rails 6.0 New Feature

961126d9892ae4df45249529a0721571?s=47 sugamasao
April 17, 2020

Ruby on Rails 6.0 New Feature

銀座Rails#20の登壇資料です
https://ginza-rails.connpass.com/event/171333/

961126d9892ae4df45249529a0721571?s=128

sugamasao

April 17, 2020
Tweet

Transcript

  1. ͔ΒֶͿ3BJMT৽ػೳ 3BJMTͷ3VCZରԠ ۜ࠲Rails#20(2020/4/17) sugamasao sugamasao@gmail.com

  2. ͩΕʁ HN:sugamasao PN:͕͢ΘΒ ·͞ͷΓ Rubyྺ : 14೥͘Β͍ Railsྺ : 10೥͘Β͍

    • WebΞϓϦέʔγϣϯϓϩάϥϚʔ • Rubyʹؔ͢Δॻ੶ͷࣥච׆ಈ
  3. ࣥචྺ 2013 2019 2014 2018 2017 վగ൛ ύʔϑΣΫτRuby

  4. ࣥචྺ 2013 2019 2014 2018 2017 վగ൛ ύʔϑΣΫτRuby ࠓ೔ͷ࿩

  5. ΑΖ͘͠ ͓Ͷ͕͍͠·͢

  6. ΋͏Rails 6ʹ ΞοϓάϨʔυ ͍ͯ͠Δਓʁ

  7. Ͱ͖͍ͯΔਓ ΤϥΠ!!!

  8. ؓ࿩ٳ୊

  9. Rails 6.0ͷ omakaseΛ͍ͭ͘ ͔঺հ͍͖ͯ͠·͢

  10. Railsͷػೳͷத Ͱ΋ਓʹΑͬͯҙ ݟ͕෼͔ΕΔ͘Μ

  11. Webpacker

  12. • webpackΛϥοϓͯ͠࢖͍΍͘͢ʢʁʣͯ͠ ͍Δgem • ΞϓϦέʔγϣϯ͔Β͸Sprockets࣌୅ͱࣅͨ Α͏ͳ࢖༻ײͰ࢖͑ΔΑ͏ʹͳ͍ͬͯΔ • JSΛಡΈࠐΉϦϯΫΛϔϧύʔͰग़ྗͨ͠Γ • ։ൃ࣌͸ߋ৽͕͋ΔͨͼʹίϯύΠϧͨ͠Γ

    Webpacker
  13. ओͳొ৔ਓ෺

  14. config/webpacker.yml /webpack/environment.js /development.js : ઃఆϑΝΠϧ܈ /test.js

  15. config/webpacker.yml /webpack/environment.js /development.js : ઃఆϑΝΠϧ܈ /test.js •webpack.config.js ૬౰ͷymlϑΝΠϧ ग़ྗઌͷఆٛͳͲ͕ߦ͑Δ •production/testͳͲ؀ڥʹΑͬͯఆٛΛ

    ม͑ΒΕΔʢdatabase.ymlΈ͍ͨʹʣ
  16. config/webpacker.yml /webpack/environment.js /development.js : ઃఆϑΝΠϧ܈ /test.js •webpackͷίϯύΠϧલʹࠩ͠ࠐΉॲཧ هड़͢Δ •loader/pluginͷ௥ՃͳͲ •ڞ௨ॲཧ͸environment.js΁

  17. // config/webpack/environment.js const { environment } = require('@rails/webpacker') const jsonLoader

    = { test: /\.json$/, use: 'json-loader' } // Insert json loader at the end of list environment.loaders.append('json', jsonLoader) environment.jsʹloaderΛ௥Ճ͢Δྫ https://github.com/rails/webpacker/blob/master/docs/webpack.md ΑΓൈਮ
  18. // config/webpack/environment.js const { environment } = require('@rails/webpacker') const jsonLoader

    = { test: /\.json$/, use: 'json-loader' } // Insert json loader at the end of list environment.loaders.append('json', jsonLoader) environment.jsʹloaderΛ௥Ճ͢Δྫ https://github.com/rails/webpacker/blob/master/docs/webpack.md ΑΓൈਮ webpacker.ymlͷ಺༰ΛಡΈࠐΜ͍͍ͩײ͡ͷΦϒδΣΫτ
  19. ઃఆͷσόοά೉͍͠໰୊ • development.jsͳͲͰ `environment.toWebpackConfig()` Λ console.logͯ͠ΈΔͱ͔ • σϑΥϧτͷ஋ΛݟͯΈΔͱ͔ https://github.com/rails/webpacker/blob/master/package/environments/base.js `getBaseConfig`

    Ͱఆ͍ٛͯ͠Δ಺༰͕σϑΥϧτ஋
  20. app/javascript/packs/application.js public/packs/js/application-xxxx.js ίϯύΠϧͷྲྀΕ % ./bin/rails webpacker:compile packσΟϨΫτϦҎԼ͕ ΤϯτϦʔϙΠϯτϑΝΠϧ Λஔ͘σΟϨΫτϦͱͳΔ

  21. Ϗϧυ͞ΕͨΤϯτϦʔϑΝΠϧͷಡΈࠐΈ <!DOCTYPE html> <html> <head> <title>SprocketsSample</title> <%= csrf_meta_tags %> <%=

    csp_meta_tag %> <%= stylesheet_link_tag 'application', media: ‘all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag ‘application', 'data-turbolinks-track': 'reload' %> </head> <body> <%= yield %> </body> </html>
  22. Ϗϧυ͞ΕͨΤϯτϦʔϑΝΠϧͷಡΈࠐΈ • ϔϧύʔϝιου javascript_pack_tag Λ࢖ͬͯΤϯτϦʔϑΝΠϧΛಡΈࠐΉ • ͪͳΈʹɺWebpacker(webpack)Ͱը૾΋ ѻ͏Α͏ʹͨ͠৔߹ɺimage_pack_tag΍ asset_pack_pathͳͲͰύεΛऔಘͰ͖Δ ※͜ΕΒͷϔϧύʔϝιου͸Webpacker

    gem͕ ఏڙ͢Δϝιου
  23. Webpacker • bin/webpack-dev-server΋͋ΔΑ • ڧ͍ͩ͜ΘΓ͕ͳ͘ʮϨʔϧʹ৐ͬͯΈΔʯ ঢ়ଶͰ։ൃ͢ΔͷͰ͋Ε͹εοͱ࢖͑ΔՄೳ ੑ͕ߴ͍ • webpackΛ௚઀ѻ͏৔߹ͱൺ΂ͯɺಠࣗͷ ΍ΓํΛௐ΂Δඞཁ͕͋Δ෼΍΍͍͜͠ͱ͍

    ͏ͷ͸͋Γ·͕͢…… ※ʢॳखͰ֎͢Έ͍ͨͳ෩ை΋͋Γ·͕͢ʣνʔϜ΍ ΞϓϦͷ։ൃํ਑ʹΑͬͯར༻͢Δ͔Ͳ͏͔ܾΊ·͠ΐ͏
  24. ෳ਺DB

  25. ࢖Θͳ͍ਓ͸શવ࢖ Θͳ͍͠ɺ࢖͏ਓ͸ ผͷgemΛ࢖ͬͯ ͍ΔͷͰ͸͘Μ ෳ਺DB

  26. Rails 6.0Ͱͷ ʮෳ਺DBʯ͸େ͖ ͘ೋͭͷػೳ͕͋Δ ෳ਺DB

  27. •ෳ਺ͷDBʹରͯ͠Read/ Write͢Δʢѻ͏DBΛ੾ Γସ͑ΒΕΔʣ • ϦʔυϨϓϦΧΛࢀর͠ ͯෛՙ෼ࢄ ෳ਺DB

  28. ෳ਺ͷDBʹରͯ͠Read/Write͢Δʢѻ͏ DBΛ੾Γସ͑ΒΕΔʣ production: database: my_primary_database user: root adapter: mysql production:

    primary: database: my_primary_database user: root adapter: mysql animals: database: my_animals_database user: animals_root adapter: mysql migrations_paths: db/animals_migrate condig/database.yml
  29. ෳ਺ͷDBʹରͯ͠Read/Write͢Δʢѻ͏ DBΛ੾Γସ͑ΒΕΔʣ production: database: my_primary_database user: root adapter: mysql production:

    primary: database: my_primary_database user: root adapter: mysql animals: database: my_animals_database user: animals_root adapter: mysql migrations_paths: db/animals_migrate condig/database.yml ઀ଓ͢ΔDBʹ໊લΛ͚ͭͯෳ਺ఆٛͰ͖Δ
  30. ෳ਺ͷDBʹରͯ͠Read/Write͢Δʢѻ͏ DBΛ੾Γସ͑ΒΕΔʣ production: database: my_primary_database user: root adapter: mysql production:

    primary: database: my_primary_database user: root adapter: mysql animals: database: my_animals_database user: animals_root adapter: mysql migrations_paths: db/animals_migrate condig/database.yml ϚΠάϨʔγϣϯϑΝΠϧΛผʑͰ؅ཧ͢Δ ʢলུͨ͠৔߹͸͍ͭ΋ͷdb/migrateʣ
  31. ෳ਺ͷDBʹରͯ͠Read/Write͢Δʢѻ͏ DBΛ੾Γସ͑ΒΕΔʣ % rails -T : rails db:migrate:animals # Migrate

    animals database for current environment rails db:migrate:primary # Migrate primary database for current environment rails db:migrate:status # Display status of migrations rails db:migrate:status:animals # Display status of migrations for animals database rails db:migrate:status:primary # Display status of migrations for primary database db:createͷDBૢ࡞΍ϚΠάϨʔγϣϯؔ࿈ͷλεΫ΋ ͦΕͧΕͷDBʹର࣮ͯ͠ߦͰ͖ΔΑ͏ʹͳΔ
  32. ෳ਺ͷDBʹରͯ͠Read/Write͢Δʢѻ͏ DBΛ੾Γସ͑ΒΕΔʣ # app/models/animal_base.rb class AnimalBase < ApplicationRecord self.abstract_class =

    true establish_connection :animals end # app/models/dog.rb class Dog < AnimalBase end
  33. ෳ਺ͷDBʹରͯ͠Read/Write͢Δʢѻ͏ DBΛ੾Γସ͑ΒΕΔʣ # app/models/animal_base.rb class AnimalBase < ApplicationRecord self.abstract_class =

    true establish_connection :animals end # app/models/dog.rb class Dog < AnimalBase end ઀ଓ͍ͨ͠DB༻ جఈΫϥεΛ࡞੒ ͯ͠ܧঝ͢Δ
  34. ϦʔυϨϓϦΧΛࢀরͯ͠ෛՙ෼ࢄ production: database: my_primary_database user: root adapter: mysql production: primary:

    database: my_primary_database user: root adapter: mysql primary_replica: database: my_primary_database user: root_readonly adapter: mysql replica: true condig/database.yml ࢀর༻DB͸replica: trueΛ͚ͭΔ
  35. ϦʔυϨϓϦΧΛࢀরͯ͠ෛՙ෼ࢄ class ApplicationRecord < ActiveRecord::Base self.abstract_class = true connects_to database:

    { writing: :primary, reading: :primary_replica } end ΞϓϦέʔγϣϯશମͰϦʔυϨϓϦΧΛ࢖͏ྫ
  36. ϦʔυϨϓϦΧΛࢀরͯ͠ෛՙ෼ࢄ config.active_record.database_selector = { delay: 2.seconds } config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver

    config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session ੾Γସ͑ઃఆΛ༗ޮʹ͢Δ config/environments/production.rb ͜ͷઃఆΛ༗ޮʹ͢Δͱ…… •GET΍HEADϦΫΤετͷ࣌͸readଆͷDBΛࢀর͢Δ •ಉҰϢʔβʔ͕ߋ৽ -> ࢀরΛͨ͠৔߹ delay ඵҎ಺ͷ ΞΫηεͰ͋Ε͹writeଆͷDBΛࢀর͢Δ
  37. Rails 6.0Ͱ͸DBΛ·͕ͨΔ join͕Ͱ͖ͳ͍ Rails 6.1Ͱղܾ͢Δ͔΋ʁ (RailsΨΠυʹΑΔͱɺαϙʔτ༧ఆͱهࡌ͞Ε͍ͯΔ) ෳ਺DB https://railsguides.jp/active_record_multiple_databases.html#%E6%B3%A8%E6%84%8F%E7%82%B9

  38. ฒྻςετ

  39. ฒྻςετ minitestϢʔβʔͷօ͞Μ͓ΊͰͱ͏͍͟͝·͢ class ActiveSupport::TestCase parallelize(workers: :number_of_processors) fixtures :all end test/test_helper.rbΑΓൈਮ

    ಛʹҙࣝ͢Δࣄͳ͘ɺউखʹฒྻςετͯ͘͠Ε·͢
  40. ฒྻςετ minitestϢʔβʔͷօ͞Μ͓ΊͰͱ͏͍͟͝·͢ class ActiveSupport::TestCase parallelize(workers: :number_of_processors) fixtures :all end test/test_helper.rbΑΓൈਮ

    ಛʹҙࣝ͢Δࣄͳ͘ɺউखʹฒྻςετͯ͘͠Ε·͢ workersʹฒྻ਺Λࢦఆ͢ΔࣄͰฒྻςετΛ࣮ߦ͢Δ
  41. ͓·͚

  42. Rails 6.0 & Ruby 2.7

  43. ࢖͍ͬͯΔਓ ͍·͔͢ʁ

  44. Rails 6.0&Ruby 2.7 ͰΩʔϫʔυ Ҿ਺ʹؔ͢Δܯࠂ͕ ΊͬͪΌग़Δ໰୊

  45. •Ruby 3.0ʹ޲͚ͨ४උͱͯ͠ͷܯࠂ •ΩʔϫʔυҾ਺ʹରͯ͠hashΦϒδΣΫτΛ౉͢͜ͱ΍ɺϝιο υͷҾ਺ʹ͓͍ͯϋογϡͱΩʔϫʔυҾ਺ͷղऍ͕ᐆດʹͳΔ ΋ͷʹରͯ͠ܯࠂ͕ग़Δ •େମͷ৔߹͸ `method(**hash)` ͷΑ͏ʹdouble splatԋࢉࢠͰ ల։ͯ͠΍Ε͹ྑ͍ʢͰࡁΉࣄ͕ଟ͍ʣ

    def foo(key: 42); end; foo({key: 42}) # warned def foo(**kw); end; foo({key: 42}) # warned def foo(key: 42); end; foo(**{key: 42}) # OK def foo(**kw); end; foo(**{key: 42}) # OK https://www.ruby-lang.org/ja/news/2019/12/25/ruby-2-7-0-released/ Ruby 2.7ϦϦʔεϊʔτΑΓൈਮ
  46. •config/boot.rbʹ `Warning[:deprecated] = false` Λ௥Ճ͢Δͱ͋Δఔ౓ܯࠂΛ཈੍͢Δ͜ͱ͸Ͱ͖Δ •͕ɺΞϓϦέʔγϣϯίʔυͷ·͍ͣ෦෼΋Ӆṭͯ͠ ͠·͏͠ɺ͔ͤͬ͘ܯࠂΛग़͍ͯ͠Δҙຯ͕ͳ͘ͳͬ ͯ͠·͏ •Rails ࣗ਎ʢͱؔ࿈͢Δgemʣͷൃ͢Δܯࠂ͕ऩ·ͬ

    ͯ͘ΕΕ͹༕͍ͳ͘Ruby 2.7Λ࢖͑ΔΜ͚ͩͲɺɺɺ
  47. https://twitter.com/kamipo/status/1214094495043637248 ʢϢʔβʔ͸ؾܰʹʮରԠͯ͠΄͍͠ʯͳͲͱݴͬ ͯ͠·͏͕ʣେมŪůŠŪ

  48. Railsຊମ΍ؔ࿈gemʹRuby 2.7ରԠΛ ೖΕ͍ͯͬͯ͘Ε͍ͯΔkamipo͞Μ

  49. https://blog.kamipo.net/entry/2020/02/26/193904

  50. ࠓճ͸kamipo͞ΜΛҾ͖߹͍ ʹग़͠·͕ͨ͠ɺ͞·͟·ਓ͕ Ruby 2.7ʹ޲͚ͨमਖ਼Λߦͳͬ ͍ͯ·͢ ѹ౗తײँ

  51. ࣍ͷόʔδϣϯͷRails ָ͕͠ΈͰ͢Ͷʂ

  52. ͕࣌ؒ༨ͬͨΒ࿩͢༻ϝϞ • Zeitwerkͷ `rails zeitwerk:check` ͕ಈ͔ͳ͍໰୊ • ͜͜ͷissueͰ΍ΓͱΓ͕͋Δ https://github.com/rails/rails/ issues/37966

    • masterͰ͸ͳ͓͍ͬͯΔͷͰɺमਖ਼൛λεΫΛDLͯ͠Ұ࣌తʹϩʔ ΧϧͷλεΫͱͯ͠ఆٛ͢Ε͹ • insert_all/upsert_all͕஍ຯʹศར • ActiveRecord-ImportΛγϯϓϧʹ࢖͍ͬͯΕ͹Ҡߦ΋͠΍͍͢