@@ -253,6 +253,60 @@ def change_name
253
253
assert_not_equal @previously_updated_at , @developer . updated_at
254
254
end
255
255
256
+ def test_saving_an_unchanged_record_with_a_mutating_before_update_callback_updates_its_timestamp
257
+ klass = Class . new ( ActiveRecord ::Base ) do
258
+ self . table_name = "developers"
259
+
260
+ include Developer ::TimestampAliases
261
+
262
+ before_update :change_name
263
+
264
+ private
265
+ def change_name
266
+ return if new_record?
267
+
268
+ self . name = "Jack Bauer"
269
+ end
270
+ end
271
+
272
+ @developer = klass . create!
273
+ @previously_updated_at = @developer . updated_at
274
+ @previous_name = @developer . name
275
+
276
+ travel ( 1 . second ) do
277
+ @developer . save!
278
+ end
279
+
280
+ @developer . reload
281
+
282
+ assert_not_equal @previous_name , @developer . name
283
+ assert_not_equal @previously_updated_at , @developer . updated_at
284
+ end
285
+
286
+ def test_saving_an_unchanged_record_with_a_non_mutating_before_update_callback_does_not_update_its_timestamp
287
+ klass = Class . new ( ActiveRecord ::Base ) do
288
+ self . table_name = "developers"
289
+
290
+ include Developer ::TimestampAliases
291
+
292
+ before_update :change_name
293
+
294
+ private
295
+ def change_name ; end
296
+ end
297
+
298
+ @developer = klass . create!
299
+ @previously_updated_at = @developer . updated_at
300
+
301
+ travel ( 1 . second ) do
302
+ @developer . save!
303
+ end
304
+
305
+ @developer . reload
306
+
307
+ assert_equal @previously_updated_at , @developer . updated_at
308
+ end
309
+
256
310
def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
257
311
pet = Pet . first
258
312
owner = pet . owner
@@ -469,7 +523,34 @@ def self.name; "Toy"; end
469
523
assert_not_equal time , pet . updated_at
470
524
end
471
525
472
- def test_timestamp_column_values_are_present_in_the_callbacks
526
+ def test_timestamp_column_values_are_present_in_create_callbacks
527
+ klass = Class . new ( ActiveRecord ::Base ) do
528
+ self . table_name = "people"
529
+
530
+ before_create do
531
+ self . born_at = created_at
532
+ end
533
+ end
534
+
535
+ person = klass . create first_name : "David"
536
+ assert_not_nil person . born_at
537
+ end
538
+
539
+ def test_timestamp_column_values_are_present_in_update_callbacks
540
+ klass = Class . new ( ActiveRecord ::Base ) do
541
+ self . table_name = "people"
542
+
543
+ before_update do
544
+ self . born_at = created_at
545
+ end
546
+ end
547
+
548
+ person = klass . create first_name : "David"
549
+ person . update first_name : "John"
550
+ assert_not_nil person . born_at
551
+ end
552
+
553
+ def test_timestamp_column_values_are_present_in_save_callbacks
473
554
klass = Class . new ( ActiveRecord ::Base ) do
474
555
self . table_name = "people"
475
556
@@ -479,7 +560,7 @@ def test_timestamp_column_values_are_present_in_the_callbacks
479
560
end
480
561
481
562
person = klass . create first_name : "David"
482
- assert_not_equal person . born_at , nil
563
+ assert_not_nil person . born_at
483
564
end
484
565
485
566
def test_timestamp_attributes_for_create_in_model
0 commit comments