1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #28050 from namusyaka/avoid-converting-int-into-float

Avoid converting integer as a string into float
This commit is contained in:
Rafael Mendonça França 2017-03-27 18:23:56 -04:00
commit 9426bd7a19
No known key found for this signature in database
GPG key ID: FC23B6D0F1EEE948
3 changed files with 15 additions and 0 deletions

View file

@ -1 +1,6 @@
* Avoid converting integer as a string into float.
*namusyaka*
Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/activemodel/CHANGELOG.md) for previous changes.

View file

@ -70,6 +70,7 @@ module ActiveModel
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

View file

@ -260,6 +260,15 @@ class NumericalityValidationTest < ActiveModel::TestCase
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" }