Module: PaperTrail::VersionConcern

Extended by:
ActiveSupport::Concern
Included in:
Version
Defined in:
lib/mongo_trails/version_concern.rb

Overview

Originally, PaperTrail did not provide this module, and all of this functionality was in ‘PaperTrail::Version`. That model still exists (and is used by most apps) but by moving the functionality to this module, people can include this concern instead of sub-classing the `Version` model.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#changesetObject

Returns what changed in this version of the item. ‘ActiveModel::Dirty#changes`. returns `nil` if your `versions` table does not have an `object_changes` text column.



219
220
221
222
# File 'lib/mongo_trails/version_concern.rb', line 219

def changeset
  return nil unless self.class.fields.keys.include? "object_changes"
  @changeset ||= load_changeset
end

#indexObject

Returns an integer representing the chronological position of the version among its siblings (see ‘sibling_versions`). The “create” event, for example, has an index of 0.



255
256
257
# File 'lib/mongo_trails/version_concern.rb', line 255

def index
  @index ||= RecordHistory.new(sibling_versions, self.class).index(self)
end

#nextObject



243
244
245
# File 'lib/mongo_trails/version_concern.rb', line 243

def next
  @next ||= sibling_versions.subsequent(self).first
end

#object_deserializedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



183
184
185
186
187
188
189
# File 'lib/mongo_trails/version_concern.rb', line 183

def object_deserialized
  if self.class.object_col_is_json?
    object
  else
    PaperTrail.serializer.load(object)
  end
end

#paper_trail_originatorObject

Returns who put the item into the state stored in this version.



225
226
227
# File 'lib/mongo_trails/version_concern.rb', line 225

def paper_trail_originator
  @paper_trail_originator ||= previous.try(:whodunnit)
end

#previousObject



247
248
249
# File 'lib/mongo_trails/version_concern.rb', line 247

def previous
  @previous ||= sibling_versions.preceding(self).first
end

#reify(options = {}) ⇒ Object

Restore the item from this version.

Options:

  • :mark_for_destruction

    • ‘true` - Mark the has_one/has_many associations that did not exist in the reified version for destruction, instead of removing them.

    • ‘false` - Default. Useful for persisting the reified version.

  • :dup

    • ‘false` - Default.

    • ‘true` - Always create a new object instance. Useful for comparing two versions of the same object.

  • :unversioned_attributes

    • ‘:nil` - Default. Attributes undefined in version record are set to nil in reified record.

    • ‘:preserve` - Attributes undefined in version record are not modified.



208
209
210
211
212
213
214
# File 'lib/mongo_trails/version_concern.rb', line 208

def reify(options = {})
  unless self.class.fields.keys.include? "object"
    raise "reify can't be called without an object column"
  end
  return nil if object.nil?
  ::PaperTrail::Reifier.reify(self, options)
end

#sibling_versions(reload = false) ⇒ Object



236
237
238
239
240
241
# File 'lib/mongo_trails/version_concern.rb', line 236

def sibling_versions(reload = false)
  if reload || !defined?(@sibling_versions) || @sibling_versions.nil?
    @sibling_versions = self.class.with_item_keys(item_type, item_id)
  end
  @sibling_versions
end

#terminatorObject Also known as: version_author

Returns who changed the item from the state it had in this version. This is an alias for ‘whodunnit`.



231
232
233
# File 'lib/mongo_trails/version_concern.rb', line 231

def terminator
  @terminator ||= whodunnit
end