Skip to content

Commit

Permalink
Properly require ActiveModel validation dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jun 9, 2009
1 parent 01515f8 commit 28f3627
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 29 deletions.
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/deprecated_error_methods.rb
Expand Up @@ -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" ActiveSupport::Deprecation.warn "Errors#add_to_base(msg) has been deprecated, use Errors#[:base] << msg instead"
self[:base] << msg self[:base] << msg
end end

def invalid?(attribute) def invalid?(attribute)
ActiveSupport::Deprecation.warn "Errors#invalid?(attribute) has been deprecated, use Errors#[attribute].any? instead" ActiveSupport::Deprecation.warn "Errors#invalid?(attribute) has been deprecated, use Errors#[attribute].any? instead"
self[attribute].any? self[attribute].any?
Expand All @@ -30,4 +30,4 @@ def each_full
to_a.each { |error| yield error } to_a.each { |error| yield error }
end end
end end
end end
16 changes: 9 additions & 7 deletions activemodel/lib/active_model/errors.rb
@@ -1,3 +1,5 @@
require 'active_support/core_ext/string/inflections'

module ActiveModel module ActiveModel
class Errors < Hash class Errors < Hash
include DeprecatedErrorMethods include DeprecatedErrorMethods
Expand All @@ -23,7 +25,7 @@ def []=(attribute, error)
end end


def each def each
each_key do |attribute| each_key do |attribute|
self[attribute].each { |error| yield attribute, error } self[attribute].each { |error| yield attribute, error }
end end
end end
Expand Down Expand Up @@ -111,15 +113,15 @@ def full_messages(options = {})
end end


# Translates an error message in it's default scope (<tt>activemodel.errrors.messages</tt>). # Translates an error message in it's default scope (<tt>activemodel.errrors.messages</tt>).
# Error messages are first looked up in <tt>models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>, if it's not there, # Error messages are first looked up in <tt>models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>, if it's not there,
# it's looked up in <tt>models.MODEL.MESSAGE</tt> and if that is not there it returns the translation of the # it's looked up in <tt>models.MODEL.MESSAGE</tt> and if that is not there it returns the translation of the
# default message (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model name, # default message (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model name,
# translated attribute name and the value are available for interpolation. # 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 # 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 <tt>class Admin < User; end</tt> and you wanted the translation for the <tt>:blank</tt> # hasn't been found. Say you have <tt>class Admin < User; end</tt> and you wanted the translation for the <tt>:blank</tt>
# error +message+ for the <tt>title</tt> +attribute+, it looks for these translations: # error +message+ for the <tt>title</tt> +attribute+, it looks for these translations:
# #
# <ol> # <ol>
# <li><tt>activemodel.errors.models.admin.attributes.title.blank</tt></li> # <li><tt>activemodel.errors.models.admin.attributes.title.blank</tt></li>
# <li><tt>activemodel.errors.models.admin.blank</tt></li> # <li><tt>activemodel.errors.models.admin.blank</tt></li>
Expand All @@ -135,7 +137,7 @@ def generate_message(attribute, message = :invalid, options = {})
klass_ancestors += @base.class.ancestors.reject {|x| x.is_a?(Module)} klass_ancestors += @base.class.ancestors.reject {|x| x.is_a?(Module)}


defaults = klass_ancestors.map do |klass| 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}" ] :"models.#{klass.name.underscore}.#{message}" ]
end end


Expand All @@ -155,4 +157,4 @@ def generate_message(attribute, message = :invalid, options = {})
I18n.translate(key, options) I18n.translate(key, options)
end end
end end
end end
10 changes: 6 additions & 4 deletions 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 ActiveModel
module Validations module Validations
extend ActiveSupport::Concern extend ActiveSupport::Concern
Expand Down Expand Up @@ -69,10 +72,9 @@ def validates_each(*attrs)
end end


private private

def validation_method(on)
def validation_method(on) :validate
:validate end
end
end end


# Returns the Errors object that holds all information about attribute error messages. # Returns the Errors object that holds all information about attribute error messages.
Expand Down
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/acceptance.rb
Expand Up @@ -39,10 +39,10 @@ def validates_acceptance_of(*attr_names)


validates_each(attr_names,configuration) do |record, attr_name, value| validates_each(attr_names,configuration) do |record, attr_name, value|
unless value == configuration[:accept] 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
end end
end end
end end
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/confirmation.rb
Expand Up @@ -36,10 +36,10 @@ def validates_confirmation_of(*attr_names)


validates_each(attr_names, configuration) do |record, attr_name, value| validates_each(attr_names, configuration) do |record, attr_name, value|
unless record.send("#{attr_name}_confirmation").nil? or value == record.send("#{attr_name}_confirmation") 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
end end
end end
end end
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/exclusion.rb
Expand Up @@ -29,10 +29,10 @@ def validates_exclusion_of(*attr_names)


validates_each(attr_names, configuration) do |record, attr_name, value| validates_each(attr_names, configuration) do |record, attr_name, value|
if enum.include?(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
end end
end end
end end
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/format.rb
Expand Up @@ -32,10 +32,10 @@ def validates_format_of(*attr_names)


validates_each(attr_names, configuration) do |record, attr_name, value| validates_each(attr_names, configuration) do |record, attr_name, value|
unless value.to_s =~ configuration[:with] 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
end end
end end
end end
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/inclusion.rb
Expand Up @@ -29,10 +29,10 @@ def validates_inclusion_of(*attr_names)


validates_each(attr_names, configuration) do |record, attr_name, value| validates_each(attr_names, configuration) do |record, attr_name, value|
unless enum.include?(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
end end
end end
end end
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/length.rb
Expand Up @@ -81,7 +81,7 @@ def validates_length_of(*attrs)
validates_each(attrs, options) do |record, attr, value| validates_each(attrs, options) do |record, attr, value|
value = options[:tokenizer].call(value) if value.kind_of?(String) value = options[:tokenizer].call(value) if value.kind_of?(String)
unless !value.nil? and value.size.method(validity_checks[option])[option_value] 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 end
end end
Expand All @@ -90,4 +90,4 @@ def validates_length_of(*attrs)
alias_method :validates_size_of, :validates_length_of alias_method :validates_size_of, :validates_length_of
end end
end end
end end
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/numericality.rb
Expand Up @@ -71,7 +71,7 @@ def validates_numericality_of(*attr_names)
case option case option
when :odd, :even when :odd, :even
unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[] 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 end
else else
unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]] unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
Expand All @@ -83,4 +83,4 @@ def validates_numericality_of(*attr_names)
end end
end end
end end
end end
6 changes: 4 additions & 2 deletions activemodel/lib/active_model/validations/presence.rb
@@ -1,3 +1,5 @@
require 'active_support/core_ext/object/blank'

module ActiveModel module ActiveModel
module Validations module Validations
module ClassMethods module ClassMethods
Expand All @@ -16,7 +18,7 @@ module ClassMethods
# #
# Configuration options: # Configuration options:
# * <tt>message</tt> - A custom error message (default is: "can't be blank"). # * <tt>message</tt> - A custom error message (default is: "can't be blank").
# * <tt>on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, # * <tt>on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>,
# <tt>:update</tt>). # <tt>:update</tt>).
# * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>).
Expand All @@ -36,4 +38,4 @@ def validates_presence_of(*attr_names)
end end
end end
end end
end end

0 comments on commit 28f3627

Please sign in to comment.