From 08a99d0eac9370b590220953283475e00e3183e6 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 20 Mar 2009 21:40:37 +0000 Subject: Add I18n translations to ActiveModel and move more AR specific parts to ActiveRecord::Validations --- activemodel/lib/active_model/errors.rb | 56 +++++++++------------- activemodel/lib/active_model/locale/en.yml | 24 ++++++++++ activemodel/lib/active_model/validations.rb | 51 ++++++-------------- activemodel/lib/active_model/validations/length.rb | 12 ++--- .../lib/active_model/validations/numericality.rb | 25 ++++++---- 5 files changed, 81 insertions(+), 87 deletions(-) create mode 100644 activemodel/lib/active_model/locale/en.yml (limited to 'activemodel/lib/active_model') 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 (activerecord.errrors.messages). + # 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. activerecord.errors.messages.MESSAGE). The translated model name, + # 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 @@ -134,36 +120,38 @@ module ActiveModel # error +message+ for the title +attribute+, it looks for these translations: # #
    - #
  1. activerecord.errors.models.admin.attributes.title.blank
  2. - #
  3. activerecord.errors.models.admin.blank
  4. - #
  5. activerecord.errors.models.user.attributes.title.blank
  6. - #
  7. activerecord.errors.models.user.blank
  8. - #
  9. activerecord.errors.messages.blank
  10. - #
  11. any default you provided through the +options+ hash (in the activerecord.errors scope)
  12. + #
  13. activemodel.errors.models.admin.attributes.title.blank
  14. + #
  15. activemodel.errors.models.admin.blank
  16. + #
  17. activemodel.errors.models.user.attributes.title.blank
  18. + #
  19. activemodel.errors.models.user.blank
  20. + #
  21. activemodel.errors.messages.blank
  22. + #
  23. any default you provided through the +options+ hash (in the activemodel.errors scope)
  24. #
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 Errors.add(field, msg) 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 -- cgit v1.2.3