aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorNathaniel Bibler <git@nathanielbibler.com>2010-01-24 13:06:29 -0500
committerwycats <wycats@gmail.com>2010-03-27 00:35:39 -0700
commit3a875e618487a06a56f6cf912cf5440f294921cc (patch)
treefe26b4bcf5c6f6f9a8425da4cc0aba2faad434ad /activerecord/lib/active_record
parent167017f65558a4461aaf8dc26a6f329a283366c2 (diff)
downloadrails-3a875e618487a06a56f6cf912cf5440f294921cc.tar.gz
rails-3a875e618487a06a56f6cf912cf5440f294921cc.tar.bz2
rails-3a875e618487a06a56f6cf912cf5440f294921cc.zip
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 <wycats@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/timestamp.rb26
1 files changed, 18 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb
index da075dabd3..cd13ea0d09 100644
--- a/activerecord/lib/active_record/timestamp.rb
+++ b/activerecord/lib/active_record/timestamp.rb
@@ -18,9 +18,8 @@ module ActiveRecord
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 @@ module ActiveRecord
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