Switch to use `class_attribute`:

- Since `ActiveModel::Error` can now be inherited by
  `ActiveModel::NestedError`, when the latter generates a
  `full_message`, the `i18n_customize_full_message` accessor set in
  the parent class is not set.

  This commit fixes that by using a `class_attribute` instead.
This commit is contained in:
Edouard CHIN 2019-07-10 23:30:44 +02:00
parent b677adede0
commit d204a09df2
2 changed files with 16 additions and 4 deletions

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
require "active_support/core_ext/class/attribute"
module ActiveModel
# == Active \Model \Error
#
@ -8,10 +10,7 @@ module ActiveModel
CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict]
MESSAGE_OPTIONS = [:message]
class << self
attr_accessor :i18n_customize_full_message # :nodoc:
end
self.i18n_customize_full_message = false
class_attribute :i18n_customize_full_message, default: false
def self.full_message(attribute, message, base_class) # :nodoc:
return message if attribute == :base

View File

@ -59,6 +59,19 @@ class I18nValidationTest < ActiveModel::TestCase
assert_equal "Name test cannot be blank", person.errors.full_message(:name_test, "cannot be blank")
end
def test_errors_full_messages_on_nested_error_uses_attribute_format
ActiveModel::Error.i18n_customize_full_message = true
I18n.backend.store_translations("en", activemodel: {
errors: { models: { person: { attributes: { gender: "Gender" } } } },
attributes: { "person/contacts": { gender: "Gender" } }
})
person = person_class.new
error = ActiveModel::Error.new(person, :gender, "can't be blank")
person.errors.import(error, attribute: "person[0].contacts.gender")
assert_equal ["Gender can't be blank"], person.errors.full_messages
end
def test_errors_full_messages_uses_attribute_format
ActiveModel::Error.i18n_customize_full_message = true