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

Merge pull request #22610 from KevinSjoberg/feature/array-member-inclusion

Validate inclusion of each object in an array
This commit is contained in:
Matthew Draper 2020-08-03 00:09:35 +09:30 committed by GitHub
commit 843898c57a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View file

@ -24,7 +24,11 @@ module ActiveModel
delimiter
end
members.send(inclusion_method(members), value)
if value.is_a?(Array)
value.all? { |v| members.send(inclusion_method(members), v) }
else
members.send(inclusion_method(members), value)
end
end
def delimiter

View file

@ -157,4 +157,21 @@ class InclusionValidationTest < ActiveModel::TestCase
ensure
Person.clear_validators!
end
def test_validates_inclusion_of_with_array_value
Person.validates_inclusion_of :karma, in: %w( abe monkey )
p = Person.new
p.karma = %w(Lifo monkey)
assert p.invalid?
assert_equal ["is not included in the list"], p.errors[:karma]
p = Person.new
p.karma = %w(abe monkey)
assert p.valid?
ensure
Person.clear_validators!
end
end