module Snapshotable::Model::InstanceMethods

Public Instance Methods

last_snapshot() click to toggle source
# File lib/snapshotable/model.rb, line 45
def last_snapshot
  snapshots.order(created_at: :desc).first
end
last_snapshot_before(time = Time.zone.now) click to toggle source
# File lib/snapshotable/model.rb, line 41
def last_snapshot_before(time = Time.zone.now)
  snapshots.order(created_at: :desc).where('created_at < ?', time).first
end
should_create_new_snapshot?(snapshot) click to toggle source
# File lib/snapshotable/model.rb, line 53
def should_create_new_snapshot?(snapshot)
  return true if last_snapshot.nil?

  snapshot_to_compare = last_snapshot
                        .attributes
                        .except(*self.class.attributes_to_ignore_on_diff)
                        .with_indifferent_access

  Hashdiff.diff(snapshot_to_compare, snapshot.with_indifferent_access).any?
end
snapshot_class() click to toggle source
# File lib/snapshotable/model.rb, line 49
def snapshot_class
  Object.const_get(snapshot_class_name)
end
snapshots() click to toggle source
# File lib/snapshotable/model.rb, line 37
def snapshots
  send(snapshot_association_name)
end
take_snapshot!(force = false) click to toggle source
# File lib/snapshotable/model.rb, line 32
def take_snapshot!(force = false)
  snapshot = SnapshotCreator.new(self).call
  snapshot_class.create!(snapshot) if force || should_create_new_snapshot?(snapshot)
end