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

Fix i18n of attributes with multi-digit indexes

Follow-up to #33615 and #37447.
This commit is contained in:
Jonathan Hefner 2019-10-17 09:23:00 -05:00 committed by George Claghorn
parent 7fd5ca79a5
commit cd619e9781
3 changed files with 9 additions and 9 deletions

View file

@ -17,7 +17,7 @@ module ActiveModel
attribute = attribute.to_s attribute = attribute.to_s
if i18n_customize_full_message && base_class.respond_to?(:i18n_scope) if i18n_customize_full_message && base_class.respond_to?(:i18n_scope)
attribute = attribute.remove(/\[\d\]/) attribute = attribute.remove(/\[\d+\]/)
parts = attribute.split(".") parts = attribute.split(".")
attribute_name = parts.pop attribute_name = parts.pop
namespace = parts.join("/") unless parts.empty? namespace = parts.join("/") unless parts.empty?
@ -69,7 +69,7 @@ module ActiveModel
if base.class.respond_to?(:i18n_scope) if base.class.respond_to?(:i18n_scope)
i18n_scope = base.class.i18n_scope.to_s i18n_scope = base.class.i18n_scope.to_s
attribute = attribute.to_s.remove(/\[\d\]/) attribute = attribute.to_s.remove(/\[\d+\]/)
defaults = base.class.lookup_ancestors.flat_map do |klass| defaults = base.class.lookup_ancestors.flat_map do |klass|
[ :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}", [ :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",

View file

@ -158,7 +158,7 @@ class ErrorTest < ActiveModel::TestCase
I18n.backend.store_translations(:en, activemodel: { errors: { models: { 'error_test/manager': { I18n.backend.store_translations(:en, activemodel: { errors: { models: { 'error_test/manager': {
attributes: { reports: { name: { presence: "must be present" } } } } } } }) attributes: { reports: { name: { presence: "must be present" } } } } } } })
error = ActiveModel::Error.new(Manager.new, :'reports[0].name', :presence) error = ActiveModel::Error.new(Manager.new, :'reports[123].name', :presence)
assert_equal "must be present", error.message assert_equal "must be present", error.message
end end

View file

@ -123,8 +123,8 @@ class I18nValidationTest < ActiveModel::TestCase
errors: { models: { 'person/contacts/addresses': { attributes: { street: { format: "%{message}" } } } } } }) errors: { models: { 'person/contacts/addresses': { attributes: { street: { format: "%{message}" } } } } } })
person = person_class.new person = person_class.new
assert_equal "cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].street', "cannot be blank") assert_equal "cannot be blank", person.errors.full_message(:'contacts[0]/addresses[123].street', "cannot be blank")
assert_equal "Contacts/addresses country cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].country', "cannot be blank") assert_equal "Contacts/addresses country cannot be blank", person.errors.full_message(:'contacts[0]/addresses[123].country', "cannot be blank")
end end
def test_errors_full_messages_with_indexed_deeply_nested_attributes_and_model_format def test_errors_full_messages_with_indexed_deeply_nested_attributes_and_model_format
@ -134,8 +134,8 @@ class I18nValidationTest < ActiveModel::TestCase
errors: { models: { 'person/contacts/addresses': { format: "%{message}" } } } }) errors: { models: { 'person/contacts/addresses': { format: "%{message}" } } } })
person = person_class.new person = person_class.new
assert_equal "cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].street', "cannot be blank") assert_equal "cannot be blank", person.errors.full_message(:'contacts[0]/addresses[123].street', "cannot be blank")
assert_equal "cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].country', "cannot be blank") assert_equal "cannot be blank", person.errors.full_message(:'contacts[0]/addresses[123].country', "cannot be blank")
end end
def test_errors_full_messages_with_indexed_deeply_nested_attributes_and_i18n_attribute_name def test_errors_full_messages_with_indexed_deeply_nested_attributes_and_i18n_attribute_name
@ -146,8 +146,8 @@ class I18nValidationTest < ActiveModel::TestCase
}) })
person = person_class.new person = person_class.new
assert_equal "Contacts/addresses street cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].street', "cannot be blank") assert_equal "Contacts/addresses street cannot be blank", person.errors.full_message(:'contacts[0]/addresses[123].street', "cannot be blank")
assert_equal "Country cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].country', "cannot be blank") assert_equal "Country cannot be blank", person.errors.full_message(:'contacts[0]/addresses[123].country', "cannot be blank")
end end
def test_errors_full_messages_with_indexed_deeply_nested_attributes_without_i18n_config def test_errors_full_messages_with_indexed_deeply_nested_attributes_without_i18n_config