diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/application/finisher.rb | 10 | ||||
-rw-r--r-- | railties/lib/rails/engine.rb | 25 | ||||
-rw-r--r-- | railties/lib/rails/railtie.rb | 3 | ||||
-rw-r--r-- | railties/test/railties/engine_test.rb | 26 |
4 files changed, 49 insertions, 15 deletions
diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 03bc270c81..9d04f1ce38 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -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 diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 652bd40ee4..b44755820c 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -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 diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index b6b57bc5b5..1dba6e1538 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -197,6 +197,9 @@ module Rails end end + def eager_load! + end + def rake_tasks self.class.rake_tasks end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 40ac11fa03..b3f65fd00d 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -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 |