提交 511bf2a0 编写于 作者: D Diego Carrion 提交者: Santiago Pastorino

refactored ActiveModel::Validations::InclusionValidator#validate_each

[#6455 state:committed]
Signed-off-by: NSantiago Pastorino <santiago@wyeworks.com>
上级 b0da0dd8
require 'active_support/core_ext/range.rb'
module ActiveModel
# == Active Model Inclusion Validator
......@@ -8,27 +10,15 @@ def check_validity!
":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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册