Module: Mongoid::Versioning

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid/versioning.rb

Overview

Include this module to get automatic versioning of root level documents. This will add a version field to the Document and a has_many association with all the versions contained in it.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#reviseObject

Create a new version of the Document. This will load the previous document from the database and set it as the next version before saving the current document. It then increments the version number. If a #max_versions limit is set in the model and it’s exceeded, the oldest version gets discarded.

Examples:

Revise the document.

person.revise

Since:

  • 1.0.0



29
30
31
32
33
34
35
36
37
# File 'lib/mongoid/versioning.rb', line 29

def revise
  previous = find_last_version
  if previous
    versions.target << previous.clone
    versions.shift if version_max.present? && versions.length > version_max
    self.version = (version || 1 ) + 1
    @modifications["versions"] = [ nil, versions.as_document ] if @modifications
  end
end

#versionlessObject

Executes a block that temporarily disables versioning. This is for cases where you do not want to version on every save.

Examples:

Execute a save without versioning.

person.versionless(&:save)

Returns:

  • (Object)

    The document or result of the block execution.

Since:

  • 2.0.0



48
49
50
51
52
53
# File 'lib/mongoid/versioning.rb', line 48

def versionless
  @versionless = true
  result = yield(self) if block_given?
  @versionless = false
  result || self
end