提交 0421fb7a 编写于 作者: J José Valim

Refactor previous commit a bit [#4057 state:resolved]

上级 26392c4a
......@@ -61,6 +61,8 @@ module ActiveModel
class Errors < ActiveSupport::OrderedHash
include DeprecatedErrorMethods
CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank]
# Pass in the instance of the object that is using the errors object.
#
# class Person
......@@ -183,11 +185,12 @@ def to_xml(options={})
def add(attribute, message = nil, options = {})
message ||= :invalid
validation_conditionals = [:if, :unless, :on]
message = generate_message(attribute, message, options.except(*validation_conditionals)) if message.is_a?(Symbol)
if message.is_a?(Symbol)
message = generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS))
elsif message.is_a?(Proc)
message = message.call
end
message = message.call if message.is_a?(Proc)
self[attribute] << message
end
......
......@@ -10,7 +10,7 @@ def check_validity!
def validate_each(record, attribute, value)
if options[:in].include?(value)
record.errors.add(attribute, :exclusion, options.except(:in).merge(:value => value))
record.errors.add(attribute, :exclusion, options.except(:in).merge!(:value => value))
end
end
end
......
......@@ -5,9 +5,9 @@ module Validations
class FormatValidator < EachValidator
def validate_each(record, attribute, value)
if options[:with] && value.to_s !~ options[:with]
record.errors.add(attribute, :invalid, options.except(:with).merge(:value => value))
record.errors.add(attribute, :invalid, options.except(:with).merge!(:value => value))
elsif options[:without] && value.to_s =~ options[:without]
record.errors.add(attribute, :invalid, options.except(:with).merge(:value => value))
record.errors.add(attribute, :invalid, options.except(:without).merge!(:value => value))
end
end
......
......@@ -10,7 +10,7 @@ def check_validity!
def validate_each(record, attribute, value)
unless options[:in].include?(value)
record.errors.add(attribute, :inclusion, options.except(:in).merge(:value => value))
record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value))
end
end
end
......
......@@ -7,6 +7,7 @@ class LengthValidator < EachValidator
CHECKS = { :is => :==, :minimum => :>=, :maximum => :<= }.freeze
DEFAULT_TOKENIZER = lambda { |value| value.split(//) }
RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]
def initialize(options)
if range = (options.delete(:in) || options.delete(:within))
......@@ -50,9 +51,8 @@ def validate_each(record, attribute, value)
next if valid_value
reserved_options = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]
record.errors.add(attribute, MESSAGES[key],
options.except(*reserved_options).merge(:count => check_value))
options.except(*RESERVED_OPTIONS).merge!(:count => check_value))
end
end
end
......
......@@ -7,6 +7,8 @@ class NumericalityValidator < EachValidator
:equal_to => :==, :less_than => :<, :less_than_or_equal_to => :<=,
:odd => :odd?, :even => :even? }.freeze
RESERVED_OPTIONS = CHECKS.keys + [:only_integer]
def initialize(options)
super(options.reverse_merge(:only_integer => false, :allow_nil => false))
end
......@@ -76,10 +78,8 @@ def parse_raw_value_as_an_integer(raw_value)
end
def filtered_options(value)
reserved_options = [:allow_nil, :odd, :even, :not_an_integer, :only_integer, :allow_nil, :less_than]
options.except(*reserved_options).merge(:value => value)
options.except(*RESERVED_OPTIONS).merge!(:value => value)
end
end
module HelperMethods
......
module ActiveModel
# == Active Model validates_with method
module Validations
module HelperMethods
private
......@@ -11,7 +9,6 @@ def _merge_attributes(attr_names)
end
module ClassMethods
# Passes the record off to the class or classes specified and allows them
# to add errors based on more complex conditions.
#
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册