aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-01-01 06:18:39 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-01-01 06:18:39 +0900
commit8af6f1ccd45eedda73293a3fe28d61fdc15841fb (patch)
tree0edaac73db0116948d47350040711e3488a19e8b /activemodel
parentbde0041f0a47a6b252852d7c49f4d3f75a29bc70 (diff)
downloadrails-8af6f1ccd45eedda73293a3fe28d61fdc15841fb.tar.gz
rails-8af6f1ccd45eedda73293a3fe28d61fdc15841fb.tar.bz2
rails-8af6f1ccd45eedda73293a3fe28d61fdc15841fb.zip
Refactor to `Array(options[:on])` only once in defining validations
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/validations.rb6
-rw-r--r--activemodel/lib/active_model/validations/callbacks.rb18
2 files changed, 13 insertions, 11 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index cdf11d190f..7f14d102dd 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -164,14 +164,14 @@ module ActiveModel
if options.key?(:on)
options = options.dup
+ options[:on] = Array(options[:on])
options[:if] = Array(options[:if])
options[:if].unshift ->(o) {
- !(Array(options[:on]) & Array(o.validation_context)).empty?
+ !(options[:on] & Array(o.validation_context)).empty?
}
end
- args << options
- set_callback(:validate, *args, &block)
+ set_callback(:validate, *args, options, &block)
end
# List all validators that are being used to validate the model using
diff --git a/activemodel/lib/active_model/validations/callbacks.rb b/activemodel/lib/active_model/validations/callbacks.rb
index 11a8b2b229..887d31ae2a 100644
--- a/activemodel/lib/active_model/validations/callbacks.rb
+++ b/activemodel/lib/active_model/validations/callbacks.rb
@@ -55,16 +55,17 @@ module ActiveModel
# person.name # => "bob"
def before_validation(*args, &block)
options = args.extract_options!
- options[:if] = Array(options[:if])
if options.key?(:on)
+ options = options.dup
+ options[:on] = Array(options[:on])
+ options[:if] = Array(options[:if])
options[:if].unshift ->(o) {
- !(Array(options[:on]) & Array(o.validation_context)).empty?
+ !(options[:on] & Array(o.validation_context)).empty?
}
end
- args << options
- set_callback(:validation, :before, *args, &block)
+ set_callback(:validation, :before, *args, options, &block)
end
# Defines a callback that will get called right after validation.
@@ -95,17 +96,18 @@ module ActiveModel
# person.status # => true
def after_validation(*args, &block)
options = args.extract_options!
+ options = options.dup
options[:prepend] = true
- options[:if] = Array(options[:if])
if options.key?(:on)
+ options[:on] = Array(options[:on])
+ options[:if] = Array(options[:if])
options[:if].unshift ->(o) {
- !(Array(options[:on]) & Array(o.validation_context)).empty?
+ !(options[:on] & Array(o.validation_context)).empty?
}
end
- args << options
- set_callback(:validation, :after, *args, &block)
+ set_callback(:validation, :after, *args, options, &block)
end
end