@@ -1234,57 +1234,39 @@ def some_method_that_is_not_on_super
1234
1234
assert_equal ( "foo" , second_model_object . subject )
1235
1235
end
1236
1236
1237
- ClassWithDeprecatedAliasAttributeBehavior = Class . new ( ActiveRecord ::Base ) do
1238
- self . table_name = "topics"
1239
- alias_attribute :subject , :title
1237
+ test "#alias_attribute with an overridden original method does not use the overridden original method" do
1238
+ class_with_deprecated_alias_attribute_behavior = Class . new ( ActiveRecord ::Base ) do
1239
+ self . table_name = "topics"
1240
+ alias_attribute :subject , :title
1240
1241
1241
- def title_was
1242
- "overridden_title_was"
1242
+ def title_was
1243
+ "overridden_title_was"
1244
+ end
1243
1245
end
1244
- end
1245
1246
1246
- test "#alias_attribute with an overridden original method issues a deprecation" do
1247
- message = <<~MESSAGE . gsub ( "\n " , " " )
1248
- AttributeMethodsTest::ClassWithDeprecatedAliasAttributeBehavior model aliases
1249
- `title` and has a method called `title_was` defined.
1250
- Starting in Rails 7.2 `subject_was` will not be calling `title_was` anymore.
1251
- You may want to additionally define `subject_was` to preserve the current behavior.
1252
- MESSAGE
1253
-
1254
- obj = assert_deprecated ( message , ActiveRecord . deprecator ) do
1255
- ClassWithDeprecatedAliasAttributeBehavior . new
1256
- end
1247
+ obj = class_with_deprecated_alias_attribute_behavior . new
1257
1248
obj . title = "hey"
1258
1249
assert_equal ( "hey" , obj . subject )
1259
- assert_equal ( "overridden_title_was" , obj . subject_was )
1250
+ assert_nil ( obj . subject_was )
1260
1251
end
1261
1252
1262
- TitleWasOverride = Module . new do
1263
- def title_was
1264
- "overridden_title_was"
1253
+ test "#alias_attribute with an overridden original method from a module does not use the overridden original method" do
1254
+ title_was_override = Module . new do
1255
+ def title_was
1256
+ "overridden_title_was"
1257
+ end
1265
1258
end
1266
- end
1267
1259
1268
- ClassWithDeprecatedAliasAttributeBehaviorFromModule = Class . new ( ActiveRecord ::Base ) do
1269
- self . table_name = "topics"
1270
- include TitleWasOverride
1271
- alias_attribute :subject , :title
1272
- end
1273
-
1274
- test "#alias_attribute with an overridden original method from a module issues a deprecation" do
1275
- message = <<~MESSAGE . gsub ( "\n " , " " )
1276
- AttributeMethodsTest::ClassWithDeprecatedAliasAttributeBehaviorFromModule model aliases
1277
- `title` and has a method called `title_was` defined.
1278
- Starting in Rails 7.2 `subject_was` will not be calling `title_was` anymore.
1279
- You may want to additionally define `subject_was` to preserve the current behavior.
1280
- MESSAGE
1281
-
1282
- obj = assert_deprecated ( message , ActiveRecord . deprecator ) do
1283
- ClassWithDeprecatedAliasAttributeBehaviorFromModule . new
1260
+ class_with_deprecated_alias_attribute_behavior_from_module = Class . new ( ActiveRecord ::Base ) do
1261
+ self . table_name = "topics"
1262
+ include title_was_override
1263
+ alias_attribute :subject , :title
1284
1264
end
1265
+
1266
+ obj = class_with_deprecated_alias_attribute_behavior_from_module . new
1285
1267
obj . title = "hey"
1286
1268
assert_equal ( "hey" , obj . subject )
1287
- assert_equal ( "overridden_title_was" , obj . subject_was )
1269
+ assert_nil ( obj . subject_was )
1288
1270
end
1289
1271
1290
1272
ClassWithDeprecatedAliasAttributeBehaviorResolved = Class . new ( ActiveRecord ::Base ) do
@@ -1300,22 +1282,17 @@ def subject_was
1300
1282
end
1301
1283
end
1302
1284
1303
- class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBehaviorResolved
1304
- end
1305
-
1306
- test "#alias_attribute with an overridden original method along with an overridden alias method doesn't issue a deprecation" do
1307
- obj = assert_not_deprecated ( ActiveRecord . deprecator ) do
1308
- ClassWithDeprecatedAliasAttributeBehaviorResolved . new
1309
- end
1285
+ test "#alias_attribute with an overridden original method along with an overridden alias method uses the overridden alias method" do
1286
+ obj = ClassWithDeprecatedAliasAttributeBehaviorResolved . new
1310
1287
obj . title = "hey"
1311
1288
assert_equal ( "hey" , obj . subject )
1312
1289
assert_equal ( "overridden_subject_was" , obj . subject_was )
1313
1290
end
1314
1291
1315
- test "#alias_attribute with an overridden original method along with an overridden alias method in a parent class doesn't issue a deprecation " do
1316
- obj = assert_not_deprecated ( ActiveRecord . deprecator ) do
1317
- ChildWithDeprecatedBehaviorResolved . new
1318
- end
1292
+ test "#alias_attribute with an overridden original method along with an overridden alias method in a parent class uses the overridden alias method " do
1293
+ child_with_deprecated_behavior_resolved = Class . new ( ClassWithDeprecatedAliasAttributeBehaviorResolved )
1294
+
1295
+ obj = child_with_deprecated_behavior_resolved . new
1319
1296
obj . title = "hey"
1320
1297
assert_equal ( "hey" , obj . subject )
1321
1298
assert_equal ( "overridden_subject_was" , obj . subject_was )
@@ -1355,58 +1332,78 @@ class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBeh
1355
1332
assert_equal 123_456 , object . id_value
1356
1333
end
1357
1334
1358
- ClassWithGeneratedAttributeMethodTarget = Class . new ( ActiveRecord ::Base ) do
1359
- self . table_name = "topics"
1360
- alias_attribute :saved_title , :title_in_database
1361
- end
1335
+ test "#alias_attribute with an _in_database method issues raises an error" do
1336
+ class_with_generated_attribute_method_target = Class . new ( ActiveRecord ::Base ) do
1337
+ def self . name
1338
+ "ClassWithGeneratedAttributeMethodTarget"
1339
+ end
1340
+
1341
+ self . table_name = "topics"
1342
+
1343
+ alias_attribute :saved_title , :title_in_database
1344
+ end
1362
1345
1363
- test "#alias_attribute with an _in_database method issues a deprecation warning" do
1364
- message = <<~MESSAGE . gsub ( "\n " , " " )
1365
- AttributeMethodsTest::ClassWithGeneratedAttributeMethodTarget model aliases
1346
+ message = <<~MESSAGE . squish
1347
+ ClassWithGeneratedAttributeMethodTarget model aliases
1366
1348
`title_in_database`, but `title_in_database` is not an attribute.
1367
- Starting in Rails 7.2, alias_attribute with non-attribute targets will raise.
1368
1349
Use `alias_method :saved_title, :title_in_database` or define the method manually.
1369
1350
MESSAGE
1370
1351
1371
- obj = assert_deprecated ( message , ActiveRecord . deprecator ) do
1372
- ClassWithGeneratedAttributeMethodTarget . new
1352
+ error = assert_raises ( ArgumentError ) do
1353
+ class_with_generated_attribute_method_target . new
1373
1354
end
1374
- obj . title = "A river runs through it"
1375
- assert_nil obj . saved_title
1376
- obj . save
1377
- assert_equal "A river runs through it" , obj . saved_title
1355
+
1356
+ assert_equal message , error . message
1378
1357
end
1379
1358
1380
- ClassWithEnumMethodTarget = Class . new ( ActiveRecord ::Base ) do
1381
- self . table_name = "books"
1359
+ test "#alias_attribute with enum method raises an error" do
1360
+ class_with_enum_method_target = Class . new ( ActiveRecord ::Base ) do
1361
+ def self . name
1362
+ "ClassWithEnumMethodTarget"
1363
+ end
1382
1364
1383
- attribute :status , :string
1365
+ self . table_name = "books"
1384
1366
1385
- enum :status , {
1386
- pending : "0" ,
1387
- completed : "1" ,
1388
- }
1389
- alias_attribute :is_pending? , :pending?
1390
- end
1367
+ attribute :status , :string
1391
1368
1392
- test "#alias_attribute with enum method issues a deprecation warning" do
1393
- message = <<~MESSAGE . gsub ( "\n " , " " )
1394
- 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.
1369
+ enum :status , {
1370
+ pending : "0" ,
1371
+ completed : "1" ,
1372
+ }
1373
+ alias_attribute :is_pending? , :pending?
1374
+ end
1375
+
1376
+ message = <<~MESSAGE . squish
1377
+ ClassWithEnumMethodTarget model aliases `pending?`, but `pending?` is not an attribute. Use `alias_method :is_pending?, :pending?` or define the method manually.
1395
1378
MESSAGE
1396
1379
1397
- obj = assert_deprecated ( message , ActiveRecord . deprecator ) do
1398
- ClassWithEnumMethodTarget . new
1380
+ error = assert_raises ( ArgumentError ) do
1381
+ class_with_enum_method_target . new
1399
1382
end
1400
- obj . status = "pending"
1401
- assert_predicate obj , :pending?
1402
- assert_predicate obj , :is_pending?
1383
+ assert_equal message , error . message
1403
1384
end
1404
1385
1405
- ClassWithAssociationTarget = Class . new ( ActiveRecord ::Base ) do
1406
- self . table_name = "books"
1407
- belongs_to :author
1386
+ test "#alias_attribute with an association method raises an error" do
1387
+ class_with_association_target = Class . new ( ActiveRecord ::Base ) do
1388
+ def self . name
1389
+ "ClassWithAssociationTarget"
1390
+ end
1391
+
1392
+ self . table_name = "books"
1393
+
1394
+ belongs_to :author
1395
+
1396
+ alias_attribute :written_by , :author
1397
+ end
1408
1398
1409
- alias_attribute :written_by , :author
1399
+ message = <<~MESSAGE . squish
1400
+ ClassWithAssociationTarget model aliases `author`, but `author` is not an attribute. Use `alias_method :written_by, :author` or define the method manually.
1401
+ MESSAGE
1402
+
1403
+ error = assert_raises ( ArgumentError ) do
1404
+ class_with_association_target . new
1405
+ end
1406
+ assert_equal message , error . message
1410
1407
end
1411
1408
1412
1409
test "#alias_attribute method on a STI class is available on subclasses" do
@@ -1425,38 +1422,31 @@ class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBeh
1425
1422
assert_equal "Text" , comment . text
1426
1423
end
1427
1424
1428
- test "#alias_attribute with an association method issues a deprecation warning" do
1429
- message = <<~MESSAGE . gsub ( "\n " , " " )
1430
- 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.
1431
- MESSAGE
1432
1425
1433
- obj = assert_deprecated ( message , ActiveRecord . deprecator ) do
1434
- ClassWithAssociationTarget . new
1435
- end
1436
- obj . author = Author . new ( name : "Octavia E. Butler" )
1437
- assert_equal "Octavia E. Butler" , obj . written_by . name
1438
- end
1426
+ test "#alias_attribute with a manually defined method raises an error" do
1427
+ class_with_aliased_manually_defined_method = Class . new ( ActiveRecord ::Base ) do
1428
+ def self . name
1429
+ "ClassWithAliasedManuallyDefinedMethod"
1430
+ end
1439
1431
1440
- ClassWithAliasedManuallyDefinedMethod = Class . new ( ActiveRecord ::Base ) do
1441
- self . table_name = "books"
1442
- alias_attribute :print , :publish
1432
+ self . table_name = "books"
1443
1433
1444
- def publish
1445
- "Publishing!"
1434
+ alias_attribute :print , :publish
1435
+
1436
+ def publish
1437
+ "Publishing!"
1438
+ end
1446
1439
end
1447
- end
1448
1440
1449
- test "#alias_attribute with a manually defined method issues a deprecation warning" do
1450
- message = <<~MESSAGE . gsub ( "\n " , " " )
1451
- AttributeMethodsTest::ClassWithAliasedManuallyDefinedMethod model aliases `publish`,
1452
- but `publish` is not an attribute.
1453
- Starting in Rails 7.2, alias_attribute with non-attribute targets will raise.
1441
+ message = <<~MESSAGE . squish
1442
+ ClassWithAliasedManuallyDefinedMethod model aliases `publish`, but `publish` is not an attribute.
1454
1443
Use `alias_method :print, :publish` or define the method manually.
1455
1444
MESSAGE
1456
1445
1457
- assert_deprecated ( message , ActiveRecord . deprecator ) do
1458
- ClassWithAliasedManuallyDefinedMethod . new
1446
+ error = assert_raises ( ArgumentError ) do
1447
+ class_with_aliased_manually_defined_method . new
1459
1448
end
1449
+ assert_equal message , error . message
1460
1450
end
1461
1451
1462
1452
private
0 commit comments