diff options
author | Robert Eshleman <c.robert.eshleman@gmail.com> | 2015-12-22 13:29:54 -0500 |
---|---|---|
committer | Robert Eshleman <c.robert.eshleman@gmail.com> | 2015-12-22 14:27:25 -0500 |
commit | 9c330798b0a77762b72a833519e10b01738cf79c (patch) | |
tree | 1af00c4ab5ef8c9ee7d1cda4b5170d446c5eb45c /activemodel/lib/active_model | |
parent | b96fdd234de9484b6c0999d04193de31d925dc3a (diff) | |
download | rails-9c330798b0a77762b72a833519e10b01738cf79c.tar.gz rails-9c330798b0a77762b72a833519e10b01738cf79c.tar.bz2 rails-9c330798b0a77762b72a833519e10b01738cf79c.zip |
Fix Regression in Numericality Validations
A regression (#22744) introduced in 7500dae caused certain numericality
validations to raise an error when run against an attribute with a
string value. Previously, these validations would successfully run
against string values because the value was cast to a numeric class.
This commit resolves the regression by converting string values to
floats before performing numericality comparison validations.
[fixes #22744]
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/validations/numericality.rb | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 9c1e8b4ba7..176a111bcc 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -39,6 +39,10 @@ module ActiveModel return end + if raw_value.is_a?(String) + value = parse_raw_value_as_a_number(raw_value) + end + options.slice(*CHECKS.keys).each do |option, option_value| case option when :odd, :even @@ -63,12 +67,15 @@ module ActiveModel protected def is_number?(raw_value) - parsed_value = Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/ - !parsed_value.nil? + !parse_raw_value_as_a_number(raw_value).nil? rescue ArgumentError, TypeError false end + def parse_raw_value_as_a_number(raw_value) + Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/ + end + def is_integer?(raw_value) /\A[+-]?\d+\z/ === raw_value.to_s end |