2004-12-16 03:01:11 +00:00
|
|
|
module ActiveRecord
|
2007-11-08 03:37:16 +00: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 17:14:41 +00:00
|
|
|
#
|
2006-06-29 02:39:32 +00:00
|
|
|
# Timestamping can be turned off by setting
|
|
|
|
# <tt>ActiveRecord::Base.record_timestamps = false</tt>
|
|
|
|
#
|
2008-03-26 12:27:52 +00:00
|
|
|
# Timestamps are in the local timezone by default but you can use UTC by setting
|
2006-06-29 02:39:32 +00:00
|
|
|
# <tt>ActiveRecord::Base.default_timezone = :utc</tt>
|
2006-02-09 19:47:13 +00:00
|
|
|
module Timestamp
|
2006-06-29 02:39:32 +00:00
|
|
|
def self.included(base) #:nodoc:
|
|
|
|
base.alias_method_chain :create, :timestamps
|
|
|
|
base.alias_method_chain :update, :timestamps
|
|
|
|
|
2007-11-26 22:45:03 +00:00
|
|
|
base.class_inheritable_accessor :record_timestamps, :instance_writer => false
|
2006-06-29 02:39:32 +00:00
|
|
|
base.record_timestamps = true
|
2006-02-09 19:47:13 +00:00
|
|
|
end
|
|
|
|
|
2007-03-13 02:14:31 +00: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 19:47:13 +00:00
|
|
|
|
2007-03-13 02:14:31 +00: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 19:05:12 +00:00
|
|
|
end
|
2004-12-16 03:01:11 +00:00
|
|
|
|
2008-03-31 01:10:04 +00:00
|
|
|
def update_with_timestamps(*args) #:nodoc:
|
2008-03-31 01:49:57 +00:00
|
|
|
if record_timestamps && (!partial_updates? || changed?)
|
2007-03-13 02:14:31 +00: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-31 01:10:04 +00:00
|
|
|
update_without_timestamps(*args)
|
2005-05-19 19:05:12 +00:00
|
|
|
end
|
2006-02-09 19:47:13 +00:00
|
|
|
end
|
2005-01-24 13:18:29 +00:00
|
|
|
end
|