From 8af6f1ccd45eedda73293a3fe28d61fdc15841fb Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 1 Jan 2018 06:18:39 +0900 Subject: Refactor to `Array(options[:on])` only once in defining validations --- activemodel/lib/active_model/validations/callbacks.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'activemodel/lib/active_model/validations/callbacks.rb') 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 -- cgit v1.2.3