mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Refactor to Array(options[:on])
only once in defining validations
This commit is contained in:
parent
bde0041f0a
commit
8af6f1ccd4
2 changed files with 13 additions and 11 deletions
|
@ -164,14 +164,14 @@ module ActiveModel
|
||||||
|
|
||||||
if options.key?(:on)
|
if options.key?(:on)
|
||||||
options = options.dup
|
options = options.dup
|
||||||
|
options[:on] = Array(options[:on])
|
||||||
options[:if] = Array(options[:if])
|
options[:if] = Array(options[:if])
|
||||||
options[:if].unshift ->(o) {
|
options[:if].unshift ->(o) {
|
||||||
!(Array(options[:on]) & Array(o.validation_context)).empty?
|
!(options[:on] & Array(o.validation_context)).empty?
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
args << options
|
set_callback(:validate, *args, options, &block)
|
||||||
set_callback(:validate, *args, &block)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# List all validators that are being used to validate the model using
|
# List all validators that are being used to validate the model using
|
||||||
|
|
|
@ -55,16 +55,17 @@ module ActiveModel
|
||||||
# person.name # => "bob"
|
# person.name # => "bob"
|
||||||
def before_validation(*args, &block)
|
def before_validation(*args, &block)
|
||||||
options = args.extract_options!
|
options = args.extract_options!
|
||||||
options[:if] = Array(options[:if])
|
|
||||||
|
|
||||||
if options.key?(:on)
|
if options.key?(:on)
|
||||||
|
options = options.dup
|
||||||
|
options[:on] = Array(options[:on])
|
||||||
|
options[:if] = Array(options[:if])
|
||||||
options[:if].unshift ->(o) {
|
options[:if].unshift ->(o) {
|
||||||
!(Array(options[:on]) & Array(o.validation_context)).empty?
|
!(options[:on] & Array(o.validation_context)).empty?
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
args << options
|
set_callback(:validation, :before, *args, options, &block)
|
||||||
set_callback(:validation, :before, *args, &block)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Defines a callback that will get called right after validation.
|
# Defines a callback that will get called right after validation.
|
||||||
|
@ -95,17 +96,18 @@ module ActiveModel
|
||||||
# person.status # => true
|
# person.status # => true
|
||||||
def after_validation(*args, &block)
|
def after_validation(*args, &block)
|
||||||
options = args.extract_options!
|
options = args.extract_options!
|
||||||
|
options = options.dup
|
||||||
options[:prepend] = true
|
options[:prepend] = true
|
||||||
options[:if] = Array(options[:if])
|
|
||||||
|
|
||||||
if options.key?(:on)
|
if options.key?(:on)
|
||||||
|
options[:on] = Array(options[:on])
|
||||||
|
options[:if] = Array(options[:if])
|
||||||
options[:if].unshift ->(o) {
|
options[:if].unshift ->(o) {
|
||||||
!(Array(options[:on]) & Array(o.validation_context)).empty?
|
!(options[:on] & Array(o.validation_context)).empty?
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
args << options
|
set_callback(:validation, :after, *args, options, &block)
|
||||||
set_callback(:validation, :after, *args, &block)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue