From 64a05a928c50a770c6562ac1840772c5d6ad6f53 Mon Sep 17 00:00:00 2001 From: Robin Mehner Date: Sun, 22 Jun 2014 12:22:27 +0200 Subject: `only_integer` of `NumericalityValidator` now allows procs and symbols --- activemodel/lib/active_model/validations/numericality.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index a9fb9804d4..5bd162433d 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -30,7 +30,7 @@ module ActiveModel 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 @@ -75,6 +75,17 @@ module ActiveModel 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 @@ -121,6 +132,7 @@ module ActiveModel # * :equal_to # * :less_than # * :less_than_or_equal_to + # * :only_integer # # For example: # -- cgit v1.2.3