Skip to content

Commit

Permalink
Make sure nested base errors are translatable
Browse files Browse the repository at this point in the history
If the user defined a translation to a nested error on base we should
look it up in the same way we do for the other attributes.

If no translation is set, we fallback to the name of the association.

Fixes #48884.
  • Loading branch information
rafaelfranca committed Aug 4, 2023
1 parent 3b3e820 commit 40c616c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
4 changes: 1 addition & 3 deletions activemodel/lib/active_model/error.rb
Expand Up @@ -49,9 +49,7 @@ def self.full_message(attribute, message, base) # :nodoc:
defaults << :"errors.format"
defaults << "%{attribute} %{message}"

attribute = attribute.remove(/\.base\z/)

attr_name = attribute.tr(".", "_").humanize
attr_name = attribute.remove(/\.base\z/).tr(".", "_").humanize
attr_name = base_class.human_attribute_name(attribute, {
default: attr_name,
base: base,
Expand Down
2 changes: 1 addition & 1 deletion activemodel/test/cases/error_test.rb
Expand Up @@ -219,7 +219,7 @@ def test_initialize

test "full_message returns the given message when the attribute contains base" do
error = ActiveModel::Error.new(Person.new, :"foo.base", "press the button")
assert_equal "foo press the button", error.full_message
assert_equal "foo.base press the button", error.full_message
end

# details
Expand Down
50 changes: 50 additions & 0 deletions activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -599,6 +599,7 @@ def self.name; "Person"; end
assert_not_predicate p, :valid?
assert_equal [{ error: "should be favorite" }], p.errors.details[:"references[1].base"]
assert_equal "should be favorite", p.errors[:"references[1].base"].first
assert_equal ["References[1] should be favorite"], p.errors.full_messages
end

def test_indexed_errors_should_be_properly_translated
Expand Down Expand Up @@ -649,6 +650,55 @@ def self.name; "Person"; end
I18n.backend = I18n::Backend::Simple.new
end

def test_indexed_errors_on_base_attribute_should_be_properly_translated
I18n.backend.store_translations(
:en,
activerecord: {
attributes: {
person: {
reference: "Super reference",
},
reference: {
base: ""
}
}
}
)
reference = Class.new(ActiveRecord::Base) do
self.table_name = "references"
def self.name; "Reference"; end

validate :should_be_favorite
validates_presence_of :job_id

private
def should_be_favorite
errors.add(:base, "should be favorite") unless favorite?
end
end

person = Class.new(ActiveRecord::Base) do
self.table_name = "people"
def self.name; "Person"; end

has_one :reference, autosave: true, anonymous_class: reference
validates :reference, presence: true
end

p = person.new
assert_not_predicate p, :valid?
assert_equal ["Super reference can’t be blank"], p.errors.full_messages

reference_invalid = reference.new(favorite: false)
p.reference = reference_invalid

assert_not_predicate reference_invalid, :valid?
assert_not_predicate p, :valid?
assert_equal [" should be favorite", "Reference job can’t be blank"], p.errors.full_messages
ensure
I18n.backend = I18n::Backend::Simple.new
end

def test_errors_details_should_be_indexed_when_global_flag_is_set
old_attribute_config = ActiveRecord.index_nested_attribute_errors
ActiveRecord.index_nested_attribute_errors = true
Expand Down

0 comments on commit 40c616c

Please sign in to comment.