Skip to content

Commit

Permalink
Allow Infinity (1.0/0.0) to pass validates_numericality_of. [#354 sta…
Browse files Browse the repository at this point in the history
…te:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
ctcherry authored and jeremy committed Jul 15, 2008
1 parent b32790c commit d126600
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/validations.rb
Expand Up @@ -855,7 +855,7 @@ def validates_numericality_of(*attr_names)
raw_value = raw_value.to_i
else
begin
raw_value = Kernel.Float(raw_value.to_s)
raw_value = Kernel.Float(raw_value)
rescue ArgumentError, TypeError
record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.default_error_messages[:not_a_number])
next
Expand Down
11 changes: 6 additions & 5 deletions activerecord/test/cases/validations_test.rb
Expand Up @@ -1391,6 +1391,7 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase
INTEGERS = [0, 10, -10] + INTEGER_STRINGS
BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) }
JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
INFINITY = [1.0/0.0]

def setup
Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
Expand All @@ -1402,27 +1403,27 @@ def test_default_validates_numericality_of
Topic.validates_numericality_of :approved

invalid!(NIL + BLANK + JUNK)
valid!(FLOATS + INTEGERS + BIGDECIMAL)
valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
end

def test_validates_numericality_of_with_nil_allowed
Topic.validates_numericality_of :approved, :allow_nil => true

invalid!(BLANK + JUNK)
valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL)
valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
end

def test_validates_numericality_of_with_integer_only
Topic.validates_numericality_of :approved, :only_integer => true

invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL)
invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY)
valid!(INTEGERS)
end

def test_validates_numericality_of_with_integer_only_and_nil_allowed
Topic.validates_numericality_of :approved, :only_integer => true, :allow_nil => true

invalid!(BLANK + JUNK + FLOATS + BIGDECIMAL)
invalid!(BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY)
valid!(NIL + INTEGERS)
end

Expand All @@ -1443,7 +1444,7 @@ def test_validates_numericality_with_greater_than_or_equal
def test_validates_numericality_with_equal_to
Topic.validates_numericality_of :approved, :equal_to => 10

invalid!([-10, 11], 'must be equal to 10')
invalid!([-10, 11] + INFINITY, 'must be equal to 10')
valid!([10])
end

Expand Down

0 comments on commit d126600

Please sign in to comment.