module DatastaxRails::Timestamps

DatastaxRails Timestamps

DatastaxRails automatically timestamps create and update operations if the table has fields named created_at or updated_at.

Timestamping can be turned off by setting:

DatastaxRails::Base.record_timestamps = false

Private Instance Methods

_create_record(*args) click to toggle source
Calls superclass method
# File lib/datastax_rails/timestamps.rb, line 25
def _create_record(*args)
  if record_timestamps
    current_time = current_time_from_proper_timezone

    all_timestamp_attributes.each do |column|
      if respond_to?(column) && respond_to?("#{column}=") && send(column).nil?
        write_attribute(column.to_s, current_time)
      end
    end
  end

  super
end
_update_record(*args) click to toggle source
Calls superclass method
# File lib/datastax_rails/timestamps.rb, line 39
def _update_record(*args)
  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
  super
end
all_timestamp_attributes() click to toggle source
# File lib/datastax_rails/timestamps.rb, line 76
def all_timestamp_attributes
  timestamp_attributes_for_create + timestamp_attributes_for_update
end
all_timestamp_attributes_in_model() click to toggle source
# File lib/datastax_rails/timestamps.rb, line 64
def all_timestamp_attributes_in_model
  timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model
end
clear_timestamp_attributes() click to toggle source

Clear attributes and changed_attributes

# File lib/datastax_rails/timestamps.rb, line 91
def clear_timestamp_attributes
  all_timestamp_attributes_in_model.each do |attribute_name|
    self[attribute_name] = nil
    changed_attributes.delete(attribute_name)
  end
end
current_time_from_proper_timezone() click to toggle source
# File lib/datastax_rails/timestamps.rb, line 86
def current_time_from_proper_timezone
  self.class.default_timezone == :utc ? Time.now.utc : Time.now
end
max_updated_column_timestamp() click to toggle source
# File lib/datastax_rails/timestamps.rb, line 80
def max_updated_column_timestamp
  if (timestamps = timestamp_attributes_for_update.map { |attr| self[attr] }.compact).present?
    timestamps.map(&:to_time).max
  end
end
should_record_timestamps?() click to toggle source
# File lib/datastax_rails/timestamps.rb, line 52
def should_record_timestamps?
  record_timestamps && (changed? || (attributes.keys & self.class.serialized_attributes.keys).present?)
end
timestamp_attributes_for_create() click to toggle source
# File lib/datastax_rails/timestamps.rb, line 72
def timestamp_attributes_for_create
  [:created_at]
end
timestamp_attributes_for_create_in_model() click to toggle source
# File lib/datastax_rails/timestamps.rb, line 56
def timestamp_attributes_for_create_in_model
  timestamp_attributes_for_create.select { |c| self.class.column_names.include?(c.to_s) }
end
timestamp_attributes_for_update() click to toggle source
# File lib/datastax_rails/timestamps.rb, line 68
def timestamp_attributes_for_update
  [:updated_at]
end
timestamp_attributes_for_update_in_model() click to toggle source
# File lib/datastax_rails/timestamps.rb, line 60
def timestamp_attributes_for_update_in_model
  timestamp_attributes_for_update.select { |c| self.class.column_names.include?(c.to_s) }
end