提交 f8f43719 编写于 作者: S Subba Rao Pasupuleti 提交者: Xavier Noria

no callbacks should be created for empty array [#5289 state:resolved]

Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 b61ff257
......@@ -90,10 +90,13 @@ def self.extended(base)
#
def define_model_callbacks(*callbacks)
options = callbacks.extract_options!
options = { :terminator => "result == false", :scope => [:kind, :name] }.merge(options)
options = {
:terminator => "result == false",
:scope => [:kind, :name],
:only => [:before, :around, :after]
}.merge(options)
types = Array.wrap(options.delete(:only))
types = [:before, :around, :after] if types.empty?
types = Array.wrap(options.delete(:only))
callbacks.each do |callback|
define_callbacks(callback, options)
......
......@@ -16,6 +16,8 @@ class ModelCallbacks
define_model_callbacks :create
define_model_callbacks :initialize, :only => :after
define_model_callbacks :multiple, :only => [:before, :around]
define_model_callbacks :empty, :only => []
before_create :before_create
around_create CallbackValidator.new
......@@ -67,4 +69,16 @@ def create
assert !ModelCallbacks.respond_to?(:around_initialize)
assert_respond_to ModelCallbacks, :after_initialize
end
test "only selects which types of callbacks should be created from an array list" do
assert_respond_to ModelCallbacks, :before_multiple
assert_respond_to ModelCallbacks, :around_multiple
assert !ModelCallbacks.respond_to?(:after_multiple)
end
test "no callbacks should be created" do
assert !ModelCallbacks.respond_to?(:before_empty)
assert !ModelCallbacks.respond_to?(:around_empty)
assert !ModelCallbacks.respond_to?(:after_empty)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册