Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit fe2d65864e6a89bfa9936ca7035b16e43cd3c394 1 parent 9df2279
@subbarao subbarao authored josevalim committed
View
9 activemodel/lib/active_model/callbacks.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)
View
14 activemodel/test/cases/callbacks_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.