aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-02-20 12:04:57 -0600
committerJoshua Peek <josh@joshpeek.com>2009-02-20 12:04:57 -0600
commit69c049f5ab45bf9bfb0d269acea0773581905fd4 (patch)
treef605c68ba9fae9ddfa77115ad0cef0da5a8ce770 /actionpack
parent53fe301a42f9d18b4a8a475e9babf64c621369ae (diff)
downloadrails-69c049f5ab45bf9bfb0d269acea0773581905fd4.tar.gz
rails-69c049f5ab45bf9bfb0d269acea0773581905fd4.tar.bz2
rails-69c049f5ab45bf9bfb0d269acea0773581905fd4.zip
Move development mode reloading up the stack to avoid issues with class reloading in middleware
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller.rb1
-rw-r--r--actionpack/lib/action_controller/dispatcher.rb41
-rw-r--r--actionpack/lib/action_controller/reloader.rb14
-rw-r--r--actionpack/test/controller/dispatcher_test.rb13
4 files changed, 50 insertions, 19 deletions
diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb
index 1a3e05cc86..ca826e7bfc 100644
--- a/actionpack/lib/action_controller.rb
+++ b/actionpack/lib/action_controller.rb
@@ -59,6 +59,7 @@ module ActionController
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 e91babde10..ec40b5c4e6 100644
--- a/actionpack/lib/action_controller/dispatcher.rb
+++ b/actionpack/lib/action_controller/dispatcher.rb
@@ -5,8 +5,9 @@ module ActionController
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 @@ module ActionController
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 @@ module ActionController
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 0000000000..46789309cd
--- /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 47226f1fc5..7887b7110c 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 @@ class DispatcherTest < Test::Unit::TestCase
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 @@ class DispatcherTest < Test::Unit::TestCase
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 @@ class DispatcherTest < Test::Unit::TestCase
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)