2007-01-22 14:43:50 +01:00
|
|
|
module ActiveRecord
|
2007-12-21 08:48:59 +01:00
|
|
|
# Active Record automatically timestamps create and update operations if the table has fields
|
|
|
|
# named created_at/created_on or updated_at/updated_on.
|
2007-01-22 14:43:50 +01:00
|
|
|
#
|
2007-02-09 09:04:31 +01:00
|
|
|
# Timestamping can be turned off by setting
|
|
|
|
# <tt>ActiveRecord::Base.record_timestamps = false</tt>
|
|
|
|
#
|
2008-05-18 06:22:34 +02:00
|
|
|
# Timestamps are in the local timezone by default but you can use UTC by setting
|
2007-02-09 09:04:31 +01:00
|
|
|
# <tt>ActiveRecord::Base.default_timezone = :utc</tt>
|
2007-01-22 14:43:50 +01:00
|
|
|
module Timestamp
|
2007-02-09 09:04:31 +01:00
|
|
|
def self.included(base) #:nodoc:
|
|
|
|
base.alias_method_chain :create, :timestamps
|
|
|
|
base.alias_method_chain :update, :timestamps
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
base.class_inheritable_accessor :record_timestamps, :instance_writer => false
|
2007-02-09 09:04:31 +01:00
|
|
|
base.record_timestamps = true
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01: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?
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
write_attribute('updated_at', t) if respond_to?(:updated_at) && updated_at.nil?
|
|
|
|
write_attribute('updated_on', t) if respond_to?(:updated_on) && updated_on.nil?
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
create_without_timestamps
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def update_with_timestamps(*args) #:nodoc:
|
|
|
|
if record_timestamps && (!partial_updates? || changed?)
|
2007-12-21 08:48:59 +01: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-05-18 06:22:34 +02:00
|
|
|
update_without_timestamps(*args)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|