Permalink
Browse files

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

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent e82b38c commit ef0da581e94be1c938202cfa7ed39fb29614e570 @subbarao subbarao committed with josevalim Aug 3, 2010
Showing with 20 additions and 3 deletions.
  1. +6 −3 activemodel/lib/active_model/callbacks.rb
  2. +14 −0 activemodel/test/cases/callbacks_test.rb
@@ -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

0 comments on commit ef0da58

Please sign in to comment.