提交 08a705b1 编写于 作者: X Xavier Noria

let environments configure load-related paths

Co-authored-by: NAllen Hsu <allen.hsusp@gmail.com>
上级 0f65f150
...@@ -482,8 +482,6 @@ What is described above are the defaults with a newly generated Rails app. There ...@@ -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). 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 The value of `autoload_paths` can be inspected. In a just-generated application
it is (edited): it is (edited):
......
...@@ -559,6 +559,12 @@ def load_seed ...@@ -559,6 +559,12 @@ def load_seed
end end
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| initializer :set_load_path, before: :bootstrap_hook do |app|
_all_load_paths(app.config.add_autoload_paths_to_load_path).reverse_each do |path| _all_load_paths(app.config.add_autoload_paths_to_load_path).reverse_each do |path|
$LOAD_PATH.unshift(path) if File.directory?(path) $LOAD_PATH.unshift(path) if File.directory?(path)
...@@ -607,12 +613,6 @@ def load_seed ...@@ -607,12 +613,6 @@ def load_seed
end end
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| initializer :prepend_helpers_path do |app|
if !isolated? || (app == self) if !isolated? || (app == self)
app.config.helpers_paths.unshift(*paths["app/helpers"].existent) app.config.helpers_paths.unshift(*paths["app/helpers"].existent)
......
...@@ -1751,6 +1751,31 @@ def index ...@@ -1751,6 +1751,31 @@ def index
assert_empty Rails.configuration.paths.load_paths - $LOAD_PATH assert_empty Rails.configuration.paths.load_paths - $LOAD_PATH
end 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 test "autoloading during initialization gets deprecation message and clearing if config.cache_classes is false" do
app_file "lib/c.rb", <<~EOS app_file "lib/c.rb", <<~EOS
class C class C
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册