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:
commit
843898c57a
2 changed files with 22 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue