diff options
Diffstat (limited to 'activerecord/lib/active_record/timestamp.rb')
-rw-r--r-- | activerecord/lib/active_record/timestamp.rb | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index cf17b1d8a4..936a18d99a 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -1,4 +1,3 @@ - module ActiveRecord # = Active Record Timestamp # @@ -10,9 +9,9 @@ module ActiveRecord # # config.active_record.record_timestamps = false # - # Timestamps are in the local timezone by default but you can use UTC by setting: + # Timestamps are in UTC by default but you can use the local timezone by setting: # - # config.active_record.default_timezone = :utc + # config.active_record.default_timezone = :local # # == Time Zone aware attributes # @@ -37,19 +36,20 @@ module ActiveRecord end def initialize_dup(other) # :nodoc: - clear_timestamp_attributes super + clear_timestamp_attributes end private - def create + def _create_record if self.record_timestamps current_time = current_time_from_proper_timezone all_timestamp_attributes.each do |column| - if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil? - write_attribute(column.to_s, current_time) + column = column.to_s + if has_attribute?(column) && !attribute_present?(column) + write_attribute(column, current_time) end end end @@ -57,7 +57,7 @@ module ActiveRecord super end - def update(*args) + def _update_record(*args) if should_record_timestamps? current_time = current_time_from_proper_timezone @@ -71,7 +71,7 @@ module ActiveRecord end def should_record_timestamps? - self.record_timestamps && (!partial_writes? || changed? || (attributes.keys & self.class.serialized_attributes.keys).present?) + self.record_timestamps && (!partial_writes? || changed?) end def timestamp_attributes_for_create_in_model @@ -98,6 +98,14 @@ module ActiveRecord timestamp_attributes_for_create + timestamp_attributes_for_update end + def max_updated_column_timestamp(timestamp_names = timestamp_attributes_for_update) + timestamp_names + .map { |attr| self[attr] } + .compact + .map(&:to_time) + .max + end + def current_time_from_proper_timezone self.class.default_timezone == :utc ? Time.now.utc : Time.now end @@ -106,7 +114,7 @@ module ActiveRecord def clear_timestamp_attributes all_timestamp_attributes_in_model.each do |attribute_name| self[attribute_name] = nil - changed_attributes.delete(attribute_name) + clear_attribute_changes([attribute_name]) end end end |