Slide 1

Slide 1 text

͔ΒֶͿ3BJMT৽ػೳ 3BJMTͷ3VCZରԠ ۜ࠲Rails#20(2020/4/17) sugamasao [email protected]

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

ΑΖ͘͠ ͓Ͷ͕͍͠·͢

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

ؓ࿩ٳ୊

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Webpacker

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

ओͳొ৔ਓ෺

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Ϗϧυ͞ΕͨΤϯτϦʔϑΝΠϧͷಡΈࠐΈ 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 %>

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

ෳ਺DB

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

ฒྻςετ

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

͓·͚

Slide 42

Slide 42 text

Rails 6.0 & Ruby 2.7

Slide 43

Slide 43 text

࢖͍ͬͯΔਓ ͍·͔͢ʁ

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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