Tidy up ActiveSupport::Callbacks::CallbackChain instance API.

This commit is contained in:
Joshua Peek 2008-04-17 23:30:01 -05:00
parent 82b4faf812
commit cf04e62127
6 changed files with 54 additions and 16 deletions

View File

@ -22,7 +22,7 @@ module ActionController
def to_prepare(identifier = nil, &block) def to_prepare(identifier = nil, &block)
@prepare_dispatch_callbacks ||= ActiveSupport::Callbacks::CallbackChain.new @prepare_dispatch_callbacks ||= ActiveSupport::Callbacks::CallbackChain.new
callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier) callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier)
@prepare_dispatch_callbacks.replace_or_append_callback(callback) @prepare_dispatch_callbacks | callback
end end
# If the block raises, send status code as a last-ditch response. # If the block raises, send status code as a last-ditch response.

View File

@ -265,7 +265,7 @@ module ActionController #:nodoc:
def skip_filter_in_chain(*filters, &test) def skip_filter_in_chain(*filters, &test)
filters, conditions = extract_options(filters) filters, conditions = extract_options(filters)
filters.each do |filter| filters.each do |filter|
if callback = find_callback(filter) then delete(callback) end if callback = find(filter) then delete(callback) end
end if conditions.empty? end if conditions.empty?
update_filter_in_chain(filters, :skip => conditions, &test) update_filter_in_chain(filters, :skip => conditions, &test)
end end
@ -302,7 +302,7 @@ module ActionController #:nodoc:
def find_or_create_filter(filter, filter_type, options = {}) def find_or_create_filter(filter, filter_type, options = {})
update_filter_in_chain([filter], options) update_filter_in_chain([filter], options)
if found_filter = find_callback(filter) { |f| f.type == filter_type } if found_filter = find(filter) { |f| f.type == filter_type }
found_filter found_filter
else else
filter_kind = case filter_kind = case
@ -326,7 +326,7 @@ module ActionController #:nodoc:
end end
def update_filter_in_chain(filters, options, &test) def update_filter_in_chain(filters, options, &test)
filters.map! { |f| block_given? ? find_callback(f, &test) : find_callback(f) } filters.map! { |f| block_given? ? find(f, &test) : find(f) }
filters.compact! filters.compact!
map! do |filter| map! do |filter|

View File

@ -7,8 +7,7 @@ module ActiveModel
%w( validate validate_on_create validate_on_update ).each do |validation_method| %w( validate validate_on_create validate_on_update ).each do |validation_method|
base.class_eval <<-"end_eval" base.class_eval <<-"end_eval"
def self.#{validation_method}(*methods, &block) def self.#{validation_method}(*methods, &block)
methods = CallbackChain.build(:#{validation_method}, *methods, &block) self.#{validation_method}_callback_chain | CallbackChain.build(:#{validation_method}, *methods, &block)
self.#{validation_method}_callback_chain.replace(#{validation_method}_callback_chain | methods)
end end
def self.#{validation_method}_callback_chain def self.#{validation_method}_callback_chain

View File

@ -285,8 +285,7 @@ module ActiveRecord
VALIDATIONS.each do |validation_method| VALIDATIONS.each do |validation_method|
base.class_eval <<-"end_eval" base.class_eval <<-"end_eval"
def self.#{validation_method}(*methods, &block) def self.#{validation_method}(*methods, &block)
methods = CallbackChain.build(:#{validation_method}, *methods, &block) self.#{validation_method}_callback_chain | CallbackChain.build(:#{validation_method}, *methods, &block)
self.#{validation_method}_callback_chain.replace(#{validation_method}_callback_chain | methods)
end end
def self.#{validation_method}_callback_chain def self.#{validation_method}_callback_chain

View File

@ -96,17 +96,26 @@ module ActiveSupport
end end
end end
def find_callback(callback, &block) def |(chain)
if chain.is_a?(Callback)
if found_callback = find(chain)
index = index(found_callback)
self[index] = chain
else
self << chain
end
else
chain.each { |callback| self | callback }
end
self
end
def find(callback, &block)
select { |c| c == callback && (!block_given? || yield(c)) }.first select { |c| c == callback && (!block_given? || yield(c)) }.first
end end
def replace_or_append_callback(callback) def delete(callback)
if found_callback = find_callback(callback) super(find(callback))
index = index(found_callback)
self[index] = callback
else
self << callback
end
end end
private private

View File

@ -96,6 +96,8 @@ class ConditionalCallbackTest < Test::Unit::TestCase
end end
class CallbackTest < Test::Unit::TestCase class CallbackTest < Test::Unit::TestCase
include ActiveSupport::Callbacks
def test_eql def test_eql
callback = Callback.new(:before, :save, :identifier => :lifesaver) callback = Callback.new(:before, :save, :identifier => :lifesaver)
assert callback.eql?(Callback.new(:before, :save, :identifier => :lifesaver)) assert callback.eql?(Callback.new(:before, :save, :identifier => :lifesaver))
@ -115,3 +117,32 @@ class CallbackTest < Test::Unit::TestCase
assert_equal({}, a.options) assert_equal({}, a.options)
end end
end end
class CallbackChainTest < Test::Unit::TestCase
include ActiveSupport::Callbacks
def setup
@chain = CallbackChain.build(:make, :bacon, :lettuce, :tomato)
end
def test_build
assert_equal 3, @chain.size
assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method)
end
def test_find
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)
end
def test_delete
assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method)
@chain.delete(:bacon)
assert_equal [:lettuce, :tomato], @chain.map(&:method)
end
end