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
|
|
|
#
|
2013-06-14 02:39:03 -04:00
|
|
|
# Timestamps are in UTC by default but you can use the local timezone by setting:
|
2010-06-16 13:52:35 -04:00
|
|
|
#
|
2013-06-14 02:39:03 -04:00
|
|
|
# config.active_record.default_timezone = :local
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
|
|
|
# == Time Zone aware attributes
|
|
|
|
#
|
2015-09-09 00:43:38 -04:00
|
|
|
# Active Record keeps all the <tt>datetime</tt> and <tt>time</tt> columns
|
|
|
|
# time-zone aware. By default, these values are stored in the database as UTC
|
|
|
|
# and converted back to the current Time.zone when pulled from the database.
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
2015-09-09 00:43:38 -04:00
|
|
|
# This feature can be turned off completely by setting:
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
2015-09-09 00:43:38 -04:00
|
|
|
# config.active_record.time_zone_aware_attributes = false
|
2010-07-29 10:37:19 -04:00
|
|
|
#
|
2015-09-09 00:43:38 -04:00
|
|
|
# You can also specify that only <tt>datetime</tt> columns should be time-zone
|
|
|
|
# aware (while <tt>time</tt> should not) by setting:
|
|
|
|
#
|
|
|
|
# ActiveRecord::Base.time_zone_aware_types = [:datetime]
|
|
|
|
#
|
|
|
|
# Finally, you can indicate specific attributes of a model for which time zone
|
|
|
|
# conversion should not applied, for instance by setting:
|
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
|
2012-10-26 10:51:02 -04:00
|
|
|
class_attribute :record_timestamps
|
|
|
|
self.record_timestamps = true
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2009-04-16 17:48:07 -04:00
|
|
|
|
2012-09-19 01:47:38 -04:00
|
|
|
def initialize_dup(other) # :nodoc:
|
2012-08-16 17:19:47 -04:00
|
|
|
super
|
2014-01-22 09:28:19 -05:00
|
|
|
clear_timestamp_attributes
|
2011-08-29 12:39:09 -04:00
|
|
|
end
|
|
|
|
|
2010-05-08 19:06:05 -04:00
|
|
|
private
|
2010-07-13 02:05:09 -04:00
|
|
|
|
2013-07-28 23:52:14 -04:00
|
|
|
def _create_record
|
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|
|
2014-08-14 14:43:57 -04:00
|
|
|
column = column.to_s
|
|
|
|
if has_attribute?(column) && !attribute_present?(column)
|
|
|
|
write_attribute(column, current_time)
|
2011-09-10 16:10:01 -04:00
|
|
|
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
|
|
|
|
2014-12-27 18:17:57 -05:00
|
|
|
def _update_record(*args, touch: true, **options)
|
|
|
|
if touch && should_record_timestamps?
|
2010-08-12 11:04:16 -04:00
|
|
|
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
|
2014-12-28 02:05:53 -05:00
|
|
|
super(*args)
|
2010-06-30 07:14:09 -04:00
|
|
|
end
|
|
|
|
|
2010-08-12 11:04:16 -04:00
|
|
|
def should_record_timestamps?
|
2014-01-22 12:55:46 -05:00
|
|
|
self.record_timestamps && (!partial_writes? || changed?)
|
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
|
|
|
|
|
2012-09-19 01:47:38 -04:00
|
|
|
def timestamp_attributes_for_update
|
2010-07-20 23:52:24 -04:00
|
|
|
[:updated_at, :updated_on]
|
|
|
|
end
|
|
|
|
|
2012-09-19 01:47:38 -04:00
|
|
|
def timestamp_attributes_for_create
|
2010-07-20 23:52:24 -04:00
|
|
|
[:created_at, :created_on]
|
|
|
|
end
|
|
|
|
|
2012-09-19 01:47:38 -04:00
|
|
|
def all_timestamp_attributes
|
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
|
|
|
|
2013-11-04 10:14:06 -05:00
|
|
|
def max_updated_column_timestamp(timestamp_names = timestamp_attributes_for_update)
|
2014-06-08 20:00:21 -04:00
|
|
|
timestamp_names
|
|
|
|
.map { |attr| self[attr] }
|
|
|
|
.compact
|
|
|
|
.map(&:to_time)
|
|
|
|
.max
|
2013-01-23 04:47:23 -05:00
|
|
|
end
|
|
|
|
|
2012-09-19 01:47:38 -04:00
|
|
|
def current_time_from_proper_timezone
|
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
|
2014-07-12 20:30:49 -04:00
|
|
|
clear_attribute_changes([attribute_name])
|
2011-08-29 12:39:09 -04:00
|
|
|
end
|
|
|
|
end
|
2006-02-09 14:47:13 -05:00
|
|
|
end
|
2010-07-02 14:36:13 -04:00
|
|
|
end
|