提交 351816fa 编写于 作者: J José Valim

Ensure that eager_load actually takes place just after the middleware stack is...

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.
上级 6617d018
...@@ -35,8 +35,14 @@ module Finisher ...@@ -35,8 +35,14 @@ module Finisher
app app
end end
initializer :finisher_hook do |app| initializer :eager_load! do
ActiveSupport.run_load_hooks(:after_initialize, app) if config.cache_classes && !$rails_rake_task
railties.all(&:eager_load!)
end
end
initializer :finisher_hook do
ActiveSupport.run_load_hooks(:after_initialize, self)
end end
# Disable dependency loading during request cycle # Disable dependency loading during request cycle
......
...@@ -132,6 +132,15 @@ def load_tasks ...@@ -132,6 +132,15 @@ def load_tasks
config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) } config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) }
end end
def eager_load!
config.eager_load_paths.each do |load_path|
matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
require_dependency file.sub(matcher, '\1')
end
end
end
# Add configured load paths to ruby load paths and remove duplicates. # Add configured load paths to ruby load paths and remove duplicates.
initializer :set_load_path, :before => :bootstrap_hook do initializer :set_load_path, :before => :bootstrap_hook do
config.load_paths.reverse_each do |path| config.load_paths.reverse_each do |path|
...@@ -203,19 +212,9 @@ def load_tasks ...@@ -203,19 +212,9 @@ def load_tasks
end end
end end
# This needs to be an initializer, since it needs to run once initializer :engines_blank_point do
# per engine and get the engine as a block parameter # We need this initializer so all extra initializers added in engines are
initializer :load_app_classes, :before => :finisher_hook do |app| # consistently executed after all the initializers above across all engines.
next if $rails_rake_task
if app.config.cache_classes
config.eager_load_paths.each do |load_path|
matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
require_dependency file.sub(matcher, '\1')
end
end
end
end end
protected protected
......
...@@ -197,6 +197,9 @@ def abstract_railtie? ...@@ -197,6 +197,9 @@ def abstract_railtie?
end end
end end
def eager_load!
end
def rake_tasks def rake_tasks
self.class.rake_tasks self.class.rake_tasks
end end
......
...@@ -28,5 +28,31 @@ def reload_config ...@@ -28,5 +28,31 @@ def reload_config
boot_rails boot_rails
assert !Rails::Engine.respond_to?(:config) assert !Rails::Engine.respond_to?(:config)
end end
test "initializers are executed after application configuration initializers" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
class Engine < ::Rails::Engine
initializer "dummy_initializer" do
end
end
end
RUBY
boot_rails
initializers = Rails.application.initializers
index = initializers.index { |i| i.name == "dummy_initializer" }
selection = initializers[(index-3)..(index)].map(&:name).map(&:to_s)
assert_equal %w(
load_config_initializers
load_config_initializers
engines_blank_point
dummy_initializer
), selection
assert index < initializers.index { |i| i.name == :build_middleware_stack }
end
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册