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.
This commit is contained in:
José Valim 2010-05-15 23:48:56 +02:00
parent 6617d01893
commit 351816fab6
4 changed files with 49 additions and 15 deletions

View File

@ -35,8 +35,14 @@ module Rails
app
end
initializer :finisher_hook do |app|
ActiveSupport.run_load_hooks(:after_initialize, app)
initializer :eager_load! do
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
# Disable dependency loading during request cycle

View File

@ -132,6 +132,15 @@ module Rails
config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) }
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.
initializer :set_load_path, :before => :bootstrap_hook do
config.load_paths.reverse_each do |path|
@ -203,19 +212,9 @@ module Rails
end
end
# This needs to be an initializer, since it needs to run once
# per engine and get the engine as a block parameter
initializer :load_app_classes, :before => :finisher_hook do |app|
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
initializer :engines_blank_point do
# We need this initializer so all extra initializers added in engines are
# consistently executed after all the initializers above across all engines.
end
protected

View File

@ -197,6 +197,9 @@ module Rails
end
end
def eager_load!
end
def rake_tasks
self.class.rake_tasks
end

View File

@ -28,5 +28,31 @@ module RailtiesTest
boot_rails
assert !Rails::Engine.respond_to?(:config)
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