2010-11-19 12:29:33 -05:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
|
|
|
|
2004-12-15 22:01:11 -05:00
|
|
|
module ActiveRecord
|
2010-06-16 13:52:35 -04:00
|
|
|
# = Active Record Timestamp
|
2010-08-12 11:04:16 -04:00
|
|
|
#
|
2010-06-16 13:52:35 -04:00
|
|
|
# Active Record automatically timestamps create and update operations if the
|
2010-08-12 11:04:16 -04:00
|
|
|
# table has fields named <tt>created_at/created_on</tt> or
|
2010-06-16 13:52:35 -04:00
|
|
|
# <tt>updated_at/updated_on</tt>.
|
|
|
|
#
|
|
|
|
# Timestamping can be turned off by setting:
|
2004-12-16 12:14:41 -05:00
|
|
|
#
|
2011-02-05 12:07:00 -05:00
|
|
|
# config.active_record.record_timestamps = false
|
2006-06-28 22:39:32 -04:00
|
|
|
#
|
2010-06-16 13:52:35 -04:00
|
|
|
# Timestamps are in the local timezone by default but you can use UTC by setting:
|
|
|
|
#
|
2011-02-05 12:07:00 -05:00
|
|
|
# config.active_record.default_timezone = :utc
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
|
|
|
# == Time Zone aware attributes
|
|
|
|
#
|
|
|
|
# By default, ActiveRecord::Base keeps all the datetime columns time zone aware by executing following code.
|
|
|
|
#
|
2011-02-05 12:07:00 -05:00
|
|
|
# config.active_record.time_zone_aware_attributes = true
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
|
|
|
# This feature can easily be turned off by assigning value <tt>false</tt> .
|
|
|
|
#
|
2011-02-05 12:07:00 -05:00
|
|
|
# If your attributes are time zone aware and you desire to skip time zone conversion to the current Time.zone
|
|
|
|
# when reading certain attributes then you can do following:
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
2011-02-05 12:07:00 -05:00
|
|
|
# class Topic < ActiveRecord::Base
|
|
|
|
# self.skip_time_zone_conversion_for_attributes = [:written_on]
|
|
|
|
# end
|
2006-02-09 14:47:13 -05:00
|
|
|
module Timestamp
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2006-06-28 22:39:32 -04:00
|
|
|
|
2009-05-11 22:23:47 -04:00
|
|
|
included do
|
2011-12-28 10:38:16 -05:00
|
|
|
config_attribute :record_timestamps, :instance_writer => true
|
2009-05-11 22:23:47 -04:00
|
|
|
self.record_timestamps = true
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
2011-08-29 12:39:09 -04:00
|
|
|
def initialize_dup(other)
|
|
|
|
clear_timestamp_attributes
|
|
|
|
end
|
|
|
|
|
2010-05-08 19:06:05 -04:00
|
|
|
private
|
2010-07-13 02:05:09 -04:00
|
|
|
|
2010-05-08 19:06:05 -04:00
|
|
|
def create #:nodoc:
|
2010-11-19 12:29:33 -05:00
|
|
|
if self.record_timestamps
|
2010-05-08 19:06:05 -04:00
|
|
|
current_time = current_time_from_proper_timezone
|
2006-02-09 14:47:13 -05:00
|
|
|
|
2010-08-12 11:04:16 -04:00
|
|
|
all_timestamp_attributes.each do |column|
|
2011-09-10 16:10:01 -04:00
|
|
|
if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil?
|
|
|
|
write_attribute(column.to_s, current_time)
|
|
|
|
end
|
2010-07-20 23:52:24 -04:00
|
|
|
end
|
2005-05-19 15:05:12 -04:00
|
|
|
end
|
2004-12-15 22:01:11 -05:00
|
|
|
|
2010-05-08 19:06:05 -04:00
|
|
|
super
|
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
2010-05-08 19:06:05 -04:00
|
|
|
def update(*args) #:nodoc:
|
2010-08-12 11:04:16 -04:00
|
|
|
if should_record_timestamps?
|
|
|
|
current_time = current_time_from_proper_timezone
|
|
|
|
|
|
|
|
timestamp_attributes_for_update_in_model.each do |column|
|
|
|
|
column = column.to_s
|
|
|
|
next if attribute_changed?(column)
|
|
|
|
write_attribute(column, current_time)
|
|
|
|
end
|
|
|
|
end
|
2010-06-30 07:14:09 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-08-12 11:04:16 -04:00
|
|
|
def should_record_timestamps?
|
2010-11-19 12:29:33 -05:00
|
|
|
self.record_timestamps && (!partial_updates? || changed? || (attributes.keys & self.class.serialized_attributes.keys).present?)
|
2010-06-30 07:14:09 -04:00
|
|
|
end
|
2010-05-08 19:06:05 -04:00
|
|
|
|
2011-01-25 17:27:27 -05:00
|
|
|
def timestamp_attributes_for_create_in_model
|
|
|
|
timestamp_attributes_for_create.select { |c| self.class.column_names.include?(c.to_s) }
|
|
|
|
end
|
|
|
|
|
2010-08-12 11:04:16 -04:00
|
|
|
def timestamp_attributes_for_update_in_model
|
2010-12-30 13:41:53 -05:00
|
|
|
timestamp_attributes_for_update.select { |c| self.class.column_names.include?(c.to_s) }
|
2010-07-20 23:52:24 -04:00
|
|
|
end
|
|
|
|
|
2011-01-25 17:33:55 -05:00
|
|
|
def all_timestamp_attributes_in_model
|
|
|
|
timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model
|
|
|
|
end
|
|
|
|
|
2010-07-20 23:52:24 -04:00
|
|
|
def timestamp_attributes_for_update #:nodoc:
|
|
|
|
[:updated_at, :updated_on]
|
|
|
|
end
|
|
|
|
|
|
|
|
def timestamp_attributes_for_create #:nodoc:
|
|
|
|
[:created_at, :created_on]
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_timestamp_attributes #:nodoc:
|
2010-08-12 11:04:16 -04:00
|
|
|
timestamp_attributes_for_create + timestamp_attributes_for_update
|
2010-07-02 14:36:13 -04:00
|
|
|
end
|
2010-08-12 11:04:16 -04:00
|
|
|
|
2010-07-02 14:36:13 -04:00
|
|
|
def current_time_from_proper_timezone #:nodoc:
|
2010-05-08 19:06:05 -04:00
|
|
|
self.class.default_timezone == :utc ? Time.now.utc : Time.now
|
|
|
|
end
|
2011-08-29 12:39:09 -04:00
|
|
|
|
|
|
|
# Clear attributes and changed_attributes
|
|
|
|
def clear_timestamp_attributes
|
|
|
|
all_timestamp_attributes_in_model.each do |attribute_name|
|
|
|
|
self[attribute_name] = nil
|
|
|
|
changed_attributes.delete(attribute_name)
|
|
|
|
end
|
|
|
|
end
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2010-07-02 14:36:13 -04:00
|
|
|
end
|