Skip to content

Commit bb52c41

Browse files
authored
Merge pull request rails#49099 from eileencodes/fix-id_value-deprecation-warning-and-message
Fix alias_attribute deprecation warning and message
2 parents 96cc933 + 6297e60 commit bb52c41

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ def alias_attribute_method_definition(code_generator, pattern, new_name, old_nam
9696
should_warn = target_name == old_name
9797
if should_warn
9898
ActiveRecord.deprecator.warn(
99-
"#{self} model aliases `#{old_name}`, but #{old_name} is not an attribute. " \
100-
"Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise. " \
101-
"Use `alias_method :#{new_name}`, :#{old_name} or define the method manually."
99+
"#{self} model aliases `#{old_name}`, but `#{old_name}` is not an attribute. " \
100+
"Starting in Rails 7.2, alias_attribute with non-attribute targets will raise. " \
101+
"Use `alias_method :#{new_name}, :#{old_name}` or define the method manually."
102102
)
103103
end
104104
super

activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -444,44 +444,51 @@ def test_no_primary_key
444444
end
445445

446446
class Barcode < ActiveRecord::Base
447+
end
448+
449+
class BarcodeCustomPk < ActiveRecord::Base
447450
self.primary_key = "code"
448451
end
449452

450453
def test_copy_table_with_existing_records_have_custom_primary_key
451-
connection = Barcode.connection
452-
connection.create_table(:barcodes, primary_key: "code", id: :string, limit: 42, force: true) do |t|
454+
connection = BarcodeCustomPk.connection
455+
connection.create_table(:barcode_custom_pks, primary_key: "code", id: :string, limit: 42, force: true) do |t|
453456
t.text :other_attr
454457
end
455458
code = "214fe0c2-dd47-46df-b53b-66090b3c1d40"
456-
Barcode.create!(code: code, other_attr: "xxx")
459+
BarcodeCustomPk.create!(code: code, other_attr: "xxx")
457460

458-
connection.remove_column("barcodes", "other_attr")
461+
connection.remove_column("barcode_custom_pks", "other_attr")
459462

460-
assert_equal code, Barcode.first.id
463+
assert_equal code, BarcodeCustomPk.first.id
461464
ensure
462-
Barcode.reset_column_information
465+
BarcodeCustomPk.reset_column_information
466+
end
467+
468+
class BarcodeCpk < ActiveRecord::Base
469+
self.primary_key = ["region", "code"]
463470
end
464471

465472
def test_copy_table_with_composite_primary_keys
466-
connection = Barcode.connection
467-
connection.create_table(:barcodes, primary_key: ["region", "code"], force: true) do |t|
473+
connection = BarcodeCpk.connection
474+
connection.create_table(:barcode_cpks, primary_key: ["region", "code"], force: true) do |t|
468475
t.string :region
469476
t.string :code
470477
t.text :other_attr
471478
end
472479
region = "US"
473480
code = "214fe0c2-dd47-46df-b53b-66090b3c1d40"
474-
Barcode.create!(region: region, code: code, other_attr: "xxx")
481+
BarcodeCpk.create!(region: region, code: code, other_attr: "xxx")
475482

476-
connection.remove_column("barcodes", "other_attr")
483+
connection.remove_column("barcode_cpks", "other_attr")
477484

478-
assert_equal ["region", "code"], connection.primary_keys("barcodes")
485+
assert_equal ["region", "code"], connection.primary_keys("barcode_cpks")
479486

480-
barcode = Barcode.first
487+
barcode = BarcodeCpk.first
481488
assert_equal region, barcode.region
482489
assert_equal code, barcode.code
483490
ensure
484-
Barcode.reset_column_information
491+
BarcodeCpk.reset_column_information
485492
end
486493

487494
def test_custom_primary_key_in_create_table

activerecord/test/cases/attribute_methods_test.rb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,9 +1209,10 @@ def title_was
12091209

12101210
test "#alias_attribute with an overridden original method issues a deprecation" do
12111211
message = <<~MESSAGE.gsub("\n", " ")
1212-
AttributeMethodsTest::ClassWithDeprecatedAliasAttributeBehavior model aliases `title` and has a method called
1213-
`title_was` defined. Starting in Rails 7.2 `subject_was` will not be calling `title_was` anymore.
1214-
You may want to additionally define `subject_was` to preserve the current behavior.
1212+
AttributeMethodsTest::ClassWithDeprecatedAliasAttributeBehavior model aliases
1213+
`title` and has a method called `title_was` defined.
1214+
Starting in Rails 7.2 `subject_was` will not be calling `title_was` anymore.
1215+
You may want to additionally define `subject_was` to preserve the current behavior.
12151216
MESSAGE
12161217

12171218
obj = assert_deprecated(message, ActiveRecord.deprecator) do
@@ -1236,9 +1237,10 @@ def title_was
12361237

12371238
test "#alias_attribute with an overridden original method from a module issues a deprecation" do
12381239
message = <<~MESSAGE.gsub("\n", " ")
1239-
AttributeMethodsTest::ClassWithDeprecatedAliasAttributeBehaviorFromModule model aliases `title` and has a method
1240-
called `title_was` defined. Starting in Rails 7.2 `subject_was` will not be calling `title_was` anymore.
1241-
You may want to additionally define `subject_was` to preserve the current behavior.
1240+
AttributeMethodsTest::ClassWithDeprecatedAliasAttributeBehaviorFromModule model aliases
1241+
`title` and has a method called `title_was` defined.
1242+
Starting in Rails 7.2 `subject_was` will not be calling `title_was` anymore.
1243+
You may want to additionally define `subject_was` to preserve the current behavior.
12421244
MESSAGE
12431245

12441246
obj = assert_deprecated(message, ActiveRecord.deprecator) do
@@ -1324,8 +1326,10 @@ class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBeh
13241326

13251327
test "#alias_attribute with an _in_database method issues a deprecation warning" do
13261328
message = <<~MESSAGE.gsub("\n", " ")
1327-
AttributeMethodsTest::ClassWithGeneratedAttributeMethodTarget model aliases `title_in_database`, but title_in_database is not an attribute.
1328-
Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise. Use `alias_method :saved_title`, :title_in_database or define the method manually.
1329+
AttributeMethodsTest::ClassWithGeneratedAttributeMethodTarget model aliases
1330+
`title_in_database`, but `title_in_database` is not an attribute.
1331+
Starting in Rails 7.2, alias_attribute with non-attribute targets will raise.
1332+
Use `alias_method :saved_title, :title_in_database` or define the method manually.
13291333
MESSAGE
13301334

13311335
obj = assert_deprecated(message, ActiveRecord.deprecator) do
@@ -1350,9 +1354,7 @@ class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBeh
13501354

13511355
test "#alias_attribute with enum method issues a deprecation warning" do
13521356
message = <<~MESSAGE.gsub("\n", " ")
1353-
AttributeMethodsTest::ClassWithEnumMethodTarget model aliases `pending?`, but pending? is not an attribute.
1354-
Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise.
1355-
Use `alias_method :is_pending?`, :pending? or define the method manually.
1357+
AttributeMethodsTest::ClassWithEnumMethodTarget model aliases `pending?`, but `pending?` is not an attribute. Starting in Rails 7.2, alias_attribute with non-attribute targets will raise. Use `alias_method :is_pending?, :pending?` or define the method manually.
13561358
MESSAGE
13571359

13581360
obj = assert_deprecated(message, ActiveRecord.deprecator) do
@@ -1372,9 +1374,7 @@ class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBeh
13721374

13731375
test "#alias_attribute with an association method issues a deprecation warning" do
13741376
message = <<~MESSAGE.gsub("\n", " ")
1375-
AttributeMethodsTest::ClassWithAssociationTarget model aliases `author`, but author is not an attribute.
1376-
Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise.
1377-
Use `alias_method :written_by`, :author or define the method manually.
1377+
AttributeMethodsTest::ClassWithAssociationTarget model aliases `author`, but `author` is not an attribute. Starting in Rails 7.2, alias_attribute with non-attribute targets will raise. Use `alias_method :written_by, :author` or define the method manually.
13781378
MESSAGE
13791379

13801380
obj = assert_deprecated(message, ActiveRecord.deprecator) do
@@ -1395,9 +1395,10 @@ def publish
13951395

13961396
test "#alias_attribute with a manually defined method issues a deprecation warning" do
13971397
message = <<~MESSAGE.gsub("\n", " ")
1398-
AttributeMethodsTest::ClassWithAliasedManuallyDefinedMethod model aliases `publish`, but publish is not an attribute.
1399-
Starting in Rails 7.2 `, alias_attribute with non-attribute targets will raise.
1400-
Use `alias_method :print`, :publish or define the method manually.
1398+
AttributeMethodsTest::ClassWithAliasedManuallyDefinedMethod model aliases `publish`,
1399+
but `publish` is not an attribute.
1400+
Starting in Rails 7.2, alias_attribute with non-attribute targets will raise.
1401+
Use `alias_method :print, :publish` or define the method manually.
14011402
MESSAGE
14021403

14031404
assert_deprecated(message, ActiveRecord.deprecator) do

0 commit comments

Comments
 (0)