Skip to content

Commit 36ea89a

Browse files
committed
Merge pull request rails#20668 from repinel/rename-restrict-dependent-destroy-i18n-key
Deprecate the keys for association `restrict_dependent_destroy` errors in favor of new key names
2 parents 51d333a + b184398 commit 36ea89a

File tree

6 files changed

+73
-4
lines changed

6 files changed

+73
-4
lines changed

activerecord/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
* Deprecate the keys for association `restrict_dependent_destroy` errors in favor
2+
of new key names.
3+
4+
Previously `has_one` and `has_many` associations were using the
5+
`one` and `many` keys respectively. Both of these keys have special
6+
meaning in I18n (they are considered to be pluralizations) so by
7+
renaming them to `has_one` and `has_many` we make the messages more explicit
8+
and most importantly they don't clash with linguistical systems that need to
9+
validate translation keys (and their pluralizations).
10+
11+
The `:'restrict_dependent_destroy.one'` key should be replaced with
12+
`:'restrict_dependent_destroy.has_one'`, and `:'restrict_dependent_destroy.many'`
13+
with `:'restrict_dependent_destroy.has_many'`.
14+
15+
*Roque Pinel*, *Christopher Dell*
16+
117
* Fix state being carried over from previous transaction.
218

319
Considering the following example where `name` is a required attribute.

activerecord/lib/active_record/associations/has_many_association.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ def handle_dependency
1616
when :restrict_with_error
1717
unless empty?
1818
record = klass.human_attribute_name(reflection.name).downcase
19-
owner.errors.add(:base, :"restrict_dependent_destroy.many", record: record)
19+
message = owner.errors.generate_message(:base, :'restrict_dependent_destroy.many', record: record, raise: true) rescue nil
20+
if message
21+
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
22+
The error key `:'restrict_dependent_destroy.many'` has been deprecated and will be removed in Rails 5.1.
23+
Please use `:'restrict_dependent_destroy.has_many'` instead.
24+
MESSAGE
25+
end
26+
owner.errors.add(:base, message || :'restrict_dependent_destroy.has_many', record: record)
2027
throw(:abort)
2128
end
2229

activerecord/lib/active_record/associations/has_one_association.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ def handle_dependency
1212
when :restrict_with_error
1313
if load_target
1414
record = klass.human_attribute_name(reflection.name).downcase
15-
owner.errors.add(:base, :"restrict_dependent_destroy.one", record: record)
15+
message = owner.errors.generate_message(:base, :'restrict_dependent_destroy.one', record: record, raise: true) rescue nil
16+
if message
17+
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
18+
The error key `:'restrict_dependent_destroy.one'` has been deprecated and will be removed in Rails 5.1.
19+
Please use `:'restrict_dependent_destroy.has_one'` instead.
20+
MESSAGE
21+
end
22+
owner.errors.add(:base, message || :'restrict_dependent_destroy.has_one', record: record)
1623
throw(:abort)
1724
end
1825

activerecord/lib/active_record/locale/en.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ en:
1616
messages:
1717
record_invalid: "Validation failed: %{errors}"
1818
restrict_dependent_destroy:
19-
one: "Cannot delete record because a dependent %{record} exists"
20-
many: "Cannot delete record because dependent %{record} exist"
19+
has_one: "Cannot delete record because a dependent %{record} exists"
20+
has_many: "Cannot delete record because dependent %{record} exist"
2121
# Append your own errors here or at the model/attributes scope.
2222

2323
# You can define own errors for models or model attributes.

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,26 @@ def test_restrict_with_exception
14261426
assert firm.companies.exists?(:name => 'child')
14271427
end
14281428

1429+
def test_restrict_with_error_is_deprecated_using_key_many
1430+
I18n.backend = I18n::Backend::Simple.new
1431+
I18n.backend.store_translations :en, activerecord: { errors: { messages: { restrict_dependent_destroy: { many: 'message for deprecated key' } } } }
1432+
1433+
firm = RestrictedWithErrorFirm.create!(name: 'restrict')
1434+
firm.companies.create(name: 'child')
1435+
1436+
assert !firm.companies.empty?
1437+
1438+
assert_deprecated { firm.destroy }
1439+
1440+
assert !firm.errors.empty?
1441+
1442+
assert_equal 'message for deprecated key', firm.errors[:base].first
1443+
assert RestrictedWithErrorFirm.exists?(name: 'restrict')
1444+
assert firm.companies.exists?(name: 'child')
1445+
ensure
1446+
I18n.backend.reload!
1447+
end
1448+
14291449
def test_restrict_with_error
14301450
firm = RestrictedWithErrorFirm.create!(:name => 'restrict')
14311451
firm.companies.create(:name => 'child')

activerecord/test/cases/associations/has_one_associations_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,25 @@ def test_restrict_with_exception
178178
assert firm.account.present?
179179
end
180180

181+
def test_restrict_with_error_is_deprecated_using_key_one
182+
I18n.backend = I18n::Backend::Simple.new
183+
I18n.backend.store_translations :en, activerecord: { errors: { messages: { restrict_dependent_destroy: { one: 'message for deprecated key' } } } }
184+
185+
firm = RestrictedWithErrorFirm.create!(name: 'restrict')
186+
firm.create_account(credit_limit: 10)
187+
188+
assert_not_nil firm.account
189+
190+
assert_deprecated { firm.destroy }
191+
192+
assert !firm.errors.empty?
193+
assert_equal 'message for deprecated key', firm.errors[:base].first
194+
assert RestrictedWithErrorFirm.exists?(name: 'restrict')
195+
assert firm.account.present?
196+
ensure
197+
I18n.backend.reload!
198+
end
199+
181200
def test_restrict_with_error
182201
firm = RestrictedWithErrorFirm.create!(:name => 'restrict')
183202
firm.create_account(:credit_limit => 10)

0 commit comments

Comments
 (0)