diff options
author | Rolf Timmermans <r.timmermans@voormedia.com> | 2011-03-12 23:05:52 +0800 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-03-13 01:35:29 +0800 |
commit | 1a3fe8ce42e202630e6b1d8cf7137002e270ebdb (patch) | |
tree | ededececae28f3ca6369f53c64085fae9b0f9c60 | |
parent | 46f6a2e3889bae420589f429b09722a37dbdf18d (diff) | |
download | rails-1a3fe8ce42e202630e6b1d8cf7137002e270ebdb.tar.gz rails-1a3fe8ce42e202630e6b1d8cf7137002e270ebdb.tar.bz2 rails-1a3fe8ce42e202630e6b1d8cf7137002e270ebdb.zip |
Prevent callbacks in child classes from being executed more than once.
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 2 | ||||
-rw-r--r-- | activesupport/test/callback_inheritance_test.rb | 30 |
2 files changed, 31 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index a7a7836f03..418102352f 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -416,7 +416,7 @@ module ActiveSupport options = filters.last.is_a?(Hash) ? filters.pop : {} filters.unshift(block) if block - ([self] + ActiveSupport::DescendantsTracker.descendants(self)).each do |target| + ([self] + ActiveSupport::DescendantsTracker.descendants(self)).reverse.each do |target| chain = target.send("_#{name}_callbacks") yield target, chain.dup, type, filters, options target.__define_runner(name) 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 |