Slide 1

Slide 1 text

Dive to Ruby on Rails Autoloading @teitei_tk Meguro.rb#20

Slide 2

Slide 2 text

͓લ͸୭ͳΜͩɻ • @teitei_tkͱ໊৐͍ͬͯ·͢ɻ • ΞΠίϯ͸ӈͰ΍ͬͯ·͢ɻ • झຯ͸ਓੜͱRailsͱ TypeScriptͰ͢ɻ • freeeͱ͍͏ձࣾͰಇ͍͍ͯ· ͢ɻ

Slide 3

Slide 3 text

ࠓ೔࿩͢͜ͱ

Slide 4

Slide 4 text

Railsͷautoload_pathsͱ eager_load_pathsͷҧ͍ʹ͍ͭ ͯ

Slide 5

Slide 5 text

autoload_pathsͱ͸ • ޠฐΛڪΕͣʹ͍͏ͱɺRails্Ͱ module΍ఆ਺Λ͍͍ײ͡ʹࣗಈͰಡ ΈࠐΜͰ͘ΕΔ࢓૊ΈͰ͢ɻ

Slide 6

Slide 6 text

eg. Ruby on Rails class Hoge < ApplicationRecord end

Slide 7

Slide 7 text

eg. PureRubyͷ৔߹ require 'application_record' class Hoge < ApplicationRecord end

Slide 8

Slide 8 text

• Pure RubyͰ͸ application_record.rb ΛrequireʹͯಡΈࠐ Ήඞཁ͕͋Δɻ • RailsͰ͸ಛఆͷσΟϨΫτϦ഑ԼͷϑΝΠϧ͸ࣗલͰಡ Έࠐ·ͣͱ΋͍͍ײ͡ʹࣗಈͰಡΈࠐΜͰ͘ΕΔ࢓૊Έ͕ ͋Δ • ͦΕ͕autoload_pathsͰ͢ɻ

Slide 9

Slide 9 text

ಛఆͷσΟϨΫτϦ is teitei.tk >> !(master) ~/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading $ bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths' Running via Spring preloader in process 39294 /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/assets /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/channels /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/controllers /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/controllers/concerns /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/helpers /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/jobs /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/mailers /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/models /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/app/models/concerns /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/ activestorage-5.2.1/app/assets /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/ activestorage-5.2.1/app/controllers /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/ activestorage-5.2.1/app/controllers/concerns /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/ activestorage-5.2.1/app/javascript /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/ activestorage-5.2.1/app/jobs /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/vendor/bundle/ruby/2.5.0/gems/ activestorage-5.2.1/app/models /Users/teitei.tk/.golang/src/github.com/teitei-tk/dive-to-rails-autoloading/test/mailers/previews

Slide 10

Slide 10 text

• puts ActiveSupport::Dependencies.autoload_paths Ͱ࣮ ࡍʹautoload_paths͕దԠ͞ΕΔ஋ΛݟΔ͜ͱ͕Ͱ͖· ͢ɻ • config/application.rbʹͯconfig.autoload_pathsʹͯ௥Ճ Λ͢Δͱɺ൓ө͞Ε·͢ɻ

Slide 11

Slide 11 text

• ৄ͘͠͸ https://railsguides.jp/ autoloading_and_reloading_constants.html#autoload- paths

Slide 12

Slide 12 text

Rails5͔Βͷڍಈͷมߋ • Rails5͔Βautoload_pathsʹ௥Ճͨ͠ϑΝΠϧ͸ RAILS_ENV=production Ͱ͸ಡΈࠐ·ͳ͍Α͏ʹͳͬͯ ·͢ɻ • https://edgeguides.rubyonrails.org/ upgrading_ruby_on_rails.html#autoloading-is-disabled- after-booting-in-the-production-environment • ୅ΘΓʹ࢖͓͏ͱݴΘΕ͍ͯΔͷ͕eager_load_pathsͰ ͢ɻ

Slide 13

Slide 13 text

eager_load_pathsͱ͸ • config.eager_load_pathsʹ௥Ճ͞Ε͍ͯΔσΟϨΫτϦͷ ϑΝΠϧΛࣄલʹҰׅͰಡΈࠐΉ࢓૊ΈͰ͢ɻ

Slide 14

Slide 14 text

Rails::Engine.eager_load! # Eager load the application by loading all ruby # files inside eager_load paths. def eager_load! config.eager_load_paths.each do |load_path| matcher = /\A#{Regexp.escape(load_path.to_s)}\/(.*)\.rb\Z/ Dir.glob("#{load_path}/**/*.rb").sort.each do |file| require_dependency file.sub(matcher, '\1') end end end

Slide 15

Slide 15 text

• config/environment.rbʹͯRails.application.initialize!Λಡ ΜͰ͍Δͱࢥ͍·͢ɻ͔ͦ͜ΒॳظԽͷྲྀΕΛಡΈऔΔ͜ ͱ͕Ͱ͖·͢ɻ • Rails::Application.initialize!-> Rails::Initializable.run_initializers -> Rails::Application.initializers -> Rails::Application::Finisher.initializers_for ->
 Rails::Engine.eager_load!

Slide 16

Slide 16 text

᷿ͰΑ͘ݴΘΕΔࣄ • Rails5Ͱ͸autoload_pathsͰ͸ͳ͘ɺeager_load_pathsʹ ม͑Ε͹ok

Slide 17

Slide 17 text

ͳ͔ͥ • ౴͑͸Rails::Engine.initializer set_load_path ʹ͋Γ·͢ɻ # Add configured load paths to Ruby's load path, and remove duplicate entries. initializer :set_load_path, before: :bootstrap_hook do _all_load_paths.reverse_each do |path| $LOAD_PATH.unshift(path) if File.directory?(path) end $LOAD_PATH.uniq! end

Slide 18

Slide 18 text

• Rails::Initializable Moduleʹఆٛ͞Ε͍ͯΔ $LOAD_PATHSʹunshiftͰ௥Ճ͍ͯ͠·͢ɻ

Slide 19

Slide 19 text

_all_load_pathsͱ͸ • Rails::Initializable Moduleʹఆٛ͞Ε͍ͯΔ $LOAD_PATHSʹunshiftͰ௥Ճ͍ͯ͠·͢ɻ • _all_load_pathsͷ࣮ଶ͸config.autoload_paths + config.eager_load_paths + config.autoload_once_pathsΛ ߹Θͤͨ෺Ͱͨ͠ɻ

Slide 20

Slide 20 text

• RAILS_ENV=production Ͱ͸ eager_load_pathsͷ࢓૊Έ Λར༻͠ɺRAILS_ENV != production Ҏ֎Ͱ͸ autoload_pathsͷ࢓૊ΈΛར༻͍ͯ͠Δɻ

Slide 21

Slide 21 text

• ৄ͘͠͸Blogʹ·ͱΊ·ͨ͠ɻ • https://note.mu/teitei_tk/n/ne5ae711719b1

Slide 22

Slide 22 text

ॴײ

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content