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



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

def revise
  previous = previous_revision
  if previous && versioned_attributes_changed?
    versions.build(previous.versioned_attributes)
    if version_max.present? && versions.length > version_max
      versions.delete(versions.first)
    end
    self.version = (version || 1 ) + 1
  end
end

#revise!Object

Forces the creation of a new version of the Document, regardless of whether a change was actually made.

Examples:

Revise the document.

person.revise!

Since:

  • 2.2.1



65
66
67
68
69
70
71
72
# File 'lib/mongoid/versioning.rb', line 65

def revise!
  new_version = versions.build((previous_revision || self).versioned_attributes)
  if version_max.present? && versions.length > version_max
    versions.delete(versions.first)
  end
  self.version = (version || 1 ) + 1
  save
end

#version_maxInteger

Note:

Refactored from using delegate for class load performance.

Get the maximum number of versions to store.

Examples:

Get the max versions.

model.version_max

Returns:

  • (Integer)

    The max number of versions.



34
35
36
# File 'lib/mongoid/versioning.rb', line 34

def version_max
  self.class.version_max
end

#versioned_attributesHash

Filters the results of attributes by removing any fields that should not be versioned.

Returns:

  • (Hash)

    A hash of versioned attributes.

Since:

  • 2.1.0



90
91
92
# File 'lib/mongoid/versioning.rb', line 90

def versioned_attributes
  only_versioned_attributes(attributes)
end

#versioned_attributes_changed?Boolean

Check if any versioned fields have been modified. This is similar to changed?, except this method also ignores fields set to be ignored by versioning.

Returns:

  • (Boolean)

    Whether fields that will be versioned have changed.

Since:

  • 2.1.0



101
102
103
# File 'lib/mongoid/versioning.rb', line 101

def versioned_attributes_changed?
  !versioned_changes.empty?
end

#versioned_changesHash

Filters the results of changes by removing any fields that should not be versioned.

Returns:

  • (Hash)

    A hash of versioned changed attributes.

Since:

  • 2.1.0



80
81
82
# File 'lib/mongoid/versioning.rb', line 80

def versioned_changes
  only_versioned_attributes(changes)
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



114
115
116
117
118
119
# File 'lib/mongoid/versioning.rb', line 114

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