ActiveModel.full_message interaction with index_errors

This commit is contained in:
Martin Larochelle 2018-08-14 11:52:26 -04:00
parent f2970a08b5
commit 0b54641878
2 changed files with 63 additions and 3 deletions

@ -379,9 +379,11 @@ def full_messages_for(attribute)
# * <tt>errors.format</tt>
def full_message(attribute, message)
return message if attribute == :base
attribute = attribute.to_s
if self.class.i18n_full_message && @base.class.respond_to?(:i18n_scope)
parts = attribute.to_s.split(".")
attribute = attribute.remove(/\[\d\]/)
parts = attribute.split(".")
attribute_name = parts.pop
namespace = parts.join("/") unless parts.empty?
attributes_scope = "#{@base.class.i18n_scope}.errors.models"
@ -410,8 +412,9 @@ def full_message(attribute, message)
defaults << :"errors.format"
defaults << "%{attribute} %{message}"
attr_name = attribute.to_s.tr(".", "_").humanize
attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
attr_name = attribute.tr(".", "_").humanize
attr_name = @base.class.human_attribute_name(attribute.to_sym, default: attr_name)
I18n.t(defaults.shift,
default: defaults,
attribute: attr_name,

@ -101,6 +101,63 @@ def test_errors_full_messages_uses_deeply_nested_model_model_format
assert_equal "cannot be blank", person.errors.full_message(:'contacts/addresses.country', "cannot be blank")
end
def test_errors_full_messages_with_indexed_deeply_nested_attributes_and_attributes_format
ActiveModel::Errors.i18n_full_message = true
I18n.backend.store_translations("en", activemodel: {
errors: { models: { 'person/contacts/addresses': { attributes: { street: { format: "%{message}" } } } } } })
person = Person.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")
end
def test_errors_full_messages_with_indexed_deeply_nested_attributes_and_model_format
ActiveModel::Errors.i18n_full_message = true
I18n.backend.store_translations("en", activemodel: {
errors: { models: { 'person/contacts/addresses': { format: "%{message}" } } } })
person = Person.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")
end
def test_errors_full_messages_with_indexed_deeply_nested_attributes_and_i18n_attribute_name
ActiveModel::Errors.i18n_full_message = true
I18n.backend.store_translations("en", activemodel: {
attributes: { 'person/contacts/addresses': { country: "Country" } }
})
person = Person.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")
end
def test_errors_full_messages_with_indexed_deeply_nested_attributes_without_i18n_config
ActiveModel::Errors.i18n_full_message = false
I18n.backend.store_translations("en", activemodel: {
errors: { models: { 'person/contacts/addresses': { attributes: { street: { format: "%{message}" } } } } } })
person = Person.new
assert_equal "Contacts[0]/addresses[0] street cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].street', "cannot be blank")
assert_equal "Contacts[0]/addresses[0] country cannot be blank", person.errors.full_message(:'contacts[0]/addresses[0].country', "cannot be blank")
end
def test_errors_full_messages_with_i18n_attribute_name_without_i18n_config
ActiveModel::Errors.i18n_full_message = false
I18n.backend.store_translations("en", activemodel: {
attributes: { 'person/contacts[0]/addresses[0]': { country: "Country" } }
})
person = Person.new
assert_equal "Contacts[0]/addresses[0] 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")
end
# ActiveModel::Validations
# A set of common cases for ActiveModel::Validations message generation that