mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Drop AR I18n deprecation and simple use errors.messages as fallback.
This commit is contained in:
parent
431fc3c817
commit
d6e2f5013c
7 changed files with 51 additions and 56 deletions
|
@ -133,28 +133,35 @@ module ActiveModel
|
||||||
# <li><tt>activemodel.errors.models.admin.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.attributes.title.blank</tt></li>
|
||||||
# <li><tt>activemodel.errors.models.user.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>
|
# <li>any default you provided through the +options+ hash (in the activemodel.errors scope)</li>
|
||||||
|
# <li><tt>activemodel.errors.messages.blank</tt></li>
|
||||||
|
# <li><tt>errors.attributes.title.blank</tt></li>
|
||||||
|
# <li><tt>errors.messages.blank</tt></li>
|
||||||
# </ol>
|
# </ol>
|
||||||
def generate_message(attribute, message = :invalid, options = {})
|
def generate_message(attribute, message = :invalid, options = {})
|
||||||
message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
|
message, options[:default] = options[:default], message if options[:default].is_a?(Symbol)
|
||||||
|
|
||||||
defaults = @base.class.lookup_ancestors.map do |klass|
|
defaults = @base.class.lookup_ancestors.map do |klass|
|
||||||
[ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}",
|
[ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{message}",
|
||||||
:"models.#{klass.name.underscore}.#{message}" ]
|
:"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{message}" ]
|
||||||
end
|
end
|
||||||
|
|
||||||
defaults << options.delete(:default)
|
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
|
key = defaults.shift
|
||||||
value = @base.send(:read_attribute_for_validation, attribute)
|
value = @base.send(:read_attribute_for_validation, attribute)
|
||||||
|
|
||||||
options = { :default => defaults,
|
options = {
|
||||||
|
:default => defaults,
|
||||||
:model => @base.class.model_name.human,
|
:model => @base.class.model_name.human,
|
||||||
:attribute => @base.class.human_attribute_name(attribute),
|
:attribute => @base.class.human_attribute_name(attribute),
|
||||||
:value => value,
|
:value => value
|
||||||
:scope => [:errors]
|
|
||||||
}.merge(options)
|
}.merge(options)
|
||||||
|
|
||||||
I18n.translate(key, options)
|
I18n.translate(key, options)
|
||||||
|
|
|
@ -254,7 +254,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_confirmation_of w/o mocha
|
# validates_confirmation_of w/o mocha
|
||||||
|
|
||||||
def test_validates_confirmation_of_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:confirmation => 'global message'}}
|
||||||
|
|
||||||
Person.validates_confirmation_of :title
|
Person.validates_confirmation_of :title
|
||||||
|
@ -275,7 +275,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_acceptance_of w/o mocha
|
# validates_acceptance_of w/o mocha
|
||||||
|
|
||||||
def test_validates_acceptance_of_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:accepted => 'global message'}}
|
||||||
|
|
||||||
Person.validates_acceptance_of :title, :allow_nil => false
|
Person.validates_acceptance_of :title, :allow_nil => false
|
||||||
|
@ -294,7 +294,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_presence_of w/o mocha
|
# validates_presence_of w/o mocha
|
||||||
|
|
||||||
def test_validates_presence_of_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:blank => 'global message'}}
|
||||||
|
|
||||||
Person.validates_presence_of :title
|
Person.validates_presence_of :title
|
||||||
|
@ -313,7 +313,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_length_of :within w/o mocha
|
# validates_length_of :within w/o mocha
|
||||||
|
|
||||||
def test_validates_length_of_within_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:too_short => 'global message'}}
|
||||||
|
|
||||||
Person.validates_length_of :title, :within => 3..5
|
Person.validates_length_of :title, :within => 3..5
|
||||||
|
@ -332,7 +332,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_length_of :is w/o mocha
|
# validates_length_of :is w/o mocha
|
||||||
|
|
||||||
def test_validates_length_of_is_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:wrong_length => 'global message'}}
|
||||||
|
|
||||||
Person.validates_length_of :title, :is => 5
|
Person.validates_length_of :title, :is => 5
|
||||||
|
@ -351,7 +351,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_format_of w/o mocha
|
# validates_format_of w/o mocha
|
||||||
|
|
||||||
def test_validates_format_of_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}}
|
||||||
|
|
||||||
Person.validates_format_of :title, :with => /^[1-9][0-9]*$/
|
Person.validates_format_of :title, :with => /^[1-9][0-9]*$/
|
||||||
|
@ -370,7 +370,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_inclusion_of w/o mocha
|
# validates_inclusion_of w/o mocha
|
||||||
|
|
||||||
def test_validates_inclusion_of_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:inclusion => 'global message'}}
|
||||||
|
|
||||||
Person.validates_inclusion_of :title, :in => %w(a b c)
|
Person.validates_inclusion_of :title, :in => %w(a b c)
|
||||||
|
@ -389,7 +389,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_exclusion_of w/o mocha
|
# validates_exclusion_of w/o mocha
|
||||||
|
|
||||||
def test_validates_exclusion_of_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:exclusion => 'global message'}}
|
||||||
|
|
||||||
Person.validates_exclusion_of :title, :in => %w(a b c)
|
Person.validates_exclusion_of :title, :in => %w(a b c)
|
||||||
|
@ -410,7 +410,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_numericality_of without :only_integer w/o mocha
|
# validates_numericality_of without :only_integer w/o mocha
|
||||||
|
|
||||||
def test_validates_numericality_of_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}}
|
||||||
|
|
||||||
Person.validates_numericality_of :title
|
Person.validates_numericality_of :title
|
||||||
|
@ -431,7 +431,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_numericality_of with :only_integer w/o mocha
|
# validates_numericality_of with :only_integer w/o mocha
|
||||||
|
|
||||||
def test_validates_numericality_of_only_integer_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}}
|
||||||
|
|
||||||
Person.validates_numericality_of :title, :only_integer => true
|
Person.validates_numericality_of :title, :only_integer => true
|
||||||
|
@ -452,7 +452,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_numericality_of :odd w/o mocha
|
# validates_numericality_of :odd w/o mocha
|
||||||
|
|
||||||
def test_validates_numericality_of_odd_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:odd => 'global message'}}
|
||||||
|
|
||||||
Person.validates_numericality_of :title, :only_integer => true, :odd => true
|
Person.validates_numericality_of :title, :only_integer => true, :odd => true
|
||||||
|
@ -473,7 +473,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
# validates_numericality_of :less_than w/o mocha
|
# validates_numericality_of :less_than w/o mocha
|
||||||
|
|
||||||
def test_validates_numericality_of_less_than_finds_custom_model_key_translation
|
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'}}
|
I18n.backend.store_translations 'en', :errors => {:messages => {:less_than => 'global message'}}
|
||||||
|
|
||||||
Person.validates_numericality_of :title, :only_integer => true, :less_than => 0
|
Person.validates_numericality_of :title, :only_integer => true, :less_than => 0
|
||||||
|
@ -502,7 +502,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_validates_with_message_symbol_must_translate_per_attribute
|
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.validates_presence_of :title, :message => :custom_error
|
||||||
@person.title = nil
|
@person.title = nil
|
||||||
@person.valid?
|
@person.valid?
|
||||||
|
@ -510,7 +510,7 @@ class I18nValidationTest < ActiveModel::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_validates_with_message_symbol_must_translate_per_model
|
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.validates_presence_of :title, :message => :custom_error
|
||||||
@person.title = nil
|
@person.title = nil
|
||||||
@person.valid?
|
@person.valid?
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
en:
|
en:
|
||||||
|
# Attributes names common to most models
|
||||||
|
#attributes:
|
||||||
|
#created_at: "Created at"
|
||||||
|
#updated_at: "Updated at"
|
||||||
|
|
||||||
|
# ActiveRecord models configuration
|
||||||
|
activerecord:
|
||||||
errors:
|
errors:
|
||||||
messages:
|
messages:
|
||||||
taken: "has already been taken"
|
taken: "has already been taken"
|
||||||
|
@ -19,13 +26,6 @@ en:
|
||||||
# custom blank validation message for login attribute of User model.
|
# custom blank validation message for login attribute of User model.
|
||||||
#models:
|
#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().
|
# Translate model names. Used in Model.human_name().
|
||||||
#models:
|
#models:
|
||||||
# For example,
|
# For example,
|
||||||
|
|
|
@ -77,17 +77,5 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,8 +10,8 @@ module ActiveRecord
|
||||||
attr_reader :record
|
attr_reader :record
|
||||||
def initialize(record)
|
def initialize(record)
|
||||||
@record = record
|
@record = record
|
||||||
errors = @record.errors.full_messages.join(I18n.t('support.array.words_connector', :default => ', '))
|
errors = @record.errors.full_messages.join(", ")
|
||||||
super(I18n.t('errors.messages.record_invalid', :errors => errors))
|
super(I18n.t("activerecord.errors.messages.record_invalid", :errors => errors))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1002,9 +1002,9 @@ module AutosaveAssociationOnACollectionAssociationTests
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_default_invalid_error_from_i18n
|
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" } }
|
{ @association_name.to_s.singularize.to_sym => { :blank => "cannot be blank" } }
|
||||||
})
|
}})
|
||||||
|
|
||||||
@pirate.send(@association_name).build(:name => '')
|
@pirate.send(@association_name).build(:name => '')
|
||||||
|
|
||||||
|
|
|
@ -50,8 +50,8 @@ class I18nValidationTest < ActiveRecord::TestCase
|
||||||
# validates_uniqueness_of w/o mocha
|
# validates_uniqueness_of w/o mocha
|
||||||
|
|
||||||
def test_validates_associated_finds_custom_model_key_translation
|
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', :activerecord => {: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 => {:messages => {:taken => 'global message'}}}
|
||||||
|
|
||||||
Topic.validates_uniqueness_of :title
|
Topic.validates_uniqueness_of :title
|
||||||
unique_topic.valid?
|
unique_topic.valid?
|
||||||
|
@ -59,7 +59,7 @@ class I18nValidationTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_validates_associated_finds_global_default_translation
|
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
|
Topic.validates_uniqueness_of :title
|
||||||
unique_topic.valid?
|
unique_topic.valid?
|
||||||
|
@ -83,16 +83,16 @@ class I18nValidationTest < ActiveRecord::TestCase
|
||||||
# validates_associated w/o mocha
|
# validates_associated w/o mocha
|
||||||
|
|
||||||
def test_validates_associated_finds_custom_model_key_translation
|
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', :activerecord => {: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 => {:messages => {:invalid => 'global message'}}}
|
||||||
|
|
||||||
Topic.validates_associated :replies
|
Topic.validates_associated :replies
|
||||||
replied_topic.valid?
|
replied_topic.valid?
|
||||||
assert_equal ['custom message'], replied_topic.errors[:replies]
|
assert_equal ['custom message'], replied_topic.errors[:replies].uniq
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_validates_associated_finds_global_default_translation
|
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
|
Topic.validates_associated :replies
|
||||||
replied_topic.valid?
|
replied_topic.valid?
|
||||||
|
|
Loading…
Reference in a new issue