Avoid converting integer as a string into float

This commit is contained in:
namusyaka 2017-02-17 22:15:01 +09:00
parent 74689fda0c
commit b0be7792ad
No known key found for this signature in database
GPG Key ID: C4B6919318C291BC
3 changed files with 14 additions and 0 deletions

@ -1,3 +1,7 @@
* Avoid converting integer as a string into float.
*namusyaka*
* Remove deprecated behavior that halts callbacks when the return is false.
*Rafael Mendonça França*

@ -70,6 +70,7 @@ def is_number?(raw_value)
end
def parse_raw_value_as_a_number(raw_value)
return raw_value.to_i if is_integer?(raw_value)
Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/
end

@ -260,6 +260,15 @@ def test_validates_numericality_of_for_ruby_class
Person.clear_validators!
end
def test_validates_numericality_with_exponent_number
base = 10_000_000_000_000_000
Topic.validates_numericality_of :approved, less_than_or_equal_to: base
topic = Topic.new
topic.approved = (base + 1).to_s
assert topic.invalid?
end
def test_validates_numericality_with_invalid_args
assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, greater_than_or_equal_to: "foo" }
assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, less_than_or_equal_to: "foo" }