diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb index 1a3e05cc868405f99d07a31cd3190c6ffcf0a99d..ca826e7bfc1fa35b9726dcdae978f4f2580302ad 100644 --- a/actionpack/lib/action_controller.rb +++ b/actionpack/lib/action_controller.rb @@ -59,6 +59,7 @@ def self.load_all! autoload :ParamsParser, 'action_controller/params_parser' autoload :PolymorphicRoutes, 'action_controller/polymorphic_routes' autoload :RecordIdentifier, 'action_controller/record_identifier' + autoload :Reloader, 'action_controller/reloader' autoload :Request, 'action_controller/request' autoload :RequestForgeryProtection, 'action_controller/request_forgery_protection' autoload :Rescue, 'action_controller/rescue' diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb index e91babde100eea92ae90dcf57165098414483bbd..ec40b5c4e66d600fd153a1002c73323e34165025 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatcher.rb @@ -5,8 +5,9 @@ class Dispatcher class << self def define_dispatcher_callbacks(cache_classes) unless cache_classes - # Development mode callbacks - before_dispatch :reload_application + unless self.middleware.include?(Reloader) + self.middleware.insert_after(Failsafe, Reloader) + end ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false end @@ -41,6 +42,30 @@ def to_prepare(identifier = nil, &block) callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier) @prepare_dispatch_callbacks.replace_or_append!(callback) end + + def run_prepare_callbacks + if defined?(Rails) && Rails.logger + logger = Rails.logger + else + logger = Logger.new($stderr) + end + + new(logger).send :run_callbacks, :prepare_dispatch + end + + def reload_application + # Run prepare callbacks before every request in development mode + run_prepare_callbacks + + Routing::Routes.reload + end + + def cleanup_application + # Cleanup the application before processing the current request. + ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord) + ActiveSupport::Dependencies.clear + ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) + end end cattr_accessor :middleware @@ -87,18 +112,6 @@ def _call(env) dispatch end - def reload_application - # Cleanup the application before processing the current request. - ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord) - ActiveSupport::Dependencies.clear - ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) - - # Run prepare callbacks before every request in development mode - run_callbacks :prepare_dispatch - - Routing::Routes.reload - end - def flush_logger Base.logger.flush end diff --git a/actionpack/lib/action_controller/reloader.rb b/actionpack/lib/action_controller/reloader.rb new file mode 100644 index 0000000000000000000000000000000000000000..46789309cd05cd43e34adf5c500b1bc10fcb5762 --- /dev/null +++ b/actionpack/lib/action_controller/reloader.rb @@ -0,0 +1,14 @@ +module ActionController + class Reloader + def initialize(app) + @app = app + end + + def call(env) + Dispatcher.reload_application + @app.call(env) + ensure + Dispatcher.cleanup_application + end + end +end diff --git a/actionpack/test/controller/dispatcher_test.rb b/actionpack/test/controller/dispatcher_test.rb index 47226f1fc545e3d0c92883d89139d1607d8bec9f..7887b7110c5340c65d2c915b4550484598ff4a15 100644 --- a/actionpack/test/controller/dispatcher_test.rb +++ b/actionpack/test/controller/dispatcher_test.rb @@ -6,14 +6,17 @@ class DispatcherTest < Test::Unit::TestCase def setup ENV['REQUEST_METHOD'] = 'GET' + Dispatcher.middleware = ActionController::MiddlewareStack.new do |middleware| + middlewares = File.expand_path(File.join(File.dirname(__FILE__), "../../lib/action_controller/middlewares.rb")) + middleware.instance_eval(File.read(middlewares)) + end + # Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new) Dispatcher.instance_variable_set("@before_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new) Dispatcher.instance_variable_set("@after_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new) Dispatcher.stubs(:require_dependency) - - @dispatcher = Dispatcher.new end def teardown @@ -65,7 +68,7 @@ def test_prepare_callbacks assert_nil a || b || c # Run callbacks - @dispatcher.send :run_callbacks, :prepare_dispatch + Dispatcher.run_prepare_callbacks assert_equal 1, a assert_equal 2, b @@ -82,7 +85,7 @@ def test_to_prepare_with_identifier_replaces Dispatcher.to_prepare(:unique_id) { |*args| a = b = 1 } Dispatcher.to_prepare(:unique_id) { |*args| a = 2 } - @dispatcher.send :run_callbacks, :prepare_dispatch + Dispatcher.run_prepare_callbacks assert_equal 2, a assert_equal nil, b end @@ -91,7 +94,7 @@ def test_to_prepare_with_identifier_replaces def dispatch(cache_classes = true) ActionController::Routing::RouteSet.any_instance.stubs(:call).returns([200, {}, 'response']) Dispatcher.define_dispatcher_callbacks(cache_classes) - @dispatcher.call({}) + Dispatcher.new.call({}) end def assert_subclasses(howmany, klass, message = klass.subclasses.inspect) diff --git a/railties/lib/console_app.rb b/railties/lib/console_app.rb index a35c96c95739af4dc3b895d017aa534073a7f895..d7cd57564f0b210a38dcc6933a69d6e83f301fec 100644 --- a/railties/lib/console_app.rb +++ b/railties/lib/console_app.rb @@ -24,7 +24,6 @@ def new_session #reloads the environment def reload! puts "Reloading..." - dispatcher = ActionController::Dispatcher.new($stdout) - dispatcher.reload_application + Dispatcher.reload_application true end diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 281b074e0a1abfb9794160b52d3dc7ffc24d2df9..7e1bd23a551e6558e1da88ac59b830d67a664d64 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -583,7 +583,7 @@ def prepare_dispatcher return unless configuration.frameworks.include?(:action_controller) require 'dispatcher' unless defined?(::Dispatcher) Dispatcher.define_dispatcher_callbacks(configuration.cache_classes) - Dispatcher.new(Rails.logger).send :run_callbacks, :prepare_dispatch + Dispatcher.run_prepare_callbacks end def disable_dependency_loading