lib/active_model/validations/numericality.rb in activemodel-4.1.16 vs lib/active_model/validations/numericality.rb in activemodel-4.2.0.beta1

- old
+ new

@@ -28,11 +28,11 @@ unless value = parse_raw_value_as_a_number(raw_value) record.errors.add(attr_name, :not_a_number, filtered_options(raw_value)) return end - if options[:only_integer] + if allow_only_integer?(record) unless value = parse_raw_value_as_an_integer(raw_value) record.errors.add(attr_name, :not_an_integer, filtered_options(raw_value)) return end end @@ -73,10 +73,21 @@ def filtered_options(value) filtered = options.except(*RESERVED_OPTIONS) filtered[:value] = value filtered end + + def allow_only_integer?(record) + case options[:only_integer] + when Symbol + record.send(options[:only_integer]) + when Proc + options[:only_integer].call(record) + else + options[:only_integer] + end + end end module HelperMethods # Validates whether the value of the specified attribute is numeric by # trying to convert it to a float with Kernel.Float (if <tt>only_integer</tt> @@ -119,9 +130,10 @@ # * <tt>:greater_than</tt> # * <tt>:greater_than_or_equal_to</tt> # * <tt>:equal_to</tt> # * <tt>:less_than</tt> # * <tt>:less_than_or_equal_to</tt> + # * <tt>:only_integer</tt> # # For example: # # class Person < ActiveRecord::Base # validates_numericality_of :width, less_than: ->(person) { person.height }