Sha256: 60fc43dc92a43f46957c1063a8aa5a448cef07cb1c27a4c8d67a74d241b7bff7

Contents?: true

Size: 786 Bytes

Versions: 1

Compression:

Stored size: 786 Bytes

Contents

# Expects a ::Timestamp ActiveRecord class with timestampable_type, timestampable_id, key:string, and stamped_at:datetime.
module FastTimestamp
  def self.included(base)
    base.class_eval { after_destroy :fast_destroy_timestamps }
  end
  
  def timestamp!(key)
    transaction do
      t = ::Timestamp.find_or_create_by_timestampable_type_and_timestampable_id_and_key(self.class.base_class.name, id, key.to_s)
      t.update_attribute :stamped_at, Time.zone.now
    end
  end

  def timestamped?(key)
    ::Timestamp.find_by_timestampable_type_and_timestampable_id_and_key(self.class.base_class.name, id, key.to_s).present?
  end

  def fast_destroy_timestamps
    ::Timestamp.destroy_all :timestampable_type => self.class.base_class.name, :timestampable_id => id
    true
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fast_timestamp-0.0.1 lib/fast_timestamp.rb