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
|
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
private
|
|
|
|
def create_with_timestamps #:nodoc:
|
|
|
|
if record_timestamps
|
|
|
|
t = self.class.default_timezone == :utc ? Time.now.utc : Time.now
|
|
|
|
write_attribute('created_at', t) if respond_to?(:created_at) && created_at.nil?
|
|
|
|
write_attribute('created_on', t) if respond_to?(:created_on) && created_on.nil?
|
2006-02-09 14:47:13 -05:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
write_attribute('updated_at', t) if respond_to?(:updated_at)
|
|
|
|
write_attribute('updated_on', t) if respond_to?(:updated_on)
|
|
|
|
end
|
|
|
|
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?)
|
2007-03-12 22:14:31 -04:00
|
|
|
t = self.class.default_timezone == :utc ? Time.now.utc : Time.now
|
|
|
|
write_attribute('updated_at', t) if respond_to?(:updated_at)
|
|
|
|
write_attribute('updated_on', t) if respond_to?(:updated_on)
|
|
|
|
end
|
2008-03-30 21:10:04 -04:00
|
|
|
update_without_timestamps(*args)
|
2005-05-19 15:05:12 -04:00
|
|
|
end
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2005-01-24 08:18:29 -05:00
|
|
|
end
|