aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2017-03-27 18:23:56 -0400
committerRafael Mendonça França <rafaelmfranca@gmail.com>2017-03-27 18:23:56 -0400
commit9426bd7a19ed3238fb976e6c243d41a28b184ff4 (patch)
treee79c7e81b59ab14874e5e56b1e0b7211d13bda1d /activemodel
parent3b474da4e6d623bf10b09bbb96b292c6dcf795af (diff)
parentb0be7792adf58e092b7f615ecbf3339ea70ee689 (diff)
downloadrails-9426bd7a19ed3238fb976e6c243d41a28b184ff4.tar.gz
rails-9426bd7a19ed3238fb976e6c243d41a28b184ff4.tar.bz2
rails-9426bd7a19ed3238fb976e6c243d41a28b184ff4.zip
Merge pull request #28050 from namusyaka/avoid-converting-int-into-float
Avoid converting integer as a string into float
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/CHANGELOG.md5
-rw-r--r--activemodel/lib/active_model/validations/numericality.rb1
-rw-r--r--activemodel/test/cases/validations/numericality_validation_test.rb9
3 files changed, 15 insertions, 0 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md
index 3bb5005275..6b9b3dd7ff 100644
--- a/activemodel/CHANGELOG.md
+++ b/activemodel/CHANGELOG.md
@@ -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.
diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb
index 30a9ef472d..995b331245 100644
--- a/activemodel/lib/active_model/validations/numericality.rb
+++ b/activemodel/lib/active_model/validations/numericality.rb
@@ -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
diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb
index a1be2de578..c0158e075f 100644
--- a/activemodel/test/cases/validations/numericality_validation_test.rb
+++ b/activemodel/test/cases/validations/numericality_validation_test.rb
@@ -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" }