diff options
Diffstat (limited to 'activemodel/lib/active_model/validations/clusivity.rb')
-rw-r--r-- | activemodel/lib/active_model/validations/clusivity.rb | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/activemodel/lib/active_model/validations/clusivity.rb b/activemodel/lib/active_model/validations/clusivity.rb index b632a2bd6b..cf1415b6c2 100644 --- a/activemodel/lib/active_model/validations/clusivity.rb +++ b/activemodel/lib/active_model/validations/clusivity.rb @@ -2,12 +2,12 @@ require 'active_support/core_ext/range.rb' module ActiveModel module Validations - module Clusivity - ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " << - "and must be supplied as the :in option of the configuration hash" + module Clusivity #:nodoc: + ERROR_MESSAGE = "An object with the method #include? or a proc, lambda or symbol is required, " << + "and must be supplied as the :in (or :within) option of the configuration hash" def check_validity! - unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) } + unless delimiter.respond_to?(:include?) || delimiter.respond_to?(:call) || delimiter.respond_to?(:to_sym) raise ArgumentError, ERROR_MESSAGE end end @@ -15,11 +15,21 @@ module ActiveModel private def include?(record, value) - delimiter = options[:in] - exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter + exclusions = if delimiter.respond_to?(:call) + delimiter.call(record) + elsif delimiter.respond_to?(:to_sym) + record.send(delimiter) + else + delimiter + end + exclusions.send(inclusion_method(exclusions), value) end + def delimiter + @delimiter ||= options[:in] || options[:within] + end + # In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the # range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt> # uses the previous logic of comparing a value with the range endpoints. |