Sha256: 5e483c7cc4aed1ccbd6ec16dd5c9dcdf2c198e5bb890fe5e308fd83360049535

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module Chanko
  class Railtie < Rails::Railtie
    initializer "chanko.include", before: :eager_load! do |app|
      ActiveSupport.on_load :action_view do
        ::ActionView::Base.send(:include, Helper, Invoker, UnitProxyProvider)
      end
      ActiveSupport.on_load :action_controller do
        ::ActionController::Base.send(:include, Controller, Invoker, UnitProxyProvider)
      end
      ActiveSupport.on_load :active_record do
        ::ActiveRecord::Base.send(:include, UnitProxyProvider)
        ::ActiveRecord::Relation.send(:include, UnitProxyProvider)
        ::ActiveRecord::Associations::CollectionAssociation.send(:include, UnitProxyProvider)
      end
    end


    initializer("chanko.zeitwerk.prepare_eager_load", before: :set_autoload_paths) do |app|
      # zeitwerk freezes autoload_paths after :set_autoload_paths.
      # So we need to prepare before set_autoload_paths
      Chanko::Loader.prepare_eager_load(mode: :zeitwerk)
    end

    initializer("chanko.classic.prepare_eager_load", after: :load_environment_config) do |app|
      # Rails5 doens't load environments/*.rb files before :set_autoload_paths.
      # In other words, at this stage, config.eager_load cannot be determined to be true or false.
      # But classic loader does not freeze paths on :set_autoload_paths.
      # After all, It's ok if it is executed after :set_autoload_paths on Rails5 and Rails6(classic).
      Chanko::Loader.prepare_eager_load(mode: :classic)
    end

    initializer("chanko.eager_load_units", before: :eager_load!) do |app|
      # This is why we need handmade eager-loading
      # https://github.com/cookpad/chanko/pull/38
      Chanko::Loader.eager_load_units!
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chanko-2.3.0 lib/chanko/railtie.rb