1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Properly require ActiveModel validation dependencies

This commit is contained in:
Joshua Peek 2009-06-08 20:32:08 -05:00
parent 01515f8ecd
commit 28f36279cd
11 changed files with 35 additions and 29 deletions

View file

@ -19,7 +19,7 @@ module ActiveModel
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 @@ module ActiveModel
to_a.each { |error| yield error }
end
end
end
end

View file

@ -1,3 +1,5 @@
require 'active_support/core_ext/string/inflections'
module ActiveModel
class Errors < Hash
include DeprecatedErrorMethods
@ -23,7 +25,7 @@ module ActiveModel
end
def each
each_key do |attribute|
each_key do |attribute|
self[attribute].each { |error| yield attribute, error }
end
end
@ -111,15 +113,15 @@ module ActiveModel
end
# 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,
# 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,
# 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
# default message (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). 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 <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:
#
#
# <ol>
# <li><tt>activemodel.errors.models.admin.attributes.title.blank</tt></li>
# <li><tt>activemodel.errors.models.admin.blank</tt></li>
@ -135,7 +137,7 @@ module ActiveModel
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 @@ module ActiveModel
I18n.translate(key, options)
end
end
end
end

View file

@ -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 @@ module ActiveModel
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.

View file

@ -39,10 +39,10 @@ module ActiveModel
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
end

View file

@ -36,10 +36,10 @@ module ActiveModel
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
end

View file

@ -29,10 +29,10 @@ module ActiveModel
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
end

View file

@ -32,10 +32,10 @@ module ActiveModel
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
end

View file

@ -29,10 +29,10 @@ module ActiveModel
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
end

View file

@ -81,7 +81,7 @@ module ActiveModel
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 @@ module ActiveModel
alias_method :validates_size_of, :validates_length_of
end
end
end
end

View file

@ -71,7 +71,7 @@ module ActiveModel
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 @@ module ActiveModel
end
end
end
end
end

View file

@ -1,3 +1,5 @@
require 'active_support/core_ext/object/blank'
module ActiveModel
module Validations
module ClassMethods
@ -16,7 +18,7 @@ module ActiveModel
#
# Configuration options:
# * <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>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>).
@ -36,4 +38,4 @@ module ActiveModel
end
end
end
end
end