Allow autoloader inflectors to be swaped out

This commit is contained in:
Jean Boussier 2019-03-08 16:48:52 +01:00
parent 199de6bee2
commit 6d624f2de9
4 changed files with 13 additions and 6 deletions

View File

@ -55,10 +55,6 @@ module ActiveSupport
private
def setup_autoloaders
Rails.autoloaders.each do |autoloader|
autoloader.inflector = Inflector
end
Dependencies.autoload_paths.each do |autoload_path|
# Zeitwerk only accepts existing directories in `push_dir` to
# prevent misconfigurations.

View File

@ -6,6 +6,7 @@ require "pathname"
require "active_support"
require "active_support/dependencies/autoload"
require "active_support/dependencies/zeitwerk_integration"
require "active_support/core_ext/kernel/reporting"
require "active_support/core_ext/module/delegation"
require "active_support/core_ext/array/extract_options"

View File

@ -7,13 +7,19 @@ module Rails
def main
if zeitwerk_enabled?
@main ||= Zeitwerk::Loader.new.tap { |loader| loader.tag = "rails.main" }
@main ||= Zeitwerk::Loader.new.tap do |loader|
loader.tag = "rails.main"
loader.inflector = ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector
end
end
end
def once
if zeitwerk_enabled?
@once ||= Zeitwerk::Loader.new.tap { |loader| loader.tag = "rails.once" }
@once ||= Zeitwerk::Loader.new.tap do |loader|
loader.tag = "rails.once"
loader.inflector = ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector
end
end
end

View File

@ -1167,6 +1167,8 @@ module ApplicationTests
assert_instance_of Zeitwerk::Loader, Rails.autoloaders.once
assert_equal "rails.once", Rails.autoloaders.once.tag
assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a
assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.main.inflector
assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.once.inflector
config.autoloader = :classic
assert_not Rails.autoloaders.zeitwerk_enabled?
@ -1181,6 +1183,8 @@ module ApplicationTests
assert_instance_of Zeitwerk::Loader, Rails.autoloaders.once
assert_equal "rails.once", Rails.autoloaders.once.tag
assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a
assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.main.inflector
assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.once.inflector
assert_raises(ArgumentError) { config.autoloader = :unknown }
end