Skip to content

Commit

Permalink
Merge pull request #22610 from KevinSjoberg/feature/array-member-incl…
Browse files Browse the repository at this point in the history
…usion

Validate inclusion of each object in an array
  • Loading branch information
matthewd committed Aug 2, 2020
2 parents 11f54e1 + 5064970 commit 843898c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion activemodel/lib/active_model/validations/clusivity.rb
Expand Up @@ -24,7 +24,11 @@ def include?(record, value)
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
Expand Down
17 changes: 17 additions & 0 deletions activemodel/test/cases/validations/inclusion_validation_test.rb
Expand Up @@ -157,4 +157,21 @@ def p.available_karmas
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

0 comments on commit 843898c

Please sign in to comment.