dive to Ruby on Rails module autoloading

F3ba76698a3a2ea827abd7b73d74a5b7?s=47 teitei_tk
October 29, 2018

dive to Ruby on Rails module autoloading

F3ba76698a3a2ea827abd7b73d74a5b7?s=128

teitei_tk

October 29, 2018
Tweet

Transcript

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

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

    ͢ɻ
  3. ࠓ೔࿩͢͜ͱ

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

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

  6. eg. Ruby on Rails class Hoge < ApplicationRecord end

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

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

    • ͦΕ͕autoload_pathsͰ͢ɻ
  9. ಛఆͷσΟϨΫτϦ 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
  10. • puts ActiveSupport::Dependencies.autoload_paths Ͱ࣮ ࡍʹautoload_paths͕దԠ͞ΕΔ஋ΛݟΔ͜ͱ͕Ͱ͖· ͢ɻ • config/application.rbʹͯconfig.autoload_pathsʹͯ௥Ճ Λ͢Δͱɺ൓ө͞Ε·͢ɻ

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

  12. 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Ͱ ͢ɻ
  13. eager_load_pathsͱ͸ • config.eager_load_pathsʹ௥Ճ͞Ε͍ͯΔσΟϨΫτϦͷ ϑΝΠϧΛࣄલʹҰׅͰಡΈࠐΉ࢓૊ΈͰ͢ɻ

  14. 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
  15. • 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!
  16. ᷿ͰΑ͘ݴΘΕΔࣄ • Rails5Ͱ͸autoload_pathsͰ͸ͳ͘ɺeager_load_pathsʹ ม͑Ε͹ok

  17. ͳ͔ͥ • ౴͑͸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
  18. • Rails::Initializable Moduleʹఆٛ͞Ε͍ͯΔ $LOAD_PATHSʹunshiftͰ௥Ճ͍ͯ͠·͢ɻ

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

    config.autoload_once_pathsΛ ߹Θͤͨ෺Ͱͨ͠ɻ
  20. • RAILS_ENV=production Ͱ͸ eager_load_pathsͷ࢓૊Έ Λར༻͠ɺRAILS_ENV != production Ҏ֎Ͱ͸ autoload_pathsͷ࢓૊ΈΛར༻͍ͯ͠Δɻ

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

  22. ॴײ

  23. None
  24. None