2010-03-18 17:59:13 -04:00
|
|
|
require 'abstract_unit'
|
2009-02-27 22:25:45 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'active_support'
|
|
|
|
|
|
|
|
class GrandParent
|
2009-10-12 23:15:43 -04:00
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
attr_reader :log, :action_name
|
|
|
|
def initialize(action_name)
|
|
|
|
@action_name, @log = action_name, []
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
define_callbacks :dispatch
|
2009-06-03 00:41:31 -04:00
|
|
|
set_callback :dispatch, :before, :before1, :before2, :per_key => {:if => proc {|c| c.action_name == "index" || c.action_name == "update" }}
|
2009-10-12 23:15:43 -04:00
|
|
|
set_callback :dispatch, :after, :after1, :after2, :per_key => {:if => proc {|c| c.action_name == "update" || c.action_name == "delete" }}
|
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def before1
|
|
|
|
@log << "before1"
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def before2
|
|
|
|
@log << "before2"
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
|
|
|
def after1
|
2009-02-27 22:25:45 -05:00
|
|
|
@log << "after1"
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def after2
|
|
|
|
@log << "after2"
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def dispatch
|
2009-10-12 23:15:43 -04:00
|
|
|
run_callbacks(:dispatch, action_name) do
|
2009-02-27 22:25:45 -05:00
|
|
|
@log << action_name
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Parent < GrandParent
|
2009-06-03 00:41:31 -04:00
|
|
|
skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}
|
|
|
|
skip_callback :dispatch, :after, :after2, :per_key => {:unless => proc {|c| c.action_name == "delete" }}
|
2009-02-27 22:25:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class Child < GrandParent
|
2009-06-03 00:41:31 -04:00
|
|
|
skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}, :if => :state_open?
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def state_open?
|
|
|
|
@state == :open
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def initialize(action_name, state)
|
|
|
|
super(action_name)
|
|
|
|
@state = state
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-31 19:48:12 -05:00
|
|
|
class EmptyParent
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
|
|
|
def performed?
|
|
|
|
@performed ||= false
|
|
|
|
end
|
|
|
|
|
|
|
|
define_callbacks :dispatch
|
|
|
|
|
|
|
|
def perform!
|
|
|
|
@performed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def dispatch
|
|
|
|
_run_dispatch_callbacks
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class EmptyChild < EmptyParent
|
|
|
|
set_callback :dispatch, :before, :do_nothing
|
|
|
|
|
|
|
|
def do_nothing
|
|
|
|
end
|
|
|
|
end
|
2009-02-27 22:25:45 -05:00
|
|
|
|
|
|
|
class BasicCallbacksTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@index = GrandParent.new("index").dispatch
|
|
|
|
@update = GrandParent.new("update").dispatch
|
|
|
|
@delete = GrandParent.new("delete").dispatch
|
|
|
|
@unknown = GrandParent.new("unknown").dispatch
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def test_basic_per_key1
|
|
|
|
assert_equal %w(before1 before2 index), @index.log
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def test_basic_per_key2
|
|
|
|
assert_equal %w(before1 before2 update after2 after1), @update.log
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def test_basic_per_key3
|
|
|
|
assert_equal %w(delete after2 after1), @delete.log
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class InheritedCallbacksTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@index = Parent.new("index").dispatch
|
|
|
|
@update = Parent.new("update").dispatch
|
|
|
|
@delete = Parent.new("delete").dispatch
|
|
|
|
@unknown = Parent.new("unknown").dispatch
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def test_inherited_excluded
|
|
|
|
assert_equal %w(before1 index), @index.log
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def test_inherited_not_excluded
|
|
|
|
assert_equal %w(before1 before2 update after1), @update.log
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def test_partially_excluded
|
|
|
|
assert_equal %w(delete after2 after1), @delete.log
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class InheritedCallbacksTest2 < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@update1 = Child.new("update", :open).dispatch
|
|
|
|
@update2 = Child.new("update", :closed).dispatch
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def test_crazy_mix_on
|
|
|
|
assert_equal %w(before1 update after2 after1), @update1.log
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def test_crazy_mix_off
|
|
|
|
assert_equal %w(before1 before2 update after2 after1), @update2.log
|
|
|
|
end
|
2009-09-21 20:47:21 -04:00
|
|
|
end
|
2009-12-31 19:48:12 -05:00
|
|
|
|
|
|
|
class DynamicInheritedCallbacks < Test::Unit::TestCase
|
|
|
|
def test_callbacks_looks_to_the_superclass_before_running
|
|
|
|
child = EmptyChild.new.dispatch
|
|
|
|
assert !child.performed?
|
|
|
|
EmptyParent.set_callback :dispatch, :before, :perform!
|
|
|
|
child = EmptyChild.new.dispatch
|
|
|
|
assert child.performed?
|
|
|
|
end
|
|
|
|
end
|