diff options
author | José Valim <jose.valim@gmail.com> | 2010-06-21 12:17:24 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-06-21 12:17:24 +0200 |
commit | 0421fb7a913c1c8a7e07a395106bbc65e75e9d45 (patch) | |
tree | 1f8c19f62541dc51be6c072d2d0cc8f904188af9 /activemodel/lib/active_model | |
parent | 26392c4ac57e27c63984d47c6326c13f502d5786 (diff) | |
download | rails-0421fb7a913c1c8a7e07a395106bbc65e75e9d45.tar.gz rails-0421fb7a913c1c8a7e07a395106bbc65e75e9d45.tar.bz2 rails-0421fb7a913c1c8a7e07a395106bbc65e75e9d45.zip |
Refactor previous commit a bit [#4057 state:resolved]
Diffstat (limited to 'activemodel/lib/active_model')
7 files changed, 16 insertions, 16 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index b4660f3587..d943e044a7 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/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 @@ module ActiveModel 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 diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index 6fe43c7219..4138892786 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -10,7 +10,7 @@ module ActiveModel 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 diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index 6f6933205f..104f403492 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -5,9 +5,9 @@ module ActiveModel 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 diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index 863f4da0ff..049b093618 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -10,7 +10,7 @@ module ActiveModel 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 diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index 77db437a33..c8a77ad666 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -7,6 +7,7 @@ module ActiveModel 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 @@ module ActiveModel 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 diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index dcc3befb08..b6aff7aa6b 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -7,6 +7,8 @@ module ActiveModel :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 @@ module ActiveModel 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 diff --git a/activemodel/lib/active_model/validations/with.rb b/activemodel/lib/active_model/validations/with.rb index a2e870d714..200efd4eb5 100644 --- a/activemodel/lib/active_model/validations/with.rb +++ b/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 @@ module ActiveModel end module ClassMethods - # Passes the record off to the class or classes specified and allows them # to add errors based on more complex conditions. # |