Skip to content

Commit 08a705b

Browse files
fxnAllen Hsu
andcommitted
let environments configure load-related paths
Co-authored-by: Allen Hsu <[email protected]>
1 parent 0f65f15 commit 08a705b

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

guides/source/autoloading_and_reloading_constants_classic_mode.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,6 @@ What is described above are the defaults with a newly generated Rails app. There
482482

483483
See also [Autoloading in the Test Environment](#autoloading-in-the-test-environment).
484484

485-
`config.autoload_paths` is not changeable from environment-specific configuration files.
486-
487485
The value of `autoload_paths` can be inspected. In a just-generated application
488486
it is (edited):
489487

railties/lib/rails/engine.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,12 @@ def load_seed
559559
end
560560
end
561561

562+
initializer :load_environment_config, before: :load_environment_hook, group: :all do
563+
paths["config/environments"].existent.each do |environment|
564+
require environment
565+
end
566+
end
567+
562568
initializer :set_load_path, before: :bootstrap_hook do |app|
563569
_all_load_paths(app.config.add_autoload_paths_to_load_path).reverse_each do |path|
564570
$LOAD_PATH.unshift(path) if File.directory?(path)
@@ -607,12 +613,6 @@ def load_seed
607613
end
608614
end
609615

610-
initializer :load_environment_config, before: :load_environment_hook, group: :all do
611-
paths["config/environments"].existent.each do |environment|
612-
require environment
613-
end
614-
end
615-
616616
initializer :prepend_helpers_path do |app|
617617
if !isolated? || (app == self)
618618
app.config.helpers_paths.unshift(*paths["app/helpers"].existent)

railties/test/application/configuration_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,31 @@ def index
17511751
assert_empty Rails.configuration.paths.load_paths - $LOAD_PATH
17521752
end
17531753

1754+
test "autoload paths can be set in the config file of the environment" do
1755+
app_dir "custom_autoload_path"
1756+
app_dir "custom_autoload_once_path"
1757+
app_dir "custom_eager_load_path"
1758+
1759+
restore_default_config
1760+
add_to_env_config "development", <<-RUBY
1761+
config.autoload_paths << "#{app_path}/custom_autoload_path"
1762+
config.autoload_once_paths << "#{app_path}/custom_autoload_once_path"
1763+
config.eager_load_paths << "#{app_path}/custom_eager_load_path"
1764+
RUBY
1765+
1766+
app "development"
1767+
1768+
Rails.application.config.tap do |config|
1769+
assert_includes config.autoload_paths, "#{app_path}/custom_autoload_path"
1770+
assert_includes config.autoload_once_paths, "#{app_path}/custom_autoload_once_path"
1771+
assert_includes config.eager_load_paths, "#{app_path}/custom_eager_load_path"
1772+
end
1773+
1774+
assert_includes $LOAD_PATH, "#{app_path}/custom_autoload_path"
1775+
assert_includes $LOAD_PATH, "#{app_path}/custom_autoload_once_path"
1776+
assert_includes $LOAD_PATH, "#{app_path}/custom_eager_load_path"
1777+
end
1778+
17541779
test "autoloading during initialization gets deprecation message and clearing if config.cache_classes is false" do
17551780
app_file "lib/c.rb", <<~EOS
17561781
class C

0 commit comments

Comments
 (0)