2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2009-02-27 22:25:45 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
module CallbackInheritanceTestFixtures
|
|
|
|
class GrandParent
|
|
|
|
include ActiveSupport::Callbacks
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
attr_reader :log, :action_name
|
|
|
|
def initialize(action_name)
|
|
|
|
@action_name, @log = action_name, []
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
define_callbacks :dispatch
|
|
|
|
set_callback :dispatch, :before, :before1, :before2, if: proc { |c| c.action_name == "index" || c.action_name == "update" }
|
|
|
|
set_callback :dispatch, :after, :after1, :after2, if: proc { |c| c.action_name == "update" || c.action_name == "delete" }
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def before1
|
|
|
|
@log << "before1"
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def before2
|
|
|
|
@log << "before2"
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def after1
|
|
|
|
@log << "after1"
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def after2
|
|
|
|
@log << "after2"
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def dispatch
|
|
|
|
run_callbacks :dispatch do
|
|
|
|
@log << action_name
|
|
|
|
end
|
|
|
|
self
|
2009-02-27 22:25:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
class Parent < GrandParent
|
|
|
|
skip_callback :dispatch, :before, :before2, unless: proc { |c| c.action_name == "update" }
|
|
|
|
skip_callback :dispatch, :after, :after2, unless: proc { |c| c.action_name == "delete" }
|
|
|
|
end
|
2009-02-27 22:25:45 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
class Child < GrandParent
|
|
|
|
skip_callback :dispatch, :before, :before2, unless: proc { |c| c.action_name == "update" }, if: :state_open?
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def state_open?
|
|
|
|
@state == :open
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def initialize(action_name, state)
|
|
|
|
super(action_name)
|
|
|
|
@state = state
|
|
|
|
end
|
2009-02-27 22:25:45 -05:00
|
|
|
end
|
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
class EmptyParent
|
|
|
|
include ActiveSupport::Callbacks
|
2009-12-31 19:48:12 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def performed?
|
|
|
|
@performed ||= false
|
|
|
|
end
|
2009-12-31 19:48:12 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
define_callbacks :dispatch
|
2009-12-31 19:48:12 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def perform!
|
|
|
|
@performed = true
|
|
|
|
end
|
2009-12-31 19:48:12 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def dispatch
|
|
|
|
run_callbacks :dispatch
|
|
|
|
self
|
|
|
|
end
|
2009-12-31 19:48:12 -05:00
|
|
|
end
|
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
class EmptyChild < EmptyParent
|
|
|
|
set_callback :dispatch, :before, :do_nothing
|
2009-12-31 19:48:12 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def do_nothing
|
|
|
|
end
|
2009-12-31 19:48:12 -05:00
|
|
|
end
|
2009-02-27 22:25:45 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
class CountingParent
|
|
|
|
include ActiveSupport::Callbacks
|
2011-03-12 10:05:52 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
attr_reader :count
|
2011-03-12 10:05:52 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
define_callbacks :dispatch
|
2011-03-12 10:05:52 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def initialize
|
|
|
|
@count = 0
|
|
|
|
end
|
2011-03-12 10:05:52 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def count!
|
|
|
|
@count += 1
|
|
|
|
end
|
2011-03-12 10:05:52 -05:00
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
def dispatch
|
|
|
|
run_callbacks(:dispatch)
|
|
|
|
self
|
|
|
|
end
|
2011-03-12 10:05:52 -05:00
|
|
|
end
|
|
|
|
|
2021-08-22 16:04:24 -04:00
|
|
|
class CountingChild < CountingParent
|
|
|
|
end
|
2011-03-12 10:05:52 -05:00
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class BasicCallbacksTest < ActiveSupport::TestCase
|
2021-08-22 16:04:24 -04:00
|
|
|
include CallbackInheritanceTestFixtures
|
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def setup
|
2021-08-22 18:49:12 -04:00
|
|
|
@index = GrandParent.new("index").dispatch
|
|
|
|
@update = GrandParent.new("update").dispatch
|
|
|
|
@delete = GrandParent.new("delete").dispatch
|
2009-02-27 22:25:45 -05:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2012-02-04 06:31:00 -05:00
|
|
|
def test_basic_conditional_callback1
|
2009-02-27 22:25:45 -05:00
|
|
|
assert_equal %w(before1 before2 index), @index.log
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2012-02-04 06:31:00 -05:00
|
|
|
def test_basic_conditional_callback2
|
2009-02-27 22:25:45 -05:00
|
|
|
assert_equal %w(before1 before2 update after2 after1), @update.log
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2012-02-04 06:31:00 -05:00
|
|
|
def test_basic_conditional_callback3
|
2009-02-27 22:25:45 -05:00
|
|
|
assert_equal %w(delete after2 after1), @delete.log
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class InheritedCallbacksTest < ActiveSupport::TestCase
|
2021-08-22 16:04:24 -04:00
|
|
|
include CallbackInheritanceTestFixtures
|
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def setup
|
|
|
|
@index = Parent.new("index").dispatch
|
|
|
|
@update = Parent.new("update").dispatch
|
|
|
|
@delete = Parent.new("delete").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
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class InheritedCallbacksTest2 < ActiveSupport::TestCase
|
2021-08-22 16:04:24 -04:00
|
|
|
include CallbackInheritanceTestFixtures
|
|
|
|
|
2009-02-27 22:25:45 -05:00
|
|
|
def setup
|
|
|
|
@update1 = Child.new("update", :open).dispatch
|
|
|
|
@update2 = Child.new("update", :closed).dispatch
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-10-05 22:27:09 -04:00
|
|
|
def test_complex_mix_on
|
2009-02-27 22:25:45 -05:00
|
|
|
assert_equal %w(before1 update after2 after1), @update1.log
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2021-10-05 22:27:09 -04:00
|
|
|
def test_complex_mix_off
|
2009-02-27 22:25:45 -05:00
|
|
|
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
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class DynamicInheritedCallbacks < ActiveSupport::TestCase
|
2021-08-22 16:04:24 -04:00
|
|
|
include CallbackInheritanceTestFixtures
|
|
|
|
|
2009-12-31 19:48:12 -05:00
|
|
|
def test_callbacks_looks_to_the_superclass_before_running
|
|
|
|
child = EmptyChild.new.dispatch
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate child, :performed?
|
2009-12-31 19:48:12 -05:00
|
|
|
EmptyParent.set_callback :dispatch, :before, :perform!
|
|
|
|
child = EmptyChild.new.dispatch
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate child, :performed?
|
2009-12-31 19:48:12 -05:00
|
|
|
end
|
2011-03-12 10:05:52 -05:00
|
|
|
|
|
|
|
def test_callbacks_should_be_performed_once_in_child_class
|
|
|
|
CountingParent.set_callback(:dispatch, :before) { count! }
|
|
|
|
child = CountingChild.new.dispatch
|
|
|
|
assert_equal 1, child.count
|
|
|
|
end
|
2009-12-31 19:48:12 -05:00
|
|
|
end
|
2018-02-06 14:53:26 -05:00
|
|
|
|
|
|
|
class DynamicDefinedCallbacks < ActiveSupport::TestCase
|
2021-08-22 16:04:24 -04:00
|
|
|
include CallbackInheritanceTestFixtures
|
|
|
|
|
2018-02-06 14:53:26 -05:00
|
|
|
def test_callbacks_should_be_performed_once_in_child_class_after_dynamic_define
|
|
|
|
GrandParent.define_callbacks(:foo)
|
|
|
|
GrandParent.set_callback(:foo, :before, :before1)
|
|
|
|
parent = Parent.new("foo")
|
|
|
|
parent.run_callbacks(:foo)
|
|
|
|
assert_equal %w(before1), parent.log
|
|
|
|
end
|
|
|
|
end
|