From 1a3fe8ce42e202630e6b1d8cf7137002e270ebdb Mon Sep 17 00:00:00 2001 From: Rolf Timmermans Date: Sat, 12 Mar 2011 23:05:52 +0800 Subject: Prevent callbacks in child classes from being executed more than once. --- activesupport/test/callback_inheritance_test.rb | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/callback_inheritance_test.rb b/activesupport/test/callback_inheritance_test.rb index 71249050fc..d569cbb4fb 100644 --- a/activesupport/test/callback_inheritance_test.rb +++ b/activesupport/test/callback_inheritance_test.rb @@ -82,6 +82,30 @@ class EmptyChild < EmptyParent end end +class CountingParent + include ActiveSupport::Callbacks + + attr_reader :count + + define_callbacks :dispatch + + def initialize + @count = 0 + end + + def count! + @count += 1 + end + + def dispatch + run_callbacks(:dispatch) + self + end +end + +class CountingChild < CountingParent +end + class BasicCallbacksTest < Test::Unit::TestCase def setup @index = GrandParent.new("index").dispatch @@ -147,4 +171,10 @@ class DynamicInheritedCallbacks < Test::Unit::TestCase child = EmptyChild.new.dispatch assert child.performed? end + + 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 end -- cgit v1.2.3