Module: PaperTrail::Model::InstanceMethods

Defined in:
lib/paper_trail/has_paper_trail.rb

Overview

Wrap the following methods in a module so we can include them only in the ActiveRecord models that declare ‘has_paper_trail`.

Instance Method Summary collapse

Instance Method Details

#live?Boolean

Returns true if this instance is the current, live one; returns false if this instance came from a previous version.

Returns:

  • (Boolean)


82
83
84
# File 'lib/paper_trail/has_paper_trail.rb', line 82

def live?
  version.nil?
end

#next_versionObject

Returns the object (not a Version) as it became next.



106
107
108
109
110
111
# File 'lib/paper_trail/has_paper_trail.rb', line 106

def next_version
  # NOTE: if self (the item) was not reified from a version, i.e. it is the
  # "live" item, we return nil.  Perhaps we should return self instead?
  subsequent_version = version ? version.next : nil
  subsequent_version.reify if subsequent_version
end

#originatorObject

Returns who put the object into its current state.



87
88
89
# File 'lib/paper_trail/has_paper_trail.rb', line 87

def originator
  version_class.with_item_keys(self.class.name, id).last.try :whodunnit
end

#previous_versionObject

Returns the object (not a Version) as it was most recently.



100
101
102
103
# File 'lib/paper_trail/has_paper_trail.rb', line 100

def previous_version
  preceding_version = version ? version.previous : send(self.class.versions_association_name).last
  preceding_version.try :reify
end

#version_at(timestamp, reify_options = {}) ⇒ Object

Returns the object (not a Version) as it was at the given timestamp.



92
93
94
95
96
97
# File 'lib/paper_trail/has_paper_trail.rb', line 92

def version_at(timestamp, reify_options={})
  # Because a version stores how its object looked *before* the change,
  # we need to look for the first version created *after* the timestamp.
  version = send(self.class.versions_association_name).after(timestamp).first
  version ? version.reify(reify_options) : self
end

#without_versioning(method = nil) ⇒ Object

Executes the given method or block without creating a new version.



114
115
116
117
118
119
120
# File 'lib/paper_trail/has_paper_trail.rb', line 114

def without_versioning(method = nil)
  paper_trail_was_enabled = self.paper_trail_enabled_for_model
  self.class.paper_trail_off
  method ? method.to_proc.call(self) : yield
ensure
  self.class.paper_trail_on if paper_trail_was_enabled
end