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. ͔ΒֶͿ3BJMT৽ػೳ
    3BJMTͷ3VCZରԠ
    ۜ࠲Rails#20(2020/4/17)
    sugamasao
    [email protected]

    View full-size slide

  2. ͩΕʁ
    HN:sugamasao
    PN:͕͢ΘΒ ·͞ͷΓ
    Rubyྺ : 14೥͘Β͍
    Railsྺ : 10೥͘Β͍
    • WebΞϓϦέʔγϣϯϓϩάϥϚʔ
    • Rubyʹؔ͢Δॻ੶ͷࣥච׆ಈ

    View full-size slide

  3. ࣥචྺ
    2013
    2019
    2014
    2018
    2017 վగ൛
    ύʔϑΣΫτRuby

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  11. ओͳొ৔ਓ෺

    View full-size slide

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

    View full-size slide

  13. config/webpacker.yml
    /webpack/environment.js
    /development.js
    :
    ઃఆϑΝΠϧ܈
    /test.js
    •webpack.config.js ૬౰ͷymlϑΝΠϧ
    ग़ྗઌͷఆٛͳͲ͕ߦ͑Δ
    •production/testͳͲ؀ڥʹΑͬͯఆٛΛ
    ม͑ΒΕΔʢdatabase.ymlΈ͍ͨʹʣ

    View full-size slide

  14. config/webpacker.yml
    /webpack/environment.js
    /development.js
    :
    ઃఆϑΝΠϧ܈
    /test.js
    •webpackͷίϯύΠϧલʹࠩ͠ࠐΉॲཧ
    هड़͢Δ
    •loader/pluginͷ௥ՃͳͲ
    •ڞ௨ॲཧ͸environment.js΁

    View full-size slide

  15. // 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 ΑΓൈਮ

    View full-size slide

  16. // 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ͷ಺༰ΛಡΈࠐΜ͍͍ͩײ͡ͷΦϒδΣΫτ

    View full-size slide

  17. ઃఆͷσόοά೉͍͠໰୊
    • development.jsͳͲͰ
    `environment.toWebpackConfig()` Λ
    console.logͯ͠ΈΔͱ͔
    • σϑΥϧτͷ஋ΛݟͯΈΔͱ͔
    https://github.com/rails/webpacker/blob/master/package/environments/base.js
    `getBaseConfig` Ͱఆ͍ٛͯ͠Δ಺༰͕σϑΥϧτ஋

    View full-size slide

  18. app/javascript/packs/application.js
    public/packs/js/application-xxxx.js
    ίϯύΠϧͷྲྀΕ
    % ./bin/rails webpacker:compile
    packσΟϨΫτϦҎԼ͕
    ΤϯτϦʔϙΠϯτϑΝΠϧ
    Λஔ͘σΟϨΫτϦͱͳΔ

    View full-size slide

  19. Ϗϧυ͞ΕͨΤϯτϦʔϑΝΠϧͷಡΈࠐΈ



    SprocketsSample
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <%= stylesheet_link_tag 'application',
    media: ‘all',
    'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag ‘application',
    'data-turbolinks-track': 'reload' %>


    <%= yield %>


    View full-size slide

  20. Ϗϧυ͞ΕͨΤϯτϦʔϑΝΠϧͷಡΈࠐΈ
    • ϔϧύʔϝιου javascript_pack_tag
    Λ࢖ͬͯΤϯτϦʔϑΝΠϧΛಡΈࠐΉ
    • ͪͳΈʹɺWebpacker(webpack)Ͱը૾΋
    ѻ͏Α͏ʹͨ͠৔߹ɺimage_pack_tag΍
    asset_pack_pathͳͲͰύεΛऔಘͰ͖Δ
    ※͜ΕΒͷϔϧύʔϝιου͸Webpacker gem͕
    ఏڙ͢Δϝιου

    View full-size slide

  21. Webpacker
    • bin/webpack-dev-server΋͋ΔΑ
    • ڧ͍ͩ͜ΘΓ͕ͳ͘ʮϨʔϧʹ৐ͬͯΈΔʯ
    ঢ়ଶͰ։ൃ͢ΔͷͰ͋Ε͹εοͱ࢖͑ΔՄೳ
    ੑ͕ߴ͍
    • webpackΛ௚઀ѻ͏৔߹ͱൺ΂ͯɺಠࣗͷ
    ΍ΓํΛௐ΂Δඞཁ͕͋Δ෼΍΍͍͜͠ͱ͍
    ͏ͷ͸͋Γ·͕͢……
    ※ʢॳखͰ֎͢Έ͍ͨͳ෩ை΋͋Γ·͕͢ʣνʔϜ΍
    ΞϓϦͷ։ൃํ਑ʹΑͬͯར༻͢Δ͔Ͳ͏͔ܾΊ·͠ΐ͏

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  25. ෳ਺ͷ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

    View full-size slide

  26. ෳ਺ͷ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ʹ໊લΛ͚ͭͯෳ਺ఆٛͰ͖Δ

    View full-size slide

  27. ෳ਺ͷ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ʣ

    View full-size slide

  28. ෳ਺ͷ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ʹର࣮ͯ͠ߦͰ͖ΔΑ͏ʹͳΔ

    View full-size slide

  29. ෳ਺ͷ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

    View full-size slide

  30. ෳ਺ͷ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༻
    جఈΫϥεΛ࡞੒
    ͯ͠ܧঝ͢Δ

    View full-size slide

  31. ϦʔυϨϓϦΧΛࢀরͯ͠ෛՙ෼ࢄ
    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Λ͚ͭΔ

    View full-size slide

  32. ϦʔυϨϓϦΧΛࢀরͯ͠ෛՙ෼ࢄ
    class ApplicationRecord < ActiveRecord::Base
    self.abstract_class = true
    connects_to database: {
    writing: :primary,
    reading: :primary_replica
    }
    end
    ΞϓϦέʔγϣϯશମͰϦʔυϨϓϦΧΛ࢖͏ྫ

    View full-size slide

  33. ϦʔυϨϓϦΧΛࢀরͯ͠ෛՙ෼ࢄ
    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Λࢀর͢Δ

    View full-size slide

  34. 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

    View full-size slide

  35. ฒྻςετ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  38. Rails 6.0
    &
    Ruby 2.7

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  41. •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ϦϦʔεϊʔτΑΓൈਮ

    View full-size slide

  42. •config/boot.rbʹ `Warning[:deprecated] = false`
    Λ௥Ճ͢Δͱ͋Δఔ౓ܯࠂΛ཈੍͢Δ͜ͱ͸Ͱ͖Δ
    •͕ɺΞϓϦέʔγϣϯίʔυͷ·͍ͣ෦෼΋Ӆṭͯ͠
    ͠·͏͠ɺ͔ͤͬ͘ܯࠂΛग़͍ͯ͠Δҙຯ͕ͳ͘ͳͬ
    ͯ͠·͏
    •Rails ࣗ਎ʢͱؔ࿈͢Δgemʣͷൃ͢Δܯࠂ͕ऩ·ͬ
    ͯ͘ΕΕ͹༕͍ͳ͘Ruby 2.7Λ࢖͑ΔΜ͚ͩͲɺɺɺ

    View full-size slide

  43. https://twitter.com/kamipo/status/1214094495043637248
    ʢϢʔβʔ͸ؾܰʹʮରԠͯ͠΄͍͠ʯͳͲͱݴͬ
    ͯ͠·͏͕ʣେมŪůŠŪ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  48. ͕࣌ؒ༨ͬͨΒ࿩͢༻ϝϞ
    • Zeitwerkͷ `rails zeitwerk:check` ͕ಈ͔ͳ͍໰୊
    • ͜͜ͷissueͰ΍ΓͱΓ͕͋Δ https://github.com/rails/rails/
    issues/37966
    • masterͰ͸ͳ͓͍ͬͯΔͷͰɺमਖ਼൛λεΫΛDLͯ͠Ұ࣌తʹϩʔ
    ΧϧͷλεΫͱͯ͠ఆٛ͢Ε͹
    • insert_all/upsert_all͕஍ຯʹศར
    • ActiveRecord-ImportΛγϯϓϧʹ࢖͍ͬͯΕ͹Ҡߦ΋͠΍͍͢

    View full-size slide