rails/rails — pull request #8309 by durran (2012-11-25 10:41:17+00:00)
Title: Handle arrays with inclusion/exclusion validation.

There are cases where applications or frameworks that use Active Model
would like to validate that members of array fields include or exclude
specific values. This is natural given the language used in these
validations, but the current implementation is only one-sided:

"Validate that the attribute is any of these values (includes) or none
of these values (excludes)."

What this pull request does is expand this language to include:

"Validate that the attribute is any of or contains any of these values
(includes) or is not or does not include any of these values
(excludes)."

The added test cases show the expectations of what this means, but I will also
put examples here. First a model for the examples.

```ruby
class Code
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks

attr_accessor :letters, :numbers

def initialize(attributes)
attributes.each do |key, value|
send "#{key}=", value
end
end

# Validate that letters contains any of the values a, b, or c.
validates_inclusion_of :letters, :in => [ "a", "b", "c" ]

# Validate that numbers contains none of the values 1, 2, or 3.
validates_exclusion_of :numbers, :in => [ 1, 2, 3 ]
end
```

Expectations:

```ruby
Code.new(:letters => [ "a" ]).valid? #=> true
Code.new(:letters => [ "a", "b" ]).valid? #=> true
Code.new(:letters => [ "a", "c" ]).valid? #=> true
Code.new(:letters => [ "z", "c" ]).valid? #=> false

Code.new(:numbers => [ 1 ]).valid? #=> false
Code.new(:numbers => [ 1, 3 ]).valid? #=> false
Code.new(:numbers => [ 5, 6 ]).valid? #=> true
```

Both the `allow_nil` and `allow_blank` options would function as normal here as well.

Let me know what you think - I would love to have this functionality in core Active Model for Rails 4.