diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-01-01 06:18:39 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-01-01 06:18:39 +0900 |
commit | 8af6f1ccd45eedda73293a3fe28d61fdc15841fb (patch) | |
tree | 0edaac73db0116948d47350040711e3488a19e8b /activemodel/lib/active_model/validations | |
parent | bde0041f0a47a6b252852d7c49f4d3f75a29bc70 (diff) | |
download | rails-8af6f1ccd45eedda73293a3fe28d61fdc15841fb.tar.gz rails-8af6f1ccd45eedda73293a3fe28d61fdc15841fb.tar.bz2 rails-8af6f1ccd45eedda73293a3fe28d61fdc15841fb.zip |
Refactor to `Array(options[:on])` only once in defining validations
Diffstat (limited to 'activemodel/lib/active_model/validations')
-rw-r--r-- | activemodel/lib/active_model/validations/callbacks.rb | 18 |
1 files changed, 10 insertions, 8 deletions
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 |