Module: Snapshotable::Model::InstanceMethods
- Defined in:
- lib/snapshotable/model.rb
Instance Method Summary collapse
- #last_snapshot ⇒ Object
- #last_snapshot_before(time = Time.now) ⇒ Object
- #should_create_new_snapshot?(snapshot) ⇒ Boolean
- #snapshot_class ⇒ Object
- #snapshots ⇒ Object
- #take_snapshot! ⇒ Object
Instance Method Details
#last_snapshot ⇒ Object
45 46 47 |
# File 'lib/snapshotable/model.rb', line 45 def last_snapshot snapshots.order(created_at: :desc).first end |
#last_snapshot_before(time = Time.now) ⇒ Object
41 42 43 |
# File 'lib/snapshotable/model.rb', line 41 def last_snapshot_before(time = Time.now) snapshots.order(created_at: :desc).where('created_at < ?', time).first end |
#should_create_new_snapshot?(snapshot) ⇒ Boolean
53 54 55 56 57 58 59 60 61 62 |
# 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 ⇒ Object
49 50 51 |
# File 'lib/snapshotable/model.rb', line 49 def snapshot_class Object.const_get(snapshot_class_name) end |
#snapshots ⇒ Object
37 38 39 |
# File 'lib/snapshotable/model.rb', line 37 def snapshots send(snapshot_association_name) end |
#take_snapshot! ⇒ Object
32 33 34 35 |
# File 'lib/snapshotable/model.rb', line 32 def take_snapshot! snapshot = SnapshotCreator.new(self).call snapshot_class.create!(snapshot) if should_create_new_snapshot?(snapshot) end |