diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 2e5bcab070..ff11ddc605 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -133,28 +133,35 @@ def full_messages #
  • activemodel.errors.models.admin.blank
  • #
  • activemodel.errors.models.user.attributes.title.blank
  • #
  • activemodel.errors.models.user.blank
  • - #
  • activemodel.errors.messages.blank
  • #
  • any default you provided through the +options+ hash (in the activemodel.errors scope)
  • + #
  • activemodel.errors.messages.blank
  • + #
  • errors.attributes.title.blank
  • + #
  • errors.messages.blank
  • # def generate_message(attribute, message = :invalid, options = {}) message, options[:default] = options[:default], message if options[:default].is_a?(Symbol) defaults = @base.class.lookup_ancestors.map do |klass| - [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}", - :"models.#{klass.name.underscore}.#{message}" ] + [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{message}", + :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{message}" ] end defaults << options.delete(:default) - defaults = defaults.compact.flatten << :"messages.#{message}" + defaults << :"#{@base.class.i18n_scope}.errors.messages.#{message}" + defaults << :"errors.attributes.#{attribute}.#{message}" + defaults << :"errors.messages.#{message}" + + defaults.compact! + defaults.flatten! key = defaults.shift value = @base.send(:read_attribute_for_validation, attribute) - options = { :default => defaults, + options = { + :default => defaults, :model => @base.class.model_name.human, :attribute => @base.class.human_attribute_name(attribute), - :value => value, - :scope => [:errors] + :value => value }.merge(options) I18n.translate(key, options) diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index 7d33fcea98..38844bb1fa 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -254,7 +254,7 @@ def test_validates_numericality_of_less_than_odd_generates_message_with_custom_d # validates_confirmation_of w/o mocha def test_validates_confirmation_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:confirmation => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:confirmation => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:confirmation => 'global message'}} Person.validates_confirmation_of :title @@ -275,7 +275,7 @@ def test_validates_confirmation_of_finds_global_default_translation # validates_acceptance_of w/o mocha def test_validates_acceptance_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:accepted => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:accepted => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:accepted => 'global message'}} Person.validates_acceptance_of :title, :allow_nil => false @@ -294,7 +294,7 @@ def test_validates_acceptance_of_finds_global_default_translation # validates_presence_of w/o mocha def test_validates_presence_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:blank => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:blank => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:blank => 'global message'}} Person.validates_presence_of :title @@ -313,7 +313,7 @@ def test_validates_presence_of_finds_global_default_translation # validates_length_of :within w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:too_short => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:too_short => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:too_short => 'global message'}} Person.validates_length_of :title, :within => 3..5 @@ -332,7 +332,7 @@ def test_validates_length_of_within_finds_global_default_translation # validates_length_of :is w/o mocha def test_validates_length_of_is_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:wrong_length => 'global message'}} Person.validates_length_of :title, :is => 5 @@ -351,7 +351,7 @@ def test_validates_length_of_is_finds_global_default_translation # validates_format_of w/o mocha def test_validates_format_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:invalid => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:invalid => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}} Person.validates_format_of :title, :with => /^[1-9][0-9]*$/ @@ -370,7 +370,7 @@ def test_validates_format_of_finds_global_default_translation # validates_inclusion_of w/o mocha def test_validates_inclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:inclusion => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:inclusion => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:inclusion => 'global message'}} Person.validates_inclusion_of :title, :in => %w(a b c) @@ -389,7 +389,7 @@ def test_validates_inclusion_of_finds_global_default_translation # validates_exclusion_of w/o mocha def test_validates_exclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:exclusion => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:exclusion => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:exclusion => 'global message'}} Person.validates_exclusion_of :title, :in => %w(a b c) @@ -410,7 +410,7 @@ def test_validates_exclusion_of_finds_global_default_translation # validates_numericality_of without :only_integer w/o mocha def test_validates_numericality_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title @@ -431,7 +431,7 @@ def test_validates_numericality_of_finds_global_default_translation # validates_numericality_of with :only_integer w/o mocha def test_validates_numericality_of_only_integer_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title, :only_integer => true @@ -452,7 +452,7 @@ def test_validates_numericality_of_only_integer_finds_global_default_translation # validates_numericality_of :odd w/o mocha def test_validates_numericality_of_odd_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:odd => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:odd => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:odd => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :odd => true @@ -473,7 +473,7 @@ def test_validates_numericality_of_odd_finds_global_default_translation # validates_numericality_of :less_than w/o mocha def test_validates_numericality_of_less_than_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:less_than => 'custom message'}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:less_than => 'custom message'}}}}}} I18n.backend.store_translations 'en', :errors => {:messages => {:less_than => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :less_than => 0 @@ -502,7 +502,7 @@ def test_validations_with_message_symbol_must_translate end def test_validates_with_message_symbol_must_translate_per_attribute - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}} Person.validates_presence_of :title, :message => :custom_error @person.title = nil @person.valid? @@ -510,7 +510,7 @@ def test_validates_with_message_symbol_must_translate_per_attribute end def test_validates_with_message_symbol_must_translate_per_model - I18n.backend.store_translations 'en', :errors => {:models => {:person => {:custom_error => "I am a custom error"}}} + I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:custom_error => "I am a custom error"}}}} Person.validates_presence_of :title, :message => :custom_error @person.title = nil @person.valid? diff --git a/activerecord/lib/active_record/locale/en.yml b/activerecord/lib/active_record/locale/en.yml index 4115cc8e17..810359fef3 100644 --- a/activerecord/lib/active_record/locale/en.yml +++ b/activerecord/lib/active_record/locale/en.yml @@ -1,9 +1,16 @@ en: - errors: - messages: - taken: "has already been taken" - record_invalid: "Validation failed: {{errors}}" - # Append your own errors here or at the model/attributes scope. + # Attributes names common to most models + #attributes: + #created_at: "Created at" + #updated_at: "Updated at" + + # ActiveRecord models configuration + activerecord: + errors: + messages: + taken: "has already been taken" + record_invalid: "Validation failed: {{errors}}" + # Append your own errors here or at the model/attributes scope. # You can define own errors for models or model attributes. # The values :model, :attribute and :value are always available for interpolation. @@ -19,13 +26,6 @@ en: # custom blank validation message for login attribute of User model. #models: - # Attributes names common to most models - #attributes: - #created_at: "Created at" - #updated_at: "Updated at" - - # ActiveRecord models configuration - #activerecord: # Translate model names. Used in Model.human_name(). #models: # For example, diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 30da494d57..2b204043b4 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -77,17 +77,5 @@ class Railtie < Rails::Railtie end end end - - initializer "active_record.i18n_deprecation" do - require 'active_support/i18n' - - begin - I18n.t(:"activerecord.errors", :raise => true) - warn "[DEPRECATION] \"activerecord.errors\" namespace is deprecated in I18n " << - "yml files, please use just \"errors\" instead." - rescue Exception => e - # No message then. - end - end end end diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index a9743aa1ea..8b266be638 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -10,8 +10,8 @@ class RecordInvalid < ActiveRecordError attr_reader :record def initialize(record) @record = record - errors = @record.errors.full_messages.join(I18n.t('support.array.words_connector', :default => ', ')) - super(I18n.t('errors.messages.record_invalid', :errors => errors)) + errors = @record.errors.full_messages.join(", ") + super(I18n.t("activerecord.errors.messages.record_invalid", :errors => errors)) end end diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index cc5460ddb7..2995cc6f72 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -1002,9 +1002,9 @@ def test_should_not_use_default_invalid_error_on_associated_models end def test_should_default_invalid_error_from_i18n - I18n.backend.store_translations(:en, :errors => { :models => + I18n.backend.store_translations(:en, :activerecord => {:errors => { :models => { @association_name.to_s.singularize.to_sym => { :blank => "cannot be blank" } } - }) + }}) @pirate.send(@association_name).build(:name => '') diff --git a/activerecord/test/cases/validations/i18n_validation_test.rb b/activerecord/test/cases/validations/i18n_validation_test.rb index 5dfbb1516f..a0ff35f948 100644 --- a/activerecord/test/cases/validations/i18n_validation_test.rb +++ b/activerecord/test/cases/validations/i18n_validation_test.rb @@ -50,8 +50,8 @@ def test_validates_uniqueness_of_generates_message_with_custom_default_message # validates_uniqueness_of w/o mocha def test_validates_associated_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:topic => {:attributes => {:title => {:taken => 'custom message'}}}}} - I18n.backend.store_translations 'en', :errors => {:messages => {:taken => 'global message'}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:taken => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:taken => 'global message'}}} Topic.validates_uniqueness_of :title unique_topic.valid? @@ -59,7 +59,7 @@ def test_validates_associated_finds_custom_model_key_translation end def test_validates_associated_finds_global_default_translation - I18n.backend.store_translations 'en', :errors => {:messages => {:taken => 'global message'}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:taken => 'global message'}}} Topic.validates_uniqueness_of :title unique_topic.valid? @@ -83,16 +83,16 @@ def test_validates_associated_generates_message_with_custom_default_message # validates_associated w/o mocha def test_validates_associated_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}} - I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_associated :replies replied_topic.valid? - assert_equal ['custom message'], replied_topic.errors[:replies] + assert_equal ['custom message'], replied_topic.errors[:replies].uniq end def test_validates_associated_finds_global_default_translation - I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_associated :replies replied_topic.valid?