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.
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.
@example Revise the document.
person.revise
@since 1.0.0
# File lib/mongoid/versioning.rb, line 35 def revise previous = previous_revision if previous && versioned_attributes_changed? versions.build(previous.versioned_attributes).attributes.delete("_id") if version_max.present? && versions.length > version_max versions.delete(versions.first) end self.version = (version || 1 ) + 1 end end
Forces the creation of a new version of the Document, regardless of whether a change was actually made.
@example Revise the document.
person.revise!
@since 2.2.1
# File lib/mongoid/versioning.rb, line 53 def revise! new_version = versions.build((previous_revision || self).versioned_attributes) versions.shift if version_max.present? && versions.length > version_max self.version = (version || 1 ) + 1 save end
Filters the results of attributes by removing any fields that should not be versioned.
@return [ Hash ] A hash of versioned attributes.
@since 2.1.0
# File lib/mongoid/versioning.rb, line 76 def versioned_attributes only_versioned_attributes(attributes) end
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.
@return [ Boolean ] Whether fields that will be versioned have changed.
@since 2.1.0
# File lib/mongoid/versioning.rb, line 87 def versioned_attributes_changed? !versioned_changes.empty? end
Filters the results of changes by removing any fields that should not be versioned.
@return [ Hash ] A hash of versioned changed attributes.
@since 2.1.0
# File lib/mongoid/versioning.rb, line 66 def versioned_changes only_versioned_attributes(changes) end
Executes a block that temporarily disables versioning. This is for cases where you do not want to version on every save.
@example Execute a save without versioning.
person.versionless(&:save)
@return [ Object ] The document or result of the block execution.
@since 2.0.0
# File lib/mongoid/versioning.rb, line 100 def versionless @versionless = true result = yield(self) if block_given? @versionless = false result || self end
Generated with the Darkfish Rdoc Generator 2.