Skip to content

Commit

Permalink
Replace AD::Callbacks.to_prepare with AD::Reloader.to_prepare
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
John Firebaugh authored and josevalim committed Dec 20, 2010
1 parent 0f7c970 commit 435bccd
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 96 deletions.
31 changes: 6 additions & 25 deletions actionpack/lib/action_dispatch/middleware/callbacks.rb
@@ -1,32 +1,14 @@
module ActionDispatch
# Provide callbacks to be executed before and after the request dispatch.
#
# It also provides a to_prepare callback, which is performed in all requests
# in development by only once in production and notification callback for async
# operations.
#
class Callbacks
include ActiveSupport::Callbacks

define_callbacks :call, :rescuable => true
define_callbacks :prepare, :scope => :name

# Add a preparation callback. Preparation callbacks are run before every
# request in development mode, and before the first request in production mode.
#
# 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
ActiveSupport::Deprecation.warn "ActionDispatch::Callbacks.to_prepare is deprecated. " <<
"Please use ActionDispatch::Reloader.to_prepare instead."
ActionDispatch::Reloader.to_prepare(*args, &block)
end

def self.before(*args, &block)
Expand All @@ -37,14 +19,13 @@ def self.after(*args, &block)
set_callback(:call, :after, *args, &block)
end

def initialize(app, prepare_each_request = false)
@app, @prepare_each_request = app, prepare_each_request
_run_prepare_callbacks
def initialize(app, unused = nil)
ActiveSupport::Deprecation.warn "Passing a second argument to ActionDispatch::Callbacks.new is deprecated." unless unused.nil?
@app = app
end

def call(env)
_run_call_callbacks do
_run_prepare_callbacks if @prepare_each_request
@app.call(env)
end
end
Expand Down
72 changes: 13 additions & 59 deletions actionpack/test/dispatch/callbacks_test.rb
@@ -1,6 +1,6 @@
require 'abstract_unit'

class DispatcherTest < Test::Unit::TestCase
class DispatcherTest < ActiveSupport::TestCase
class Foo
cattr_accessor :a, :b
end
Expand All @@ -13,65 +13,9 @@ def call(env)

def setup
Foo.a, Foo.b = 0, 0
ActionDispatch::Callbacks.reset_callbacks(:prepare)
ActionDispatch::Callbacks.reset_callbacks(:call)
end

def test_prepare_callbacks_with_cache_classes
a = b = c = nil
ActionDispatch::Callbacks.to_prepare { |*args| a = b = c = 1 }
ActionDispatch::Callbacks.to_prepare { |*args| b = c = 2 }
ActionDispatch::Callbacks.to_prepare { |*args| c = 3 }

# Ensure to_prepare callbacks are not run when defined
assert_nil a || b || c

# Run callbacks
dispatch

assert_equal 1, a
assert_equal 2, b
assert_equal 3, c

# Make sure they are only run once
a = b = c = nil
dispatch
assert_nil a || b || c
end

def test_prepare_callbacks_without_cache_classes
a = b = c = nil
ActionDispatch::Callbacks.to_prepare { |*args| a = b = c = 1 }
ActionDispatch::Callbacks.to_prepare { |*args| b = c = 2 }
ActionDispatch::Callbacks.to_prepare { |*args| c = 3 }

# Ensure to_prepare callbacks are not run when defined
assert_nil a || b || c

# Run callbacks
dispatch(false)

assert_equal 1, a
assert_equal 2, b
assert_equal 3, c

# Make sure they are run again
a = b = c = nil
dispatch(false)
assert_equal 1, a
assert_equal 2, b
assert_equal 3, c
end

def test_to_prepare_with_identifier_replaces
ActionDispatch::Callbacks.to_prepare(:unique_id) { |*args| Foo.a, Foo.b = 1, 1 }
ActionDispatch::Callbacks.to_prepare(:unique_id) { |*args| Foo.a = 2 }

dispatch
assert_equal 2, Foo.a
assert_equal 0, Foo.b
end

def test_before_and_after_callbacks
ActionDispatch::Callbacks.before { |*args| Foo.a += 1; Foo.b += 1 }
ActionDispatch::Callbacks.after { |*args| Foo.a += 1; Foo.b += 1 }
Expand All @@ -85,10 +29,20 @@ def test_before_and_after_callbacks
assert_equal 4, Foo.b
end

def test_to_prepare_deprecation
prepared = false
assert_deprecated do
ActionDispatch::Callbacks.to_prepare { prepared = true }
end

ActionDispatch::Reloader.prepare!
assert prepared
end

private

def dispatch(cache_classes = true, &block)
@dispatcher ||= ActionDispatch::Callbacks.new(block || DummyApp.new, !cache_classes)
def dispatch(&block)
@dispatcher ||= ActionDispatch::Callbacks.new(block || DummyApp.new)
@dispatcher.call({'rack.input' => StringIO.new('')})
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/railtie.rb
Expand Up @@ -82,7 +82,7 @@ class Railtie < Rails::Railtie
ActiveSupport.on_load(:active_record) do
instantiate_observers

ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do
ActionDispatch::Reloader.to_prepare(:activerecord_instantiate_observers) do
ActiveRecord::Base.instantiate_observers
end
end
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/file_update_checker.rb
Expand Up @@ -8,7 +8,7 @@ module ActiveSupport
# I18n.reload!
# end
#
# ActionDispatch::Callbacks.to_prepare do
# ActionDispatch::Reloader.to_prepare do
# i18n_reloader.execute_if_updated
# end
#
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/i18n_railtie.rb
Expand Up @@ -19,7 +19,7 @@ def self.reloader
# on to_prepare callbacks. This will only happen on the config.after_initialize
# callback below.
initializer "i18n.callbacks" do
ActionDispatch::Callbacks.to_prepare do
ActionDispatch::Reloader.to_prepare do
I18n::Railtie.reloader.execute_if_updated
end
end
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/application.rb
Expand Up @@ -157,7 +157,7 @@ def default_middleware_stack
middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
middleware.use ::ActionDispatch::Reloader unless config.cache_classes
middleware.use ::ActionDispatch::Callbacks, !config.cache_classes
middleware.use ::ActionDispatch::Callbacks
middleware.use ::ActionDispatch::Cookies

if config.session_store
Expand Down
8 changes: 6 additions & 2 deletions railties/lib/rails/application/finisher.rb
Expand Up @@ -21,7 +21,7 @@ module Finisher

initializer :add_to_prepare_blocks do
config.to_prepare_blocks.each do |block|
ActionDispatch::Callbacks.to_prepare(&block)
ActionDispatch::Reloader.to_prepare(&block)
end
end

Expand All @@ -37,6 +37,10 @@ module Finisher
build_middleware_stack
end

initializer :run_prepare_callbacks do
ActionDispatch::Reloader.prepare!
end

initializer :eager_load! do
if config.cache_classes && !$rails_rake_task
ActiveSupport.run_load_hooks(:before_eager_load, self)
Expand All @@ -52,7 +56,7 @@ module Finisher
initializer :set_routes_reloader do |app|
reloader = lambda { app.routes_reloader.execute_if_updated }
reloader.call
ActionDispatch::Callbacks.to_prepare(&reloader)
ActionDispatch::Reloader.to_prepare(&reloader)
end

# Disable dependency loading during request cycle
Expand Down
3 changes: 1 addition & 2 deletions railties/lib/rails/console/app.rb
Expand Up @@ -26,7 +26,6 @@ def new_session
# reloads the environment
def reload!(print=true)
puts "Reloading..." if print
# This triggers the to_prepare callbacks
ActionDispatch::Callbacks.new(Proc.new {}, false).call({})
ActionDispatch::Reloader.reload!
true
end
8 changes: 4 additions & 4 deletions railties/test/application/console_test.rb
Expand Up @@ -26,14 +26,14 @@ def test_new_session_should_return_integration_session
assert_instance_of ActionDispatch::Integration::Session, session
end

def test_reload_should_fire_preparation_callbacks
def test_reload_should_fire_preparation_and_cleanup_callbacks
load_environment
a = b = c = nil

# TODO: These should be defined on the initializer
ActionDispatch::Callbacks.to_prepare { a = b = c = 1 }
ActionDispatch::Callbacks.to_prepare { b = c = 2 }
ActionDispatch::Callbacks.to_prepare { c = 3 }
ActionDispatch::Reloader.to_prepare { a = b = c = 1 }
ActionDispatch::Reloader.to_prepare { b = c = 2 }
ActionDispatch::Reloader.to_cleanup { c = 3 }

# Hide Reloading... output
silence_stream(STDOUT) { reload! }
Expand Down

0 comments on commit 435bccd

Please sign in to comment.