From 3a875e618487a06a56f6cf912cf5440f294921cc Mon Sep 17 00:00:00 2001 From: Nathaniel Bibler Date: Sun, 24 Jan 2010 13:06:29 -0500 Subject: [PATCH] Changed behavior of touch and added touch! Originally implemented by Obie Fernandez, updated touch! to act as a thin wrapper to touch. [#2520 state:resolved] Signed-off-by: wycats --- activerecord/lib/active_record/timestamp.rb | 26 ++++++++++++++------- activerecord/test/cases/timestamp_test.rb | 25 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index da075dabd39e2..cd13ea0d09bc1 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -18,9 +18,8 @@ module Timestamp self.record_timestamps = true end - # Saves the record with the updated_at/on attributes set to the current time. - # If the save fails because of validation errors, an ActiveRecord::RecordInvalid exception is raised. - # If an attribute name is passed, that attribute is used for the touch instead of the updated_at/on attributes. + # Updates only the record's updated_at/on attributes to the current time without checking validations. + # If an attribute name is passed, that attribute is used instead of the updated_at/on attributes. # # Examples: # @@ -30,13 +29,24 @@ def touch(attribute = nil) current_time = current_time_from_proper_timezone if attribute - write_attribute(attribute, current_time) + update_attribute(attribute, current_time) else - write_attribute('updated_at', current_time) if respond_to?(:updated_at) - write_attribute('updated_on', current_time) if respond_to?(:updated_on) + update_attribute('updated_at', current_time) if respond_to?(:updated_at) + update_attribute('updated_on', current_time) if respond_to?(:updated_on) end - - save! + end + + # Saves the entire record with the updated_at/on attributes set to the current time. + # If the save fails because of validation errors, an ActiveRecord::RecordInvalid exception is raised. + # If an attribute name is passed, that attribute is used for the touch instead of the updated_at/on attributes. + # + # Examples: + # + # product.touch! # updates updated_at + # product.touch!(:designed_at) # updates the designed_at attribute + def touch!(attribute = nil) + raise ActiveRecord::RecordInvalid.new(self) unless valid? + touch(attribute) end diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index 24b237a72b36e..6c3596347d8a2 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -30,6 +30,24 @@ def test_touching_a_record_updates_its_timestamp assert @previously_updated_at != @developer.updated_at end + def test_touching_a_record_updates_its_timestamp_even_if_object_instance_is_invalid + @developer.name = nil + @developer.touch + + assert @previously_updated_at != @developer.updated_at + end + + def test_touch_bang_a_record_updates_its_timestamp + @developer.touch! + + assert @previously_updated_at != @developer.updated_at + end + + def test_touch_banging_a_record_fails_if_object_instance_is_invalid + @developer.name = nil + assert_raise(ActiveRecord::RecordInvalid) { @developer.touch! } + end + def test_touching_a_different_attribute previously_created_at = @developer.created_at @developer.touch(:created_at) @@ -37,6 +55,13 @@ def test_touching_a_different_attribute assert previously_created_at != @developer.created_at end + def test_touch_banging_a_different_attribute + previously_created_at = @developer.created_at + @developer.touch!(:created_at) + + assert previously_created_at != @developer.created_at + end + def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at pet = Pet.first owner = pet.owner