提交 ddaeb4b6 编写于 作者: S Santiago Pastorino

Merge pull request #5643 from rafaelfranca/remove-duplication

Remove code duplication in InclusionValidator and ExclusionValidator.
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"
def check_validity!
unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) }
raise ArgumentError, ERROR_MESSAGE
end
end
private
def include?(record, value)
delimiter = options[:in]
exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter
exclusions.send(inclusion_method(exclusions), value)
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.
def inclusion_method(enumerable)
enumerable.is_a?(Range) ? :cover? : :include?
end
end
end
end
require 'active_support/core_ext/range'
require "active_model/validations/clusivity"
module ActiveModel
# == Active Model Exclusion Validator
module Validations
class ExclusionValidator < EachValidator
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"
def check_validity!
unless [:include?, :call].any? { |method| options[:in].respond_to?(method) }
raise ArgumentError, ERROR_MESSAGE
end
end
include Clusivity
def validate_each(record, attribute, value)
delimiter = options[:in]
exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter
if exclusions.send(inclusion_method(exclusions), value)
if include?(record, value)
record.errors.add(attribute, :exclusion, options.except(:in).merge!(:value => value))
end
end
private
# 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.
def inclusion_method(enumerable)
enumerable.is_a?(Range) ? :cover? : :include?
end
end
module HelperMethods
......
require 'active_support/core_ext/range'
require "active_model/validations/clusivity"
module ActiveModel
# == Active Model Inclusion Validator
module Validations
class InclusionValidator < EachValidator
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"
def check_validity!
unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) }
raise ArgumentError, ERROR_MESSAGE
end
end
include Clusivity
def validate_each(record, attribute, value)
delimiter = options[:in]
exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter
unless exclusions.send(inclusion_method(exclusions), value)
unless include?(record, value)
record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value))
end
end
private
# 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.
def inclusion_method(enumerable)
enumerable.is_a?(Range) ? :cover? : :include?
end
end
module HelperMethods
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册