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

Ruby on Rails 6.0 New Feature

Ruby on Rails 6.0 New Feature

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

sugamasao

April 17, 2020
Tweet

More Decks by sugamasao

Other Decks in Technology

Transcript

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

    • WebΞϓϦέʔγϣϯϓϩάϥϚʔ • Rubyʹؔ͢Δॻ੶ͷࣥච׆ಈ
  2. // 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 ΑΓൈਮ
  3. // 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ͷ಺༰ΛಡΈࠐΜ͍͍ͩײ͡ͷΦϒδΣΫτ
  4. Ϗϧυ͞ΕͨΤϯτϦʔϑΝΠϧͷಡΈࠐΈ <!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>
  5. ෳ਺ͷ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
  6. ෳ਺ͷ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ʹ໊લΛ͚ͭͯෳ਺ఆٛͰ͖Δ
  7. ෳ਺ͷ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ʣ
  8. ෳ਺ͷ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ʹର࣮ͯ͠ߦͰ͖ΔΑ͏ʹͳΔ
  9. ෳ਺ͷ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༻ جఈΫϥεΛ࡞੒ ͯ͠ܧঝ͢Δ
  10. ϦʔυϨϓϦΧΛࢀরͯ͠ෛՙ෼ࢄ 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Λ͚ͭΔ
  11. ϦʔυϨϓϦΧΛࢀরͯ͠ෛՙ෼ࢄ class ApplicationRecord < ActiveRecord::Base self.abstract_class = true connects_to database:

    { writing: :primary, reading: :primary_replica } end ΞϓϦέʔγϣϯશମͰϦʔυϨϓϦΧΛ࢖͏ྫ
  12. ϦʔυϨϓϦΧΛࢀরͯ͠ෛՙ෼ࢄ 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Λࢀর͢Δ
  13. •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ϦϦʔεϊʔτΑΓൈਮ
  14. ͕࣌ؒ༨ͬͨΒ࿩͢༻ϝϞ • Zeitwerkͷ `rails zeitwerk:check` ͕ಈ͔ͳ͍໰୊ • ͜͜ͷissueͰ΍ΓͱΓ͕͋Δ https://github.com/rails/rails/ issues/37966

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