let environments configure load-related paths

Co-authored-by: Allen Hsu <allen.hsusp@gmail.com>
This commit is contained in:
Xavier Noria 2019-11-02 11:23:33 +01:00
parent 14d958ec71
commit 43863bfc57
3 changed files with 31 additions and 8 deletions

View File

@ -482,8 +482,6 @@ What is described above are the defaults with a newly generated Rails app. There
See also [Autoloading in the Test Environment](#autoloading-in-the-test-environment).
`config.autoload_paths` is not changeable from environment-specific configuration files.
The value of `autoload_paths` can be inspected. In a just-generated application
it is (edited):

View File

@ -560,6 +560,12 @@ module Rails
end
end
initializer :load_environment_config, before: :load_environment_hook, group: :all do
paths["config/environments"].existent.each do |environment|
require environment
end
end
initializer :set_load_path, before: :bootstrap_hook do |app|
_all_load_paths(app.config.add_autoload_paths_to_load_path).reverse_each do |path|
$LOAD_PATH.unshift(path) if File.directory?(path)
@ -608,12 +614,6 @@ module Rails
end
end
initializer :load_environment_config, before: :load_environment_hook, group: :all do
paths["config/environments"].existent.each do |environment|
require environment
end
end
initializer :prepend_helpers_path do |app|
if !isolated? || (app == self)
app.config.helpers_paths.unshift(*paths["app/helpers"].existent)

View File

@ -1751,6 +1751,31 @@ module ApplicationTests
assert_empty Rails.configuration.paths.load_paths - $LOAD_PATH
end
test "autoload paths can be set in the config file of the environment" do
app_dir "custom_autoload_path"
app_dir "custom_autoload_once_path"
app_dir "custom_eager_load_path"
restore_default_config
add_to_env_config "development", <<-RUBY
config.autoload_paths << "#{app_path}/custom_autoload_path"
config.autoload_once_paths << "#{app_path}/custom_autoload_once_path"
config.eager_load_paths << "#{app_path}/custom_eager_load_path"
RUBY
app "development"
Rails.application.config.tap do |config|
assert_includes config.autoload_paths, "#{app_path}/custom_autoload_path"
assert_includes config.autoload_once_paths, "#{app_path}/custom_autoload_once_path"
assert_includes config.eager_load_paths, "#{app_path}/custom_eager_load_path"
end
assert_includes $LOAD_PATH, "#{app_path}/custom_autoload_path"
assert_includes $LOAD_PATH, "#{app_path}/custom_autoload_once_path"
assert_includes $LOAD_PATH, "#{app_path}/custom_eager_load_path"
end
test "autoloading during initialization gets deprecation message and clearing if config.cache_classes is false" do
app_file "lib/c.rb", <<~EOS
class C