aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-03-20 21:40:37 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-03-20 21:45:13 +0000
commit08a99d0eac9370b590220953283475e00e3183e6 (patch)
treee1b32a221fe5e4b0e639ff74f5c099af32da29e6 /activemodel/lib/active_model
parentcc5e019f6bc48663fe75a00e68293c0645998d14 (diff)
downloadrails-08a99d0eac9370b590220953283475e00e3183e6.tar.gz
rails-08a99d0eac9370b590220953283475e00e3183e6.tar.bz2
rails-08a99d0eac9370b590220953283475e00e3183e6.zip
Add I18n translations to ActiveModel and move more AR specific parts to ActiveRecord::Validations
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r--activemodel/lib/active_model/errors.rb56
-rw-r--r--activemodel/lib/active_model/locale/en.yml24
-rw-r--r--activemodel/lib/active_model/validations.rb51
-rw-r--r--activemodel/lib/active_model/validations/length.rb12
-rw-r--r--activemodel/lib/active_model/validations/numericality.rb25
5 files changed, 81 insertions, 87 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index b9541ca3be..c2bde29895 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -2,15 +2,6 @@ module ActiveModel
class Errors < Hash
include DeprecatedErrorMethods
- class << self
- def default_error_messages
- message = "Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages')."
- ActiveSupport::Deprecation.warn(message)
-
- I18n.translate 'activerecord.errors.messages'
- end
- end
-
def initialize(base)
@base = base
super()
@@ -74,7 +65,7 @@ module ActiveModel
# Will add an error message to each of the attributes in +attributes+ that is empty.
def add_on_empty(attributes, custom_message = nil)
[attributes].flatten.each do |attribute|
- value = @base.respond_to?(attribute.to_s) ? @base.send(attribute.to_s) : @base[attribute.to_s]
+ value = @base.get_attribute_value(attribute)
is_empty = value.respond_to?(:empty?) ? value.empty? : false
add(attribute, :empty, :default => custom_message) unless !value.nil? && !is_empty
end
@@ -83,14 +74,14 @@ module ActiveModel
# Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?).
def add_on_blank(attributes, custom_message = nil)
[attributes].flatten.each do |attribute|
- value = @base.respond_to?(attribute.to_sym) ? @base.send(attribute.to_sym) : @base[attribute.to_sym]
+ value = @base.get_attribute_value(attribute)
add(attribute, :blank, :default => custom_message) if value.blank?
end
end
# Returns all the full error messages in an array.
#
- # class Company < ActiveRecord::Base
+ # class Company
# validates_presence_of :name, :address, :email
# validates_length_of :name, :in => 5..30
# end
@@ -107,13 +98,8 @@ module ActiveModel
if attribute == :base
messages.each {|m| full_messages << m }
else
- if @base.class.respond_to?(:human_attribute_name)
- attr_name = @base.class.human_attribute_name(attribute.to_s)
- else
- attr_name = attribute.to_s.humanize
- end
-
- prefix = attr_name + I18n.t('activerecord.errors.format.separator', :default => ' ')
+ attr_name = attribute.to_s.humanize
+ prefix = attr_name + I18n.t('activemodel.errors.format.separator', :default => ' ')
messages.each do |m|
full_messages << "#{prefix}#{m}"
end
@@ -123,10 +109,10 @@ module ActiveModel
full_messages
end
- # Translates an error message in it's default scope (<tt>activerecord.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,
# 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>activerecord.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.
#
# When using inheritence in your models, it will check all the inherited models too, but only if the model itself
@@ -134,36 +120,38 @@ module ActiveModel
# error +message+ for the <tt>title</tt> +attribute+, it looks for these translations:
#
# <ol>
- # <li><tt>activerecord.errors.models.admin.attributes.title.blank</tt></li>
- # <li><tt>activerecord.errors.models.admin.blank</tt></li>
- # <li><tt>activerecord.errors.models.user.attributes.title.blank</tt></li>
- # <li><tt>activerecord.errors.models.user.blank</tt></li>
- # <li><tt>activerecord.errors.messages.blank</tt></li>
- # <li>any default you provided through the +options+ hash (in the activerecord.errors scope)</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.user.attributes.title.blank</tt></li>
+ # <li><tt>activemodel.errors.models.user.blank</tt></li>
+ # <li><tt>activemodel.errors.messages.blank</tt></li>
+ # <li>any default you provided through the +options+ hash (in the activemodel.errors scope)</li>
# </ol>
def generate_message(attribute, message = :invalid, options = {})
message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
- defaults = @base.class.self_and_descendants_from_active_record.map do |klass|
+ klass_ancestors = [@base.class]
+ 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}.#{message}" ]
end
-
+
defaults << options.delete(:default)
defaults = defaults.compact.flatten << :"messages.#{message}"
key = defaults.shift
- value = @base.respond_to?(attribute) ? @base.send(attribute) : nil
+ value = @base.get_attribute_value(attribute)
options = { :default => defaults,
- :model => @base.class.human_name,
- :attribute => @base.class.human_attribute_name(attribute.to_s),
+ :model => @base.class.name.humanize,
+ :attribute => attribute.to_s.humanize,
:value => value,
- :scope => [:activerecord, :errors]
+ :scope => [:activemodel, :errors]
}.merge(options)
I18n.translate(key, options)
end
-
end
end \ No newline at end of file
diff --git a/activemodel/lib/active_model/locale/en.yml b/activemodel/lib/active_model/locale/en.yml
new file mode 100644
index 0000000000..0c2cf9ea33
--- /dev/null
+++ b/activemodel/lib/active_model/locale/en.yml
@@ -0,0 +1,24 @@
+en:
+ activemodel:
+ errors:
+ # The values :model, :attribute and :value are always available for interpolation
+ # The value :count is available when applicable. Can be used for pluralization.
+ messages:
+ inclusion: "is not included in the list"
+ exclusion: "is reserved"
+ invalid: "is invalid"
+ confirmation: "doesn't match confirmation"
+ accepted: "must be accepted"
+ empty: "can't be empty"
+ blank: "can't be blank"
+ too_long: "is too long (maximum is {{count}} characters)"
+ too_short: "is too short (minimum is {{count}} characters)"
+ wrong_length: "is the wrong length (should be {{count}} characters)"
+ not_a_number: "is not a number"
+ greater_than: "must be greater than {{count}}"
+ greater_than_or_equal_to: "must be greater than or equal to {{count}}"
+ equal_to: "must be equal to {{count}}"
+ less_than: "must be less than {{count}}"
+ less_than_or_equal_to: "must be less than or equal to {{count}}"
+ odd: "must be odd"
+ even: "must be even"
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index 3ea17381c9..c0bbad0396 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -5,7 +5,7 @@ module ActiveModel
def self.included(base) # :nodoc:
base.extend(ClassMethods)
base.__send__(:include, ActiveSupport::Callbacks)
- base.define_callbacks :validate, :validate_on_create, :validate_on_update
+ base.define_callbacks :validate
end
module ClassMethods
@@ -64,7 +64,7 @@ module ActiveModel
# Declare the validation.
send(validation_method(options[:on] || :save), options) do |record|
attrs.each do |attr|
- value = record.send(attr)
+ value = record.get_attribute_value(attr)
next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
yield record, attr, value
end
@@ -72,13 +72,14 @@ module ActiveModel
end
private
- def validation_method(on)
- case on
- when :save then :validate
- when :create then :validate_on_create
- when :update then :validate_on_update
- end
+
+ def validation_method(on)
+ case on
+ when :save then :validate
+ when :create then :validate_on_create
+ when :update then :validate_on_update
end
+ end
end
# Returns the Errors object that holds all information about attribute error messages.
@@ -91,27 +92,7 @@ module ActiveModel
errors.clear
run_callbacks(:validate)
-
- if respond_to?(:validate)
- # ActiveSupport::Deprecation.warn "Base#validate has been deprecated, please use Base.validate :method instead"
- validate
- end
-
- if new_record?
- run_callbacks(:validate_on_create)
-
- if respond_to?(:validate_on_create)
- # ActiveSupport::Deprecation.warn "Base#validate_on_create has been deprecated, please use Base.validate_on_create :method instead"
- validate_on_create
- end
- else
- run_callbacks(:validate_on_update)
-
- if respond_to?(:validate_on_update)
- # ActiveSupport::Deprecation.warn "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead"
- validate_on_update
- end
- end
+ validate if respond_to?(:validate)
errors.empty?
end
@@ -121,19 +102,15 @@ module ActiveModel
!valid?
end
+ def get_attribute_value(attribute)
+ respond_to?(attribute.to_sym) ? send(attribute.to_sym) : instance_variable_get(:"@#{attribute}")
+ end
+
protected
# Overwrite this method for validation checks on all saves and use <tt>Errors.add(field, msg)</tt> for invalid attributes.
def validate
end
-
- # Overwrite this method for validation checks used only on creation.
- def validate_on_create
- end
-
- # Overwrite this method for validation checks used only on updates.
- def validate_on_update
- end
end
end
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb
index f6bb1f6d70..9736595990 100644
--- a/activemodel/lib/active_model/validations/length.rb
+++ b/activemodel/lib/active_model/validations/length.rb
@@ -48,12 +48,12 @@ module ActiveModel
# Ensure that one and only one range option is specified.
range_options = ALL_RANGE_OPTIONS & options.keys
case range_options.size
- when 0
- raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
- when 1
- # Valid number of options; do nothing.
- else
- raise ArgumentError, 'Too many range options specified. Choose only one.'
+ when 0
+ raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
+ when 1
+ # Valid number of options; do nothing.
+ else
+ raise ArgumentError, 'Too many range options specified. Choose only one.'
end
# Get range option and value.
diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb
index d8e3470304..99035b8af8 100644
--- a/activemodel/lib/active_model/validations/numericality.rb
+++ b/activemodel/lib/active_model/validations/numericality.rb
@@ -35,7 +35,6 @@ module ActiveModel
configuration = { :on => :save, :only_integer => false, :allow_nil => false }
configuration.update(attr_names.extract_options!)
-
numericality_options = ALL_NUMERICALITY_CHECKS.keys & configuration.keys
(numericality_options - [ :odd, :even ]).each do |option|
@@ -43,7 +42,13 @@ module ActiveModel
end
validates_each(attr_names,configuration) do |record, attr_name, value|
- raw_value = record.send("#{attr_name}_before_type_cast") || value
+ before_type_cast = "#{attr_name}_before_type_cast"
+
+ if record.respond_to?(before_type_cast.to_sym)
+ raw_value = record.send("#{attr_name}_before_type_cast") || value
+ else
+ raw_value = value
+ end
next if configuration[:allow_nil] and raw_value.nil?
@@ -64,14 +69,14 @@ module ActiveModel
numericality_options.each do |option|
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])
- end
- else
- unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
- record.errors.add(attr_name, option, :default => configuration[:message], :value => raw_value, :count => configuration[option])
- end
+ 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])
+ end
+ else
+ unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
+ record.errors.add(attr_name, option, :default => configuration[:message], :value => raw_value, :count => configuration[option])
+ end
end
end
end