1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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 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

View file

@ -132,6 +132,15 @@ module Rails
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 @@ module Rails
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

View file

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

View file

@ -28,5 +28,31 @@ module RailtiesTest
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