2004-12-15 22:01:11 -05:00
|
|
|
module ActiveRecord
|
2006-06-28 22:39:32 -04:00
|
|
|
# Active Record automatically timestamps create and update if the table has fields
|
|
|
|
# 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>
|
|
|
|
#
|
2006-08-07 02:54:22 -04:00
|
|
|
# Keep in mind that, via inheritance, you can turn off timestamps on a per
|
|
|
|
# model basis by setting <tt>record_timestamps</tt> to false in the desired
|
|
|
|
# models.
|
|
|
|
#
|
|
|
|
# class Feed < ActiveRecord::Base
|
|
|
|
# self.record_timestamps = false
|
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
#
|
2006-06-28 22:39:32 -04:00
|
|
|
# Timestamps are in the local timezone by default but can use UTC by setting
|
|
|
|
# <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:
|
|
|
|
super
|
|
|
|
|
|
|
|
base.alias_method_chain :create, :timestamps
|
|
|
|
base.alias_method_chain :update, :timestamps
|
|
|
|
|
2007-01-27 20:31:31 -05:00
|
|
|
base.cattr_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
|
|
|
|
|
2005-02-23 18:51:34 -05:00
|
|
|
def create_with_timestamps #:nodoc:
|
2005-05-19 15:05:12 -04:00
|
|
|
if record_timestamps
|
2006-06-28 22:39:32 -04:00
|
|
|
t = self.class.default_timezone == :utc ? Time.now.utc : Time.now
|
2005-05-19 15:05:12 -04:00
|
|
|
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
|
|
|
|
2005-05-19 15:05:12 -04:00
|
|
|
write_attribute('updated_at', t) if respond_to?(:updated_at)
|
|
|
|
write_attribute('updated_on', t) if respond_to?(:updated_on)
|
|
|
|
end
|
2004-12-18 10:15:27 -05:00
|
|
|
create_without_timestamps
|
2004-12-15 22:01:11 -05:00
|
|
|
end
|
|
|
|
|
2005-02-23 18:51:34 -05:00
|
|
|
def update_with_timestamps #:nodoc:
|
2005-05-19 15:05:12 -04:00
|
|
|
if record_timestamps
|
2006-06-28 22:39:32 -04:00
|
|
|
t = self.class.default_timezone == :utc ? Time.now.utc : Time.now
|
2005-05-19 15:05:12 -04:00
|
|
|
write_attribute('updated_at', t) if respond_to?(:updated_at)
|
|
|
|
write_attribute('updated_on', t) if respond_to?(:updated_on)
|
|
|
|
end
|
2004-12-18 10:15:27 -05:00
|
|
|
update_without_timestamps
|
2004-12-15 22:01:11 -05:00
|
|
|
end
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2005-01-24 08:18:29 -05:00
|
|
|
end
|