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
-
#revise ⇒ Object
Create a new version of the
Document. -
#versionless ⇒ Object
Executes a block that temporarily disables versioning.
Instance Method Details
#revise ⇒ Object
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.
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 |
#versionless ⇒ Object
Executes a block that temporarily disables versioning. This is for cases where you do not want to version on every save.
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 |