mirror of
https://github.com/github/rails.git
synced 2026-01-09 14:48:01 -05:00
validates_inclusion_of and validates_exclusion_of now accept
`:within` option as alias of `:in` as documented. Fix #7118
This commit is contained in:
@@ -5,24 +5,27 @@ module ActiveModel
|
||||
module Validations
|
||||
class ExclusionValidator < EachValidator
|
||||
ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " <<
|
||||
"and must be supplied as the :in option of the configuration hash"
|
||||
"and must be supplied as the :in (or :within) option of the configuration hash"
|
||||
|
||||
def check_validity!
|
||||
unless [:include?, :call].any? { |method| options[:in].respond_to?(method) }
|
||||
unless [:include?, :call].any? { |method| delimiter.respond_to?(method) }
|
||||
raise ArgumentError, ERROR_MESSAGE
|
||||
end
|
||||
end
|
||||
|
||||
def validate_each(record, attribute, value)
|
||||
delimiter = options[:in]
|
||||
exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter
|
||||
if exclusions.send(inclusion_method(exclusions), value)
|
||||
record.errors.add(attribute, :exclusion, options.except(:in).merge!(:value => value))
|
||||
record.errors.add(attribute, :exclusion, options.except(:in, :within).merge!(:value => value))
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def delimiter
|
||||
@delimiter ||= options[:in] || options[:within]
|
||||
end
|
||||
|
||||
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible
|
||||
# values in the range for equality, so it may be slow for large ranges. The new
|
||||
# <tt>Range#cover?</tt> uses the previous logic of comparing a value with the
|
||||
@@ -49,6 +52,7 @@ module ActiveModel
|
||||
# part of. This can be supplied as a proc or lambda which returns an enumerable.
|
||||
# If the enumerable is a range the test is performed with <tt>Range#cover?</tt>
|
||||
# (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
|
||||
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
|
||||
# * <tt>:message</tt> - Specifies a custom error message (default is: "is reserved").
|
||||
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
|
||||
# is +nil+ (default is +false+).
|
||||
@@ -65,7 +69,7 @@ module ActiveModel
|
||||
# the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
|
||||
# or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
|
||||
# proc or string should return or evaluate to a true or false value.
|
||||
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
||||
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
||||
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
|
||||
def validates_exclusion_of(*attr_names)
|
||||
validates_with ExclusionValidator, _merge_attributes(attr_names)
|
||||
|
||||
@@ -5,24 +5,27 @@ module ActiveModel
|
||||
module Validations
|
||||
class InclusionValidator < EachValidator
|
||||
ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " <<
|
||||
"and must be supplied as the :in option of the configuration hash"
|
||||
"and must be supplied as the :in (or :within) option of the configuration hash"
|
||||
|
||||
def check_validity!
|
||||
unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) }
|
||||
unless [:include?, :call].any?{ |method| delimiter.respond_to?(method) }
|
||||
raise ArgumentError, ERROR_MESSAGE
|
||||
end
|
||||
end
|
||||
|
||||
def validate_each(record, attribute, value)
|
||||
delimiter = options[:in]
|
||||
exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter
|
||||
unless exclusions.send(inclusion_method(exclusions), value)
|
||||
record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value))
|
||||
record.errors.add(attribute, :inclusion, options.except(:in, :within).merge!(:value => value))
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def delimiter
|
||||
@delimiter ||= options[:in] || options[:within]
|
||||
end
|
||||
|
||||
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible
|
||||
# values in the range for equality, so it may be slow for large ranges. The new
|
||||
# <tt>Range#cover?</tt> uses the previous logic of comparing a value with the
|
||||
@@ -48,6 +51,7 @@ module ActiveModel
|
||||
# supplied as a proc or lambda which returns an enumerable. If the enumerable
|
||||
# is a range the test is performed with <tt>Range#cover?</tt>
|
||||
# (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
|
||||
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
|
||||
# * <tt>:message</tt> - Specifies a custom error message (default is: "is not
|
||||
# included in the list").
|
||||
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
|
||||
@@ -65,7 +69,7 @@ module ActiveModel
|
||||
# if the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
|
||||
# or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
|
||||
# proc or string should return or evaluate to a true or false value.
|
||||
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
||||
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
||||
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
|
||||
def validates_inclusion_of(*attr_names)
|
||||
validates_with InclusionValidator, _merge_attributes(attr_names)
|
||||
|
||||
@@ -28,6 +28,16 @@ class ExclusionValidationTest < ActiveModel::TestCase
|
||||
assert_equal ["option monkey is restricted"], t.errors[:title]
|
||||
end
|
||||
|
||||
def test_validates_exclusion_of_with_within_option
|
||||
Topic.validates_exclusion_of( :title, :within => %w( abe monkey ) )
|
||||
|
||||
assert Topic.new("title" => "something", "content" => "abc")
|
||||
|
||||
t = Topic.new("title" => "monkey")
|
||||
assert t.invalid?
|
||||
assert t.errors[:title].any?
|
||||
end
|
||||
|
||||
def test_validates_exclusion_of_for_ruby_class
|
||||
Person.validates_exclusion_of :karma, :in => %w( abe monkey )
|
||||
|
||||
|
||||
@@ -159,6 +159,17 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
# validates_inclusion_of using :within w/ mocha
|
||||
|
||||
COMMON_CASES.each do |name, validation_options, generate_message_options|
|
||||
test "validates_inclusion_of using :within on generated message #{name}" do
|
||||
Person.validates_inclusion_of :title, validation_options.merge(:within => %w(a b c))
|
||||
@person.title = 'z'
|
||||
@person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(:value => 'z'))
|
||||
@person.valid?
|
||||
end
|
||||
end
|
||||
|
||||
# validates_exclusion_of w/ mocha
|
||||
|
||||
COMMON_CASES.each do |name, validation_options, generate_message_options|
|
||||
@@ -170,6 +181,17 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
# validates_exclusion_of using :within w/ mocha
|
||||
|
||||
COMMON_CASES.each do |name, validation_options, generate_message_options|
|
||||
test "validates_exclusion_of using :within generated message #{name}" do
|
||||
Person.validates_exclusion_of :title, validation_options.merge(:within => %w(a b c))
|
||||
@person.title = 'a'
|
||||
@person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(:value => 'a'))
|
||||
@person.valid?
|
||||
end
|
||||
end
|
||||
|
||||
# validates_numericality_of without :only_integer w/ mocha
|
||||
|
||||
COMMON_CASES.each do |name, validation_options, generate_message_options|
|
||||
|
||||
@@ -60,6 +60,16 @@ class InclusionValidationTest < ActiveModel::TestCase
|
||||
assert_equal ["option uhoh is not in the list"], t.errors[:title]
|
||||
end
|
||||
|
||||
def test_validates_inclusion_of_with_within_option
|
||||
Topic.validates_inclusion_of( :title, :within => %w( a b c d e f g ) )
|
||||
|
||||
assert Topic.new("title" => "a", "content" => "abc").valid?
|
||||
|
||||
t = Topic.new("title" => "uhoh", "content" => "abc")
|
||||
assert t.invalid?
|
||||
assert t.errors[:title].any?
|
||||
end
|
||||
|
||||
def test_validates_inclusion_of_for_ruby_class
|
||||
Person.validates_inclusion_of :karma, :in => %w( abe monkey )
|
||||
|
||||
|
||||
Reference in New Issue
Block a user