aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/callbacks.rb21
-rw-r--r--activesupport/test/callbacks_test.rb8
2 files changed, 17 insertions, 12 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb
index 9c59b7ac76..7b905930bb 100644
--- a/activesupport/lib/active_support/callbacks.rb
+++ b/activesupport/lib/active_support/callbacks.rb
@@ -96,15 +96,12 @@ module ActiveSupport
end
end
- def |(chain)
- if chain.is_a?(CallbackChain)
- chain.each { |callback| self | callback }
+ # TODO: Decompose into more Array like behavior
+ def replace_or_append!(chain)
+ if index = index(chain)
+ self[index] = chain
else
- if (found_callback = find(chain)) && (index = index(chain))
- self[index] = chain
- else
- self << chain
- end
+ self << chain
end
self
end
@@ -157,6 +154,14 @@ module ActiveSupport
self.class.new(@kind, @method, @options.dup)
end
+ def hash
+ if @identifier
+ @identifier.hash
+ else
+ @method.hash
+ end
+ end
+
def call(*args, &block)
evaluate_method(method, *args, &block) if should_run_callback?(*args)
rescue LocalJumpError
diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb
index 7f71ca2262..25b8eecef5 100644
--- a/activesupport/test/callbacks_test.rb
+++ b/activesupport/test/callbacks_test.rb
@@ -134,10 +134,10 @@ class CallbackChainTest < Test::Unit::TestCase
assert_equal :bacon, @chain.find(:bacon).method
end
- def test_union
- assert_equal [:bacon, :lettuce, :tomato], (@chain | Callback.new(:make, :bacon)).map(&:method)
- assert_equal [:bacon, :lettuce, :tomato, :turkey], (@chain | CallbackChain.build(:make, :bacon, :lettuce, :tomato, :turkey)).map(&:method)
- assert_equal [:bacon, :lettuce, :tomato, :turkey, :mayo], (@chain | Callback.new(:make, :mayo)).map(&:method)
+ def test_replace_or_append
+ assert_equal [:bacon, :lettuce, :tomato], (@chain.replace_or_append!(Callback.new(:make, :bacon))).map(&:method)
+ assert_equal [:bacon, :lettuce, :tomato, :turkey], (@chain.replace_or_append!(Callback.new(:make, :turkey))).map(&:method)
+ assert_equal [:bacon, :lettuce, :tomato, :turkey, :mayo], (@chain.replace_or_append!(Callback.new(:make, :mayo))).map(&:method)
end
def test_delete