diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/guides/source/initialization.textile | 2 | ||||
-rw-r--r-- | railties/lib/rails/application.rb | 10 | ||||
-rw-r--r-- | railties/lib/rails/application/bootstrap.rb | 8 | ||||
-rw-r--r-- | railties/lib/rails/application/configuration.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/application/finisher.rb | 6 | ||||
-rw-r--r-- | railties/lib/rails/engine.rb | 15 | ||||
-rw-r--r-- | railties/lib/rails/railtie/configuration.rb | 48 | ||||
-rw-r--r-- | railties/test/application/initializers/initializers_test.rb | 13 |
8 files changed, 65 insertions, 39 deletions
diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile index 9ce27fa331..96d6998e1c 100644 --- a/railties/guides/source/initialization.textile +++ b/railties/guides/source/initialization.textile @@ -2379,7 +2379,6 @@ Now that we've referenced that class, it will be required for us. You'll notice * initialize_subscriber * set_clear_dependencies_hook * initialize_dependency_mechanism -* bootstrap_load_path These are all defined using the +initializer+ method: @@ -2930,7 +2929,6 @@ With +@@autoloads+ being * initialize_subscriber * set_clear_dependencies_hook * initialize_dependency_mechanism -* bootstrap_load_path h4. Active Support Initializers diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index d39f9a2ae9..9e18dccf69 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -12,7 +12,7 @@ module Rails # points to it. # # In other words, Rails::Application is Singleton and whenever you are accessing - # Rails::Application.config or YourApplication::Application.config, you are actually + # Rails::Application.config or YourApplication::Application.config, you are actually # accessing YourApplication::Application.instance.config. # # == Initialization @@ -40,7 +40,7 @@ module Rails # # The Application is also responsible for building the middleware stack and setting up # both application and engines metals. - # + # class Application < Engine autoload :Bootstrap, 'rails/application/bootstrap' autoload :Configurable, 'rails/application/configurable' @@ -69,6 +69,8 @@ module Rails raise "You cannot have more than one Rails::Application" if Rails.application super Rails.application = base.instance + + ActiveSupport.run_load_hooks(:before_configuration, base.instance) end def respond_to?(*args) @@ -82,7 +84,7 @@ module Rails end end - delegate :metal_loader, :to => :config + delegate :middleware, :metal_loader, :to => :config def require_environment! environment = paths.config.environment.to_a.first @@ -125,7 +127,7 @@ module Rails end def app - @app ||= middleware.build(routes) + @app ||= config.middleware.build(routes) end def call(env) diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index 022e1a91d8..e62eed8a87 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -10,7 +10,8 @@ module Rails require environment if environment end - initializer :load_all_active_support do + initializer :load_active_support do + require 'active_support/dependencies' require "active_support/all" unless config.active_support.bare end @@ -18,7 +19,6 @@ module Rails # Used by Passenger to ensure everything's loaded before forking and # to avoid autoload race conditions in JRuby. initializer :preload_frameworks do - require 'active_support/dependencies' ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks end @@ -66,8 +66,8 @@ module Rails ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load end - initializer :bootstrap_load_path do - # This is just an initializer used as hook so all load paths are loaded together + initializer :bootstrap_hook do |app| + ActiveSupport.run_load_hooks(:before_initialize, app) end end end diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 1ad77fdfec..cd77f1adaf 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -33,7 +33,7 @@ module Rails end def middleware - @middleware ||= default_middleware_stack + @middleware ||= app_middleware.merge_into(default_middleware_stack) end def metal_loader diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 94507bb387..03bc270c81 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -35,10 +35,8 @@ module Rails app end - initializer :after_initialize do - config.after_initialize_blocks.each do |block| - block.call(self) - end + initializer :finisher_hook do |app| + ActiveSupport.run_load_hooks(:after_initialize, app) end # Disable dependency loading during request cycle diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index ab0ead65a9..652bd40ee4 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -45,7 +45,7 @@ module Rails # app.middleware.use MyEngine::Middleware # end # end - # + # # == Paths # # Since Rails 3.0, both your Application and Engines do not have hardcoded paths. @@ -125,7 +125,7 @@ module Rails end end - delegate :middleware, :paths, :root, :to => :config + delegate :paths, :root, :to => :config def load_tasks super @@ -133,7 +133,7 @@ module Rails end # Add configured load paths to ruby load paths and remove duplicates. - initializer :set_load_path, :before => :bootstrap_load_path do + initializer :set_load_path, :before => :bootstrap_hook do config.load_paths.reverse_each do |path| $LOAD_PATH.unshift(path) if File.directory?(path) end @@ -142,7 +142,10 @@ module Rails # Set the paths from which Rails will automatically load source files, # and the load_once paths. - initializer :set_autoload_paths, :before => :bootstrap_load_path do |app| + # + # This needs to be an initializer, since it needs to run once + # per engine and get the engine as a block parameter + initializer :set_autoload_paths, :before => :bootstrap_hook do |app| ActiveSupport::Dependencies.load_paths.unshift(*config.load_paths) if reloadable?(app) @@ -200,7 +203,9 @@ module Rails end end - initializer :load_app_classes do |app| + # 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 diff --git a/railties/lib/rails/railtie/configuration.rb b/railties/lib/rails/railtie/configuration.rb index 16eccaccc4..f57d82a3d8 100644 --- a/railties/lib/rails/railtie/configuration.rb +++ b/railties/lib/rails/railtie/configuration.rb @@ -3,10 +3,50 @@ require 'rails/configuration' module Rails class Railtie class Configuration + class MiddlewareStackProxy + def initialize + @operations = [] + end + + def insert_before(*args, &block) + @operations << [:insert_before, args, block] + end + + alias insert insert_before + + def insert_after(*args, &block) + @operations << [:insert_after, args, block] + end + + def swap(*args, &block) + @operations << [:swap, args, block] + end + + def use(*args, &block) + @operations << [:use, args, block] + end + + def merge_into(other) + @operations.each do |operation, args, block| + other.send(operation, *args, &block) + end + other + end + end + def initialize @@options ||= {} end + # This allows you to modify the application's middlewares from Engines. + # + # All operations you run on the app_middleware will be replayed on the + # application once it is defined and the default_middlewares are + # created + def app_middleware + @@app_middleware ||= MiddlewareStackProxy.new + end + # Holds generators configuration: # # config.generators do |g| @@ -28,12 +68,8 @@ module Rails end end - def after_initialize_blocks - @@after_initialize_blocks ||= [] - end - - def after_initialize(&blk) - after_initialize_blocks << blk if blk + def after_initialize(&block) + ActiveSupport.on_load(:after_initialize, :yield => true, &block) end def to_prepare_blocks diff --git a/railties/test/application/initializers/initializers_test.rb b/railties/test/application/initializers/initializers_test.rb index 2e6a707175..eca42dada6 100644 --- a/railties/test/application/initializers/initializers_test.rb +++ b/railties/test/application/initializers/initializers_test.rb @@ -28,19 +28,6 @@ module ApplicationTests assert_equal "congratulations", $test_after_initialize_block2 end - test "after_initialize block works correctly when no block is passed" do - add_to_config <<-RUBY - config.root = "#{app_path}" - config.after_initialize { $test_after_initialize_block1 = "success" } - config.after_initialize # don't pass a block, this is what we're testing! - config.after_initialize { $test_after_initialize_block2 = "congratulations" } - RUBY - require "#{app_path}/config/environment" - - assert_equal "success", $test_after_initialize_block1 - assert_equal "congratulations", $test_after_initialize_block2 - end - test "after_initialize runs after frameworks have been initialized" do $activerecord_configurations = nil add_to_config <<-RUBY |