Skip to content

Commit

Permalink
Small changes on AD::Reloader.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Dec 20, 2010
1 parent e683ab7 commit 0cbfd6c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 45 deletions.
23 changes: 3 additions & 20 deletions actionpack/lib/action_dispatch/middleware/reloader.rb
Expand Up @@ -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!
Expand All @@ -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
Expand Down
25 changes: 5 additions & 20 deletions actionpack/test/dispatch/reloader_test.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/railtie.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion railties/lib/rails/console/app.rb
Expand Up @@ -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
6 changes: 3 additions & 3 deletions railties/test/application/console_test.rb
Expand Up @@ -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! }
Expand Down

0 comments on commit 0cbfd6c

Please sign in to comment.