diff options
author | Diego Carrion <dc.rec1@gmail.com> | 2011-02-19 21:13:33 -0200 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2011-02-19 23:24:38 -0200 |
commit | 511bf2a0505309c65bbf2ad9394e3c548762ea5d (patch) | |
tree | d98efa3927c75a9b1f527f17e7336596cd43e793 /activemodel | |
parent | b0da0dd8345b68a4e54c52e398cc65bbc0c4fbb5 (diff) | |
download | rails-511bf2a0505309c65bbf2ad9394e3c548762ea5d.tar.gz rails-511bf2a0505309c65bbf2ad9394e3c548762ea5d.tar.bz2 rails-511bf2a0505309c65bbf2ad9394e3c548762ea5d.zip |
refactored ActiveModel::Validations::InclusionValidator#validate_each
[#6455 state:committed]
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/validations/inclusion.rb | 26 |
1 files changed, 8 insertions, 18 deletions
diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index 108586b8df..b0903a6a2c 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/range.rb' + module ActiveModel # == Active Model Inclusion Validator @@ -8,27 +10,15 @@ module ActiveModel ":in option of the configuration hash" unless options[:in].respond_to?(:include?) end + def validate_each(record, attribute, value) + record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value)) unless options[:in].send(include?, value) + end + # On Ruby 1.9 Range#include? checks all possible values in the range for equality, # so it may be slow for large ranges. The new Range#cover? uses the previous logic # of comparing a value with the range endpoints. - if (1..2).respond_to?(:cover?) - def validate_each(record, attribute, value) - included = if options[:in].is_a?(Range) - options[:in].cover?(value) - else - options[:in].include?(value) - end - - unless included - record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value)) - end - end - else - def validate_each(record, attribute, value) - unless options[:in].include?(value) - record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value)) - end - end + def include? + options[:in].is_a?(Range) ? :cover? : :include? end end |