1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Handle frozen conditions in validate

Co-authored-by: Rafael França <rafael@franca.dev>
This commit is contained in:
alpaca-tc 2020-11-06 11:56:47 +09:00
parent 53e97f0fa0
commit 3d1cb9a1f3
2 changed files with 10 additions and 4 deletions

View file

@ -163,10 +163,10 @@ module ActiveModel
if options.key?(:on)
options = options.dup
options[:on] = Array(options[:on])
options[:if] = Array(options[:if])
options[:if].unshift ->(o) {
!(options[:on] & Array(o.validation_context)).empty?
}
options[:if] = [
->(o) { !(options[:on] & Array(o.validation_context)).empty? },
*options[:if]
]
end
set_callback(:validate, *args, options, &block)

View file

@ -159,6 +159,12 @@ class ValidationsTest < ActiveModel::TestCase
assert_equal ["will never be valid"], t.errors["title"]
end
def test_validates_with_array_condition_does_not_mutate_the_array
opts = []
Topic.validate(if: opts, on: :create) { }
assert_empty opts
end
def test_invalid_validator
Topic.validate :i_dont_exist
assert_raises(NoMethodError) do