Skip to content

Commit 351816f

Browse files
committed
Ensure that eager_load actually takes place just after the middleware stack is built by using another pattern.
Also create a engine_blank_point initializer to ensure any :before or :after hooks defined inside engines won't move the configuration initializers to other places.
1 parent 6617d01 commit 351816f

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

railties/lib/rails/application/finisher.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ module Finisher
3535
app
3636
end
3737

38-
initializer :finisher_hook do |app|
39-
ActiveSupport.run_load_hooks(:after_initialize, app)
38+
initializer :eager_load! do
39+
if config.cache_classes && !$rails_rake_task
40+
railties.all(&:eager_load!)
41+
end
42+
end
43+
44+
initializer :finisher_hook do
45+
ActiveSupport.run_load_hooks(:after_initialize, self)
4046
end
4147

4248
# Disable dependency loading during request cycle

railties/lib/rails/engine.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ def load_tasks
132132
config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) }
133133
end
134134

135+
def eager_load!
136+
config.eager_load_paths.each do |load_path|
137+
matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
138+
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
139+
require_dependency file.sub(matcher, '\1')
140+
end
141+
end
142+
end
143+
135144
# Add configured load paths to ruby load paths and remove duplicates.
136145
initializer :set_load_path, :before => :bootstrap_hook do
137146
config.load_paths.reverse_each do |path|
@@ -203,19 +212,9 @@ def load_tasks
203212
end
204213
end
205214

206-
# This needs to be an initializer, since it needs to run once
207-
# per engine and get the engine as a block parameter
208-
initializer :load_app_classes, :before => :finisher_hook do |app|
209-
next if $rails_rake_task
210-
211-
if app.config.cache_classes
212-
config.eager_load_paths.each do |load_path|
213-
matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
214-
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
215-
require_dependency file.sub(matcher, '\1')
216-
end
217-
end
218-
end
215+
initializer :engines_blank_point do
216+
# We need this initializer so all extra initializers added in engines are
217+
# consistently executed after all the initializers above across all engines.
219218
end
220219

221220
protected

railties/lib/rails/railtie.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ def abstract_railtie?
197197
end
198198
end
199199

200+
def eager_load!
201+
end
202+
200203
def rake_tasks
201204
self.class.rake_tasks
202205
end

railties/test/railties/engine_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,31 @@ def reload_config
2828
boot_rails
2929
assert !Rails::Engine.respond_to?(:config)
3030
end
31+
32+
test "initializers are executed after application configuration initializers" do
33+
@plugin.write "lib/bukkits.rb", <<-RUBY
34+
class Bukkits
35+
class Engine < ::Rails::Engine
36+
initializer "dummy_initializer" do
37+
end
38+
end
39+
end
40+
RUBY
41+
42+
boot_rails
43+
44+
initializers = Rails.application.initializers
45+
index = initializers.index { |i| i.name == "dummy_initializer" }
46+
selection = initializers[(index-3)..(index)].map(&:name).map(&:to_s)
47+
48+
assert_equal %w(
49+
load_config_initializers
50+
load_config_initializers
51+
engines_blank_point
52+
dummy_initializer
53+
), selection
54+
55+
assert index < initializers.index { |i| i.name == :build_middleware_stack }
56+
end
3157
end
3258
end

0 commit comments

Comments
 (0)