lib/active_model/validations/numericality.rb in activemodel-5.0.0.beta1.1 vs lib/active_model/validations/numericality.rb in activemodel-5.0.0.beta2
- old
+ new
@@ -37,10 +37,14 @@
if allow_only_integer?(record) && !is_integer?(raw_value)
record.errors.add(attr_name, :not_an_integer, filtered_options(raw_value))
return
end
+ unless raw_value.is_a?(Numeric)
+ value = parse_raw_value_as_a_number(raw_value)
+ end
+
options.slice(*CHECKS.keys).each do |option, option_value|
case option
when :odd, :even
unless value.to_i.send(CHECKS[option])
record.errors.add(attr_name, option, filtered_options(value))
@@ -61,13 +65,16 @@
end
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