From b0be7792adf58e092b7f615ecbf3339ea70ee689 Mon Sep 17 00:00:00 2001 From: namusyaka Date: Fri, 17 Feb 2017 22:15:01 +0900 Subject: [PATCH] Avoid converting integer as a string into float --- activemodel/CHANGELOG.md | 4 ++++ activemodel/lib/active_model/validations/numericality.rb | 1 + .../cases/validations/numericality_validation_test.rb | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index edaac8c7cde..a667e116b62 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -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* diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 30a9ef472d2..995b3312451 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 a1be2de5785..c0158e075fb 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" }