diff --git a/actionpack/lib/action_dispatch/middleware/reloader.rb b/actionpack/lib/action_dispatch/middleware/reloader.rb index b84410fc623d7..579b5d8a02ec4 100644 --- a/actionpack/lib/action_dispatch/middleware/reloader.rb +++ b/actionpack/lib/action_dispatch/middleware/reloader.rb @@ -25,26 +25,14 @@ class Reloader # Add a preparation callback. Preparation callbacks are run before each # request. - # - # If a symbol with a block is given, the symbol is used as an identifier. - # That allows to_prepare to be called again with the same identifier to - # replace the existing callback. Passing an identifier is a suggested - # practice if the code adding a preparation block may be reloaded. def self.to_prepare(*args, &block) - first_arg = args.first - if first_arg.is_a?(Symbol) && block_given? - remove_method :"__#{first_arg}" if method_defined?(:"__#{first_arg}") - define_method :"__#{first_arg}", &block - set_callback(:prepare, :"__#{first_arg}") - else - set_callback(:prepare, *args, &block) - end + set_callback(:prepare, *args, &block) end # Add a cleanup callback. Cleanup callbacks are run after each request is # complete (after #close is called on the response body). - def self.to_cleanup(&block) - set_callback(:cleanup, &block) + def self.to_cleanup(*args, &block) + set_callback(:cleanup, *args, &block) end def self.prepare! @@ -55,11 +43,6 @@ def self.cleanup! new(nil).send(:_run_cleanup_callbacks) end - def self.reload! - prepare! - cleanup! - end - def initialize(app) @app = app end diff --git a/actionpack/test/dispatch/reloader_test.rb b/actionpack/test/dispatch/reloader_test.rb index 58ef346b5c261..995b19030c33d 100644 --- a/actionpack/test/dispatch/reloader_test.rb +++ b/actionpack/test/dispatch/reloader_test.rb @@ -20,16 +20,6 @@ def test_prepare_callbacks assert_equal 3, c end - def test_to_prepare_with_identifier_replaces - a = b = 0 - Reloader.to_prepare(:unique_id) { |*args| a = b = 1 } - Reloader.to_prepare(:unique_id) { |*args| a = 2 } - - call_and_return_body - assert_equal 2, a - assert_equal 0, b - end - class MyBody < Array def initialize(&block) @on_close = block @@ -50,7 +40,7 @@ def close def test_returned_body_object_always_responds_to_close body = call_and_return_body - assert body.respond_to?(:close) + assert_respond_to body, :close end def test_returned_body_object_behaves_like_underlying_object @@ -83,10 +73,10 @@ def test_returned_body_object_responds_to_all_methods_supported_by_underlying_ob body = call_and_return_body do [200, { "Content-Type" => "text/html" }, MyBody.new] end - assert body.respond_to?(:size) - assert body.respond_to?(:each) - assert body.respond_to?(:foo) - assert body.respond_to?(:bar) + assert_respond_to body, :size + assert_respond_to body, :each + assert_respond_to body, :foo + assert_respond_to body, :bar end def test_cleanup_callbacks_are_called_when_body_is_closed @@ -124,11 +114,6 @@ def test_manual_reloading Reloader.cleanup! assert !prepared assert cleaned - - prepared = cleaned = false - Reloader.reload! - assert prepared - assert cleaned end private diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 61fb0ae468aa7..2accf0a48f675 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -80,7 +80,7 @@ class Railtie < Rails::Railtie ActiveSupport.on_load(:active_record) do instantiate_observers - ActionDispatch::Reloader.to_prepare(:activerecord_instantiate_observers) do + ActionDispatch::Reloader.to_prepare do ActiveRecord::Base.instantiate_observers end end diff --git a/railties/lib/rails/console/app.rb b/railties/lib/rails/console/app.rb index 4a988efd82390..95c74baae2ce4 100644 --- a/railties/lib/rails/console/app.rb +++ b/railties/lib/rails/console/app.rb @@ -26,6 +26,7 @@ def new_session # reloads the environment def reload!(print=true) puts "Reloading..." if print - ActionDispatch::Reloader.reload! + ActionDispatch::Reloader.cleanup! + ActionDispatch::Reloader.prepare! true end diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb index 5f84c2b9481ed..793e73556c1a8 100644 --- a/railties/test/application/console_test.rb +++ b/railties/test/application/console_test.rb @@ -31,9 +31,9 @@ def test_reload_should_fire_preparation_and_cleanup_callbacks a = b = c = nil # TODO: These should be defined on the initializer - ActionDispatch::Reloader.to_prepare { a = b = c = 1 } - ActionDispatch::Reloader.to_prepare { b = c = 2 } - ActionDispatch::Reloader.to_cleanup { c = 3 } + ActionDispatch::Reloader.to_cleanup { a = b = c = 1 } + ActionDispatch::Reloader.to_cleanup { b = c = 2 } + ActionDispatch::Reloader.to_prepare { c = 3 } # Hide Reloading... output silence_stream(STDOUT) { reload! }