validates_numericality_of takes :greater_than, :greater_than_or_equal_to, :equal_to, :less_than, :less_than_or_equal_to, :odd, and :even options. Closes #3952.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6850 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2007-05-26 00:30:14 +00:00
parent 1f03c5111d
commit 64d655628c
3 changed files with 118 additions and 15 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* validates_numericality_of takes :greater_than, :greater_than_or_equal_to, :equal_to, :less_than, :less_than_or_equal_to, :odd, and :even options. #3952 [Bob Silva, Dan Kubb, Josh Peek]
* MySQL: create_database takes :charset and :collation options. Charset defaults to utf8. #8448 [matt]
* Find with a list of ids supports limit/offset. #8437 [hrudududu]

View File

@ -35,7 +35,14 @@ module ActiveRecord
:too_short => "is too short (minimum is %d characters)",
:wrong_length => "is the wrong length (should be %d characters)",
:taken => "has already been taken",
:not_a_number => "is not a number"
:not_a_number => "is not a number",
:greater_than => "must be greater than %d",
:greater_than_or_equal_to => "must be greater than or equal to %d",
:equal_to => "must be equal to %d",
:less_than => "must be less than %d",
:less_than_or_equal_to => "must be less than or equal to %d",
:odd => "must be odd",
:even => "must be even"
}
# Holds a hash with all the default error messages, such that they can be replaced by your own copy or localizations.
@ -298,6 +305,10 @@ module ActiveRecord
}.freeze
ALL_RANGE_OPTIONS = [ :is, :within, :in, :minimum, :maximum ].freeze
ALL_NUMERICALITY_CHECKS = { :greater_than => '>', :greater_than_or_equal_to => '>=',
:equal_to => '==', :less_than => '<', :less_than_or_equal_to => '<=',
:odd => 'odd?', :even => 'even?' }.freeze
def validate(*methods, &block)
methods << block if block_given?
@ -751,31 +762,57 @@ module ActiveRecord
# * <tt>on</tt> Specifies when this validation is active (default is :save, other options :create, :update)
# * <tt>only_integer</tt> Specifies whether the value has to be an integer, e.g. an integral value (default is false)
# * <tt>allow_nil</tt> Skip validation if attribute is nil (default is false). Notice that for fixnum and float columns empty strings are converted to nil
# * <tt>greater_than</tt> Specifies the value must be greater than the supplied value
# * <tt>greater_than_or_equal_to</tt> Specifies the value must be greater than or equal the supplied value
# * <tt>equal_to</tt> Specifies the value must be equal to the supplied value
# * <tt>less_than</tt> Specifies the value must be less than the supplied value
# * <tt>less_than_or_equal_to</tt> Specifies the value must be less than or equal the supplied value
# * <tt>odd</tt> Specifies the value must be an odd number
# * <tt>even</tt> Specifies the value must be an even number
# * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
# method, proc or string should return or evaluate to a true or false value.
def validates_numericality_of(*attr_names)
configuration = { :message => ActiveRecord::Errors.default_error_messages[:not_a_number], :on => :save,
:only_integer => false, :allow_nil => false }
configuration = { :on => :save, :only_integer => false, :allow_nil => false }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
if configuration[:only_integer]
validates_each(attr_names,configuration) do |record, attr_name,value|
record.errors.add(attr_name, configuration[:message]) unless record.send("#{attr_name}_before_type_cast").to_s =~ /\A[+-]?\d+\Z/
end
else
validates_each(attr_names,configuration) do |record, attr_name,value|
next if configuration[:allow_nil] and record.send("#{attr_name}_before_type_cast").nil?
begin
Kernel.Float(record.send("#{attr_name}_before_type_cast").to_s)
numericality_options = ALL_NUMERICALITY_CHECKS.keys & configuration.keys
(numericality_options - [ :odd, :even ]).each do |option|
raise ArgumentError, ":#{option} must be a number" unless configuration[option].is_a?(Numeric)
end
validates_each(attr_names,configuration) do |record, attr_name, value|
raw_value = record.send("#{attr_name}_before_type_cast") || value
next if configuration[:allow_nil] and raw_value.nil?
if configuration[:only_integer]
unless raw_value.to_s =~ /\A[+-]?\d+\Z/
record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.default_error_messages[:not_a_number])
next
end
raw_value = raw_value.to_i
else
begin
raw_value = Kernel.Float(raw_value.to_s)
rescue ArgumentError, TypeError
record.errors.add(attr_name, configuration[:message])
record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.default_error_messages[:not_a_number])
next
end
end
numericality_options.each do |option|
case option
when :odd, :even
record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.default_error_messages[option]) unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[]
else
record.errors.add(attr_name, configuration[:message] || (ActiveRecord::Errors.default_error_messages[option] % configuration[option])) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
end
end
end
end
# Creates an object just like Base.create but calls save! instead of save
# so an exception is raised if the record is invalid.
def create!(attributes = nil)

View File

@ -444,6 +444,13 @@ class ValidationsTest < Test::Unit::TestCase
assert Topic.create("title" => nil, "content" => "abc").valid?
end
def test_numericality_with_getter_method
Developer.validates_numericality_of( :salary )
developer = Developer.new("name" => "michael", "salary" => nil)
developer.instance_eval("def salary; read_attribute('salary') ? read_attribute('salary') : 100000; end")
assert developer.valid?
end
def test_numericality_with_allow_nil_and_getter_method
Developer.validates_numericality_of( :salary, :allow_nil => true)
developer = Developer.new("name" => "michael", "salary" => nil)
@ -1107,11 +1114,68 @@ class ValidatesNumericalityTest < Test::Unit::TestCase
valid!(NIL + INTEGERS)
end
def test_validates_numericality_with_greater_than
Topic.validates_numericality_of :approved, :greater_than => 10
invalid!([-10, 10], 'must be greater than 10')
valid!([11])
end
def test_validates_numericality_with_greater_than_or_equal
Topic.validates_numericality_of :approved, :greater_than_or_equal_to => 10
invalid!([-9, 9], 'must be greater than or equal to 10')
valid!([10])
end
def test_validates_numericality_with_equal_to
Topic.validates_numericality_of :approved, :equal_to => 10
invalid!([-10, 11], 'must be equal to 10')
valid!([10])
end
def test_validates_numericality_with_less_than
Topic.validates_numericality_of :approved, :less_than => 10
invalid!([10], 'must be less than 10')
valid!([-9, 9])
end
def test_validates_numericality_with_less_than_or_equal_to
Topic.validates_numericality_of :approved, :less_than_or_equal_to => 10
invalid!([11], 'must be less than or equal to 10')
valid!([-10, 10])
end
def test_validates_numericality_with_odd
Topic.validates_numericality_of :approved, :odd => true
invalid!([-2, 2], 'must be odd')
valid!([-1, 1])
end
def test_validates_numericality_with_even
Topic.validates_numericality_of :approved, :even => true
invalid!([-1, 1], 'must be even')
valid!([-2, 2])
end
def test_validates_numericality_with_greater_than_less_than_and_even
Topic.validates_numericality_of :approved, :greater_than => 1, :less_than => 4, :even => true
invalid!([1, 3, 4])
valid!([2])
end
private
def invalid!(values)
def invalid!(values, error=nil)
with_each_topic_approved_value(values) do |topic, value|
assert !topic.valid?, "#{value.inspect} not rejected as a number"
assert topic.errors.on(:approved)
assert_equal error, topic.errors.on(:approved) if error
end
end