From cd619e97818110201d38e572c95ab7393bc70f4a Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Thu, 17 Oct 2019 09:23:00 -0500 Subject: [PATCH] Fix i18n of attributes with multi-digit indexes Follow-up to #33615 and #37447. --- activemodel/lib/active_model/error.rb | 4 ++-- activemodel/test/cases/error_test.rb | 2 +- .../test/cases/validations/i18n_validation_test.rb | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/activemodel/lib/active_model/error.rb b/activemodel/lib/active_model/error.rb index 74e5394352..d529fbc365 100644 --- a/activemodel/lib/active_model/error.rb +++ b/activemodel/lib/active_model/error.rb @@ -17,7 +17,7 @@ module ActiveModel attribute = attribute.to_s if i18n_customize_full_message && base_class.respond_to?(:i18n_scope) - attribute = attribute.remove(/\[\d\]/) + attribute = attribute.remove(/\[\d+\]/) parts = attribute.split(".") attribute_name = parts.pop namespace = parts.join("/") unless parts.empty? @@ -69,7 +69,7 @@ module ActiveModel if base.class.respond_to?(:i18n_scope) 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| [ :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}", diff --git a/activemodel/test/cases/error_test.rb b/activemodel/test/cases/error_test.rb index 7ef0375fb5..ce6c73fe8a 100644 --- a/activemodel/test/cases/error_test.rb +++ b/activemodel/test/cases/error_test.rb @@ -158,7 +158,7 @@ class ErrorTest < ActiveModel::TestCase I18n.backend.store_translations(:en, activemodel: { errors: { models: { 'error_test/manager': { 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 end diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index c81649f493..2dc3e681c4 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -123,8 +123,8 @@ class I18nValidationTest < ActiveModel::TestCase errors: { models: { 'person/contacts/addresses': { attributes: { street: { format: "%{message}" } } } } } }) person = person_class.new - assert_equal "cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].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 "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[123].country', "cannot be blank") end 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}" } } } }) 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[0].country', "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[123].country', "cannot be blank") end 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 - assert_equal "Contacts/addresses street cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].street', "cannot be blank") - assert_equal "Country cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].country', "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[123].country', "cannot be blank") end def test_errors_full_messages_with_indexed_deeply_nested_attributes_without_i18n_config