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.



167
168
169
# File 'lib/paper_trail/has_paper_trail.rb', line 167

def live?
  @is_live ||= source_version.nil?
end

#next_versionObject

Returns the object (not a Version) as it became next. 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?


199
200
201
202
203
204
# File 'lib/paper_trail/has_paper_trail.rb', line 199

def next_version
  subsequent_version = source_version.next
  subsequent_version ? subsequent_version.reify : self.class.find(self.id)
rescue
  nil
end

#originatorObject

Returns who put the object into its current state.



172
173
174
# File 'lib/paper_trail/has_paper_trail.rb', line 172

def originator
  @originator ||= self.class.paper_trail_version_class.with_item_keys(self.class.base_class.name, id).last.try :whodunnit
end

#paper_trail_enabled_for_model?Boolean



206
207
208
# File 'lib/paper_trail/has_paper_trail.rb', line 206

def paper_trail_enabled_for_model?
  self.class.paper_trail_enabled_for_model?
end

#previous_versionObject

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



191
192
193
194
# File 'lib/paper_trail/has_paper_trail.rb', line 191

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

#touch_with_version(name = nil) ⇒ Object

Mimicks behavior of `touch` method from `ActiveRecord::Persistence`, but generates a version

TODO: lookinto leveraging the `after_touch` callback from `ActiveRecord` to allow the

regular `touch` method go generate a version as normal. May make sense to switch the `record_update`
method to leverage an `after_update` callback anyways (likely for v3.1.0)

Raises:

  • (ActiveRecordError)


234
235
236
237
238
239
240
241
242
243
# File 'lib/paper_trail/has_paper_trail.rb', line 234

def touch_with_version(name = nil)
  raise ActiveRecordError, "can not touch on a new record object" unless persisted?

  attributes = timestamp_attributes_for_update_in_model
  attributes << name if name
  current_time = current_time_from_proper_timezone

  attributes.each { |column| write_attribute(column, current_time) }
  save!
end

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

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



177
178
179
180
181
182
# File 'lib/paper_trail/has_paper_trail.rb', line 177

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.
  v = send(self.class.versions_association_name).subsequent(timestamp).first
  v ? v.reify(reify_options) : self
end

#versions_between(start_time, end_time, reify_options = {}) ⇒ Object

Returns the objects (not Versions) as they were between the given times.



185
186
187
188
# File 'lib/paper_trail/has_paper_trail.rb', line 185

def versions_between(start_time, end_time, reify_options={})
  versions = send(self.class.versions_association_name).between(start_time, end_time)
  versions.collect { |version| version_at(version.send PaperTrail.timestamp_field) }
end

#whodunnit(value) ⇒ Object

Temporarily overwrites the value of whodunnit and then executes the provided block.



220
221
222
223
224
225
226
227
# File 'lib/paper_trail/has_paper_trail.rb', line 220

def whodunnit(value)
  raise ArgumentError, 'expected to receive a block' unless block_given?
  current_whodunnit = PaperTrail.whodunnit
  PaperTrail.whodunnit = value
  yield self
ensure
  PaperTrail.whodunnit = current_whodunnit
end

#without_versioning(method = nil) ⇒ Object

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



211
212
213
214
215
216
217
# File 'lib/paper_trail/has_paper_trail.rb', line 211

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(self)
ensure
  self.class.paper_trail_on! if paper_trail_was_enabled
end