2004-12-15 22:01:11 -05:00
|
|
|
module ActiveRecord
|
|
|
|
# Active Records will automatically record creation and/or update timestamps of database objects
|
2004-12-16 10:21:16 -05:00
|
|
|
# if fields of the names created_at/created_on or updated_at/updated_on are present. This module is
|
|
|
|
# automatically included, so you don't need to do that manually.
|
2004-12-16 12:14:41 -05:00
|
|
|
#
|
|
|
|
# This behavior can be turned off by setting <tt>ActiveRecord::Base.record_timestamps = false</tt>.
|
2005-01-24 08:18:29 -05:00
|
|
|
# This behavior can use GMT by setting <tt>ActiveRecord::Base.timestamps_gmt = true</tt>
|
2004-12-15 22:01:11 -05:00
|
|
|
module Timestamp
|
|
|
|
def self.append_features(base) # :nodoc:
|
|
|
|
super
|
2004-12-18 10:15:27 -05:00
|
|
|
|
|
|
|
base.class_eval do
|
|
|
|
alias_method :create_without_timestamps, :create
|
|
|
|
alias_method :create, :create_with_timestamps
|
|
|
|
|
|
|
|
alias_method :update_without_timestamps, :update
|
|
|
|
alias_method :update, :update_with_timestamps
|
|
|
|
end
|
2004-12-15 22:01:11 -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
|
2005-02-24 07:00:42 -05: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?
|
2004-12-18 10:15:27 -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
|
2005-02-24 07:00:42 -05: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
|
|
|
|
end
|
2004-12-16 10:21:16 -05:00
|
|
|
|
|
|
|
class Base
|
|
|
|
# Records the creation date and possibly time in created_on (date only) or created_at (date and time) and the update date and possibly
|
|
|
|
# time in updated_on and updated_at. This only happens if the object responds to either of these messages, which they will do automatically
|
|
|
|
# if the table has columns of either of these names. This feature is turned on by default.
|
|
|
|
@@record_timestamps = true
|
|
|
|
cattr_accessor :record_timestamps
|
2005-02-24 07:00:42 -05:00
|
|
|
|
|
|
|
# deprecated: use ActiveRecord::Base.default_timezone instead.
|
2005-01-24 08:18:29 -05:00
|
|
|
@@timestamps_gmt = false
|
2005-02-24 07:00:42 -05:00
|
|
|
def self.timestamps_gmt=( gmt ) #:nodoc:
|
|
|
|
warn "timestamps_gmt= is deprecated. use default_timezone= instead"
|
|
|
|
self.default_timezone = ( gmt ? :utc : :local )
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.timestamps_gmt #:nodoc:
|
|
|
|
warn "timestamps_gmt is deprecated. use default_timezone instead"
|
|
|
|
self.default_timezone == :utc
|
|
|
|
end
|
2004-12-16 10:21:16 -05:00
|
|
|
end
|
2005-01-24 08:18:29 -05:00
|
|
|
end
|