Skip to content
Browse files

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

  • Loading branch information...
1 parent 26392c4 commit 0421fb7a913c1c8a7e07a395106bbc65e75e9d45 @josevalim josevalim committed Jun 21, 2010
View
11 activemodel/lib/active_model/errors.rb
@@ -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
View
2 activemodel/lib/active_model/validations/exclusion.rb
@@ -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
View
4 activemodel/lib/active_model/validations/format.rb
@@ -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
View
2 activemodel/lib/active_model/validations/inclusion.rb
@@ -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
View
4 activemodel/lib/active_model/validations/length.rb
@@ -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
View
6 activemodel/lib/active_model/validations/numericality.rb
@@ -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
View
3 activemodel/lib/active_model/validations/with.rb
@@ -1,6 +1,4 @@
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.
#

0 comments on commit 0421fb7

Please sign in to comment.
Something went wrong with that request. Please try again.