Skip to content

Commit

Permalink
Keep the attribute's name translation as is:
Browse files Browse the repository at this point in the history
- When an error is added to a record due to
  `restrict_dependent_destroy` error, we were always downcasing its
  attribute name even when an application had specified a translation.

  Now, the attribute's name will be downcased only when no translation
  exists.

  Fix rails#53448
  • Loading branch information
Edouard-chin committed Jan 16, 2025
1 parent fa6886b commit 70cd25b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion activemodel/lib/active_model/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ def human_attribute_name(attribute, options = {})
defaults << options[:default] if options[:default]
defaults << MISSING_TRANSLATION unless raise_on_missing

capitalize_missing_translation = options.delete(:_capitalize) { true }
translation = I18n.translate(defaults.shift, count: 1, raise: raise_on_missing, **options, default: defaults)
translation = attribute.humanize if translation == MISSING_TRANSLATION
translation = attribute.humanize(capitalize: capitalize_missing_translation) if translation == MISSING_TRANSLATION
translation
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def handle_dependency

when :restrict_with_error
unless empty?
record = owner.class.human_attribute_name(reflection.name).downcase
record = owner.class.human_attribute_name(reflection.name, _capitalize: false)
owner.errors.add(:base, :'restrict_dependent_destroy.has_many', record: record)
throw(:abort)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def handle_dependency

when :restrict_with_error
if load_target
record = owner.class.human_attribute_name(reflection.name).downcase
record = owner.class.human_attribute_name(reflection.name, _capitalize: false)
owner.errors.add(:base, :'restrict_dependent_destroy.has_one', record: record)
throw(:abort)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,7 @@ def test_restrict_with_error

def test_restrict_with_error_with_locale
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations "en", activerecord: { attributes: { restricted_with_error_firm: { companies: "client companies" } } }
I18n.backend.store_translations "en", activerecord: { attributes: { restricted_with_error_firm: { companies: "LLC companies" } } }
firm = RestrictedWithErrorFirm.create!(name: "restrict")
firm.companies.create(name: "child")

Expand All @@ -2016,7 +2016,7 @@ def test_restrict_with_error_with_locale

assert_not_empty firm.errors

assert_equal "Cannot delete record because dependent client companies exist", firm.errors[:base].first
assert_equal "Cannot delete record because dependent LLC companies exist", firm.errors[:base].first
assert RestrictedWithErrorFirm.exists?(name: "restrict")
assert firm.companies.exists?(name: "child")
ensure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_restrict_with_error

def test_restrict_with_error_with_locale
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations "en", activerecord: { attributes: { restricted_with_error_firm: { account: "firm account" } } }
I18n.backend.store_translations "en", activerecord: { attributes: { restricted_with_error_firm: { account: "LLC account" } } }
firm = RestrictedWithErrorFirm.create!(name: "restrict")
firm.create_account(credit_limit: 10)

Expand All @@ -252,7 +252,7 @@ def test_restrict_with_error_with_locale
firm.destroy

assert_not_empty firm.errors
assert_equal "Cannot delete record because a dependent firm account exists", firm.errors[:base].first
assert_equal "Cannot delete record because a dependent LLC account exists", firm.errors[:base].first
assert RestrictedWithErrorFirm.exists?(name: "restrict")
assert_predicate firm.account, :present?
ensure
Expand Down

0 comments on commit 70cd25b

Please sign in to comment.