2004-12-15 22:01:11 -05:00
|
|
|
module ActiveRecord
|
2007-11-07 22:37:16 -05:00
|
|
|
# Active Record automatically timestamps create and update operations if the table has fields
|
|
|
|
# named created_at/created_on or updated_at/updated_on.
|
2004-12-16 12:14:41 -05:00
|
|
|
#
|
2006-06-28 22:39:32 -04:00
|
|
|
# Timestamping can be turned off by setting
|
|
|
|
# <tt>ActiveRecord::Base.record_timestamps = false</tt>
|
|
|
|
#
|
2008-03-26 08:27:52 -04:00
|
|
|
# Timestamps are in the local timezone by default but you can use UTC by setting
|
2006-06-28 22:39:32 -04:00
|
|
|
# <tt>ActiveRecord::Base.default_timezone = :utc</tt>
|
2006-02-09 14:47:13 -05:00
|
|
|
module Timestamp
|
2006-06-28 22:39:32 -04:00
|
|
|
def self.included(base) #:nodoc:
|
|
|
|
base.alias_method_chain :create, :timestamps
|
|
|
|
base.alias_method_chain :update, :timestamps
|
|
|
|
|
2007-11-26 17:45:03 -05:00
|
|
|
base.class_inheritable_accessor :record_timestamps, :instance_writer => false
|
2006-06-28 22:39:32 -04:00
|
|
|
base.record_timestamps = true
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
|
|
|
# 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.
|
2009-04-16 18:25:55 -04:00
|
|
|
# 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)
|
2009-04-16 17:48:07 -04:00
|
|
|
current_time = current_time_from_proper_timezone
|
|
|
|
|
2009-04-16 18:25:55 -04:00
|
|
|
if attribute
|
|
|
|
write_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)
|
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2006-02-09 14:47:13 -05:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
private
|
|
|
|
def create_with_timestamps #:nodoc:
|
|
|
|
if record_timestamps
|
2009-04-16 17:48:07 -04:00
|
|
|
current_time = current_time_from_proper_timezone
|
|
|
|
|
|
|
|
write_attribute('created_at', current_time) if respond_to?(:created_at) && created_at.nil?
|
|
|
|
write_attribute('created_on', current_time) if respond_to?(:created_on) && created_on.nil?
|
2006-02-09 14:47:13 -05:00
|
|
|
|
2009-04-16 17:48:07 -04:00
|
|
|
write_attribute('updated_at', current_time) if respond_to?(:updated_at) && updated_at.nil?
|
|
|
|
write_attribute('updated_on', current_time) if respond_to?(:updated_on) && updated_on.nil?
|
2007-03-12 22:14:31 -04:00
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
create_without_timestamps
|
2005-05-19 15:05:12 -04:00
|
|
|
end
|
2004-12-15 22:01:11 -05:00
|
|
|
|
2008-03-30 21:10:04 -04:00
|
|
|
def update_with_timestamps(*args) #:nodoc:
|
2008-03-30 21:49:57 -04:00
|
|
|
if record_timestamps && (!partial_updates? || changed?)
|
2009-04-16 17:48:07 -04:00
|
|
|
current_time = current_time_from_proper_timezone
|
|
|
|
|
|
|
|
write_attribute('updated_at', current_time) if respond_to?(:updated_at)
|
|
|
|
write_attribute('updated_on', current_time) if respond_to?(:updated_on)
|
2007-03-12 22:14:31 -04:00
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
2008-03-30 21:10:04 -04:00
|
|
|
update_without_timestamps(*args)
|
2005-05-19 15:05:12 -04:00
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
|
|
|
def current_time_from_proper_timezone
|
|
|
|
self.class.default_timezone == :utc ? Time.now.utc : Time.now
|
|
|
|
end
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
end
|