aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2019-01-22 15:33:54 -0500
committerGitHub <noreply@github.com>2019-01-22 15:33:54 -0500
commitea6a488f51f439854bf3c528e4225eb45f767d00 (patch)
treee976030770f2bfe896efa47afdf01860eb60e1e3 /activemodel
parent0815474d2f3d98e6d6844adaa771cf08ca1b5616 (diff)
parentf01e38509c442903edaf12f23749193ce1f2d1d9 (diff)
downloadrails-ea6a488f51f439854bf3c528e4225eb45f767d00.tar.gz
rails-ea6a488f51f439854bf3c528e4225eb45f767d00.tar.bz2
rails-ea6a488f51f439854bf3c528e4225eb45f767d00.zip
Merge pull request #35010 from Edouard-chin/ec-numericality-validator-fix
Fix NumericalityValidator on object responding to `to_f`:
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/validations/numericality.rb11
-rw-r--r--activemodel/test/cases/validations/numericality_validation_test.rb13
2 files changed, 16 insertions, 8 deletions
diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb
index c5997283ea..9cb8b543b0 100644
--- a/activemodel/lib/active_model/validations/numericality.rb
+++ b/activemodel/lib/active_model/validations/numericality.rb
@@ -10,7 +10,6 @@ module ActiveModel
RESERVED_OPTIONS = CHECKS.keys + [:only_integer]
INTEGER_REGEX = /\A[+-]?\d+\z/
- DECIMAL_REGEX = /\A[+-]?\d+\.?\d*(e|e[+-])?\d+\z/
def check_validity!
keys = CHECKS.keys - [:odd, :even]
@@ -92,8 +91,8 @@ module ActiveModel
raw_value
elsif is_integer?(raw_value)
raw_value.to_i
- elsif is_decimal?(raw_value) && !is_hexadecimal_literal?(raw_value)
- BigDecimal(raw_value)
+ elsif !is_hexadecimal_literal?(raw_value)
+ Kernel.Float(raw_value).to_d
end
end
@@ -101,12 +100,8 @@ module ActiveModel
INTEGER_REGEX.match?(raw_value.to_s)
end
- def is_decimal?(raw_value)
- DECIMAL_REGEX.match?(raw_value.to_s)
- end
-
def is_hexadecimal_literal?(raw_value)
- /\A0[xX]/.match?(raw_value)
+ /\A0[xX]/.match?(raw_value.to_s)
end
def filtered_options(value)
diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb
index eb4b02df93..fcdf123062 100644
--- a/activemodel/test/cases/validations/numericality_validation_test.rb
+++ b/activemodel/test/cases/validations/numericality_validation_test.rb
@@ -281,6 +281,19 @@ class NumericalityValidationTest < ActiveModel::TestCase
assert_predicate topic, :invalid?
end
+ def test_validates_numericalty_with_object_acting_as_numeric
+ klass = Class.new do
+ def to_f
+ 123.54
+ end
+ end
+
+ Topic.validates_numericality_of :price
+ topic = Topic.new(price: klass.new)
+
+ assert_predicate topic, :valid?
+ 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" }