From 28f36279cd484dc66b8b0ca2f0c3d75fd9ac631c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 8 Jun 2009 20:32:08 -0500 Subject: [PATCH] Properly require ActiveModel validation dependencies --- .../lib/active_model/deprecated_error_methods.rb | 4 ++-- activemodel/lib/active_model/errors.rb | 16 +++++++++------- activemodel/lib/active_model/validations.rb | 10 ++++++---- .../lib/active_model/validations/acceptance.rb | 4 ++-- .../lib/active_model/validations/confirmation.rb | 4 ++-- .../lib/active_model/validations/exclusion.rb | 4 ++-- .../lib/active_model/validations/format.rb | 4 ++-- .../lib/active_model/validations/inclusion.rb | 4 ++-- .../lib/active_model/validations/length.rb | 4 ++-- .../lib/active_model/validations/numericality.rb | 4 ++-- .../lib/active_model/validations/presence.rb | 6 ++++-- 11 files changed, 35 insertions(+), 29 deletions(-) diff --git a/activemodel/lib/active_model/deprecated_error_methods.rb b/activemodel/lib/active_model/deprecated_error_methods.rb index 433de8931a7e6..dd8050c549e1b 100644 --- a/activemodel/lib/active_model/deprecated_error_methods.rb +++ b/activemodel/lib/active_model/deprecated_error_methods.rb @@ -19,7 +19,7 @@ def add_to_base(msg) ActiveSupport::Deprecation.warn "Errors#add_to_base(msg) has been deprecated, use Errors#[:base] << msg instead" self[:base] << msg end - + def invalid?(attribute) ActiveSupport::Deprecation.warn "Errors#invalid?(attribute) has been deprecated, use Errors#[attribute].any? instead" self[attribute].any? @@ -30,4 +30,4 @@ def each_full to_a.each { |error| yield error } end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 4be91d0505de6..61bf4a81b8aaa 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/string/inflections' + module ActiveModel class Errors < Hash include DeprecatedErrorMethods @@ -23,7 +25,7 @@ def []=(attribute, error) end def each - each_key do |attribute| + each_key do |attribute| self[attribute].each { |error| yield attribute, error } end end @@ -111,15 +113,15 @@ def full_messages(options = {}) end # Translates an error message in it's default scope (activemodel.errrors.messages). - # Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, if it's not there, - # it's looked up in models.MODEL.MESSAGE and if that is not there it returns the translation of the - # default message (e.g. activemodel.errors.messages.MESSAGE). The translated model name, + # Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, if it's not there, + # it's looked up in models.MODEL.MESSAGE and if that is not there it returns the translation of the + # default message (e.g. activemodel.errors.messages.MESSAGE). The translated model name, # translated attribute name and the value are available for interpolation. # # When using inheritence in your models, it will check all the inherited models too, but only if the model itself # hasn't been found. Say you have class Admin < User; end and you wanted the translation for the :blank # error +message+ for the title +attribute+, it looks for these translations: - # + # #
    #
  1. activemodel.errors.models.admin.attributes.title.blank
  2. #
  3. activemodel.errors.models.admin.blank
  4. @@ -135,7 +137,7 @@ def generate_message(attribute, message = :invalid, options = {}) klass_ancestors += @base.class.ancestors.reject {|x| x.is_a?(Module)} defaults = klass_ancestors.map do |klass| - [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}", + [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}", :"models.#{klass.name.underscore}.#{message}" ] end @@ -155,4 +157,4 @@ def generate_message(attribute, message = :invalid, options = {}) I18n.translate(key, options) end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 336c2757fcb60..6b6f51d942017 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -1,3 +1,6 @@ +require 'active_support/core_ext/array/extract_options' +require 'active_support/core_ext/hash/keys' + module ActiveModel module Validations extend ActiveSupport::Concern @@ -69,10 +72,9 @@ def validates_each(*attrs) end private - - def validation_method(on) - :validate - end + def validation_method(on) + :validate + end end # Returns the Errors object that holds all information about attribute error messages. diff --git a/activemodel/lib/active_model/validations/acceptance.rb b/activemodel/lib/active_model/validations/acceptance.rb index 0c9ef51726b3a..b65c9b933df7f 100644 --- a/activemodel/lib/active_model/validations/acceptance.rb +++ b/activemodel/lib/active_model/validations/acceptance.rb @@ -39,10 +39,10 @@ def validates_acceptance_of(*attr_names) validates_each(attr_names,configuration) do |record, attr_name, value| unless value == configuration[:accept] - record.errors.add(attr_name, :accepted, :default => configuration[:message]) + record.errors.add(attr_name, :accepted, :default => configuration[:message]) end end end end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/validations/confirmation.rb b/activemodel/lib/active_model/validations/confirmation.rb index b9823172f7b58..d414224dd2e7b 100644 --- a/activemodel/lib/active_model/validations/confirmation.rb +++ b/activemodel/lib/active_model/validations/confirmation.rb @@ -36,10 +36,10 @@ def validates_confirmation_of(*attr_names) validates_each(attr_names, configuration) do |record, attr_name, value| unless record.send("#{attr_name}_confirmation").nil? or value == record.send("#{attr_name}_confirmation") - record.errors.add(attr_name, :confirmation, :default => configuration[:message]) + record.errors.add(attr_name, :confirmation, :default => configuration[:message]) end end end end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index 0aa9848ee154b..2cfdec97a5012 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -29,10 +29,10 @@ def validates_exclusion_of(*attr_names) validates_each(attr_names, configuration) do |record, attr_name, value| if enum.include?(value) - record.errors.add(attr_name, :exclusion, :default => configuration[:message], :value => value) + record.errors.add(attr_name, :exclusion, :default => configuration[:message], :value => value) end end end end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index 8efce8ba2b504..6f3b668bf0805 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -32,10 +32,10 @@ def validates_format_of(*attr_names) validates_each(attr_names, configuration) do |record, attr_name, value| unless value.to_s =~ configuration[:with] - record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) + record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) end end end end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index a4bc8fe035bdb..0d7dc5cd6430e 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -29,10 +29,10 @@ def validates_inclusion_of(*attr_names) validates_each(attr_names, configuration) do |record, attr_name, value| unless enum.include?(value) - record.errors.add(attr_name, :inclusion, :default => configuration[:message], :value => value) + record.errors.add(attr_name, :inclusion, :default => configuration[:message], :value => value) end end end end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index bb9a269a02f99..db0439d4475cc 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -81,7 +81,7 @@ def validates_length_of(*attrs) validates_each(attrs, options) do |record, attr, value| value = options[:tokenizer].call(value) if value.kind_of?(String) unless !value.nil? and value.size.method(validity_checks[option])[option_value] - record.errors.add(attr, key, :default => custom_message, :count => option_value) + record.errors.add(attr, key, :default => custom_message, :count => option_value) end end end @@ -90,4 +90,4 @@ def validates_length_of(*attrs) alias_method :validates_size_of, :validates_length_of end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 79fca2f1eab3d..ada6e28594fa0 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -71,7 +71,7 @@ def validates_numericality_of(*attr_names) case option when :odd, :even unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[] - record.errors.add(attr_name, option, :value => raw_value, :default => configuration[:message]) + record.errors.add(attr_name, option, :value => raw_value, :default => configuration[:message]) end else unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]] @@ -83,4 +83,4 @@ def validates_numericality_of(*attr_names) end end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/validations/presence.rb b/activemodel/lib/active_model/validations/presence.rb index 518bc8a952236..72d6b1c6f0c0b 100644 --- a/activemodel/lib/active_model/validations/presence.rb +++ b/activemodel/lib/active_model/validations/presence.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/object/blank' + module ActiveModel module Validations module ClassMethods @@ -16,7 +18,7 @@ module ClassMethods # # Configuration options: # * message - A custom error message (default is: "can't be blank"). - # * on - Specifies when this validation is active (default is :save, other options :create, + # * on - Specifies when this validation is active (default is :save, other options :create, # :update). # * if - Specifies a method, proc or string to call to determine if the validation should # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). @@ -36,4 +38,4 @@ def validates_presence_of(*attr_names) end end end -end \ No newline at end of file +end