Permalink
Browse files

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.
  • Loading branch information...
1 parent 6617d01 commit 351816fab6dbe564b7bddbd877648edb06a2bfb1 @josevalim josevalim committed May 15, 2010
View
10 railties/lib/rails/application/finisher.rb
@@ -35,8 +35,14 @@ module Finisher
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
25 railties/lib/rails/engine.rb
@@ -132,6 +132,15 @@ def load_tasks
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 @@ def load_tasks
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
3 railties/lib/rails/railtie.rb
@@ -197,6 +197,9 @@ def abstract_railtie?
end
end
+ def eager_load!
+ end
+
def rake_tasks
self.class.rake_tasks
end
View
26 railties/test/railties/engine_test.rb
@@ -28,5 +28,31 @@ def reload_config
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

0 comments on commit 351816f

Please sign in to comment.