From 2c8579516ede5e468a4ea3299e9a17a1acdd1c2e Mon Sep 17 00:00:00 2001 From: Subba Rao Pasupuleti Date: Tue, 3 Aug 2010 17:04:41 -0400 Subject: [PATCH] no callbacks should be created for empty array --- activemodel/lib/active_model/callbacks.rb | 9 ++++++--- activemodel/test/cases/callbacks_test.rb | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index 8c10c54..b150fc6 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -90,10 +90,13 @@ module ActiveModel # 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) diff --git a/activemodel/test/cases/callbacks_test.rb b/activemodel/test/cases/callbacks_test.rb index 9675b5d..64dc7b5 100644 --- a/activemodel/test/cases/callbacks_test.rb +++ b/activemodel/test/cases/callbacks_test.rb @@ -16,6 +16,8 @@ class CallbacksTest < ActiveModel::TestCase 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 @@ class CallbacksTest < ActiveModel::TestCase 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 -- 1.7.0.4