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.
Instance Method Summary (collapse)
-
- (Object) revise
Create a new version of the Document.
-
- (Object) revise!
Forces the creation of a new version of the Document, regardless of whether a change was actually made.
-
- (Hash) versioned_attributes
Filters the results of attributes by removing any fields that should not be versioned.
-
- (Boolean) versioned_attributes_changed?
Check if any versioned fields have been modified.
-
- (Hash) versioned_changes
Filters the results of changes by removing any fields that should not be versioned.
-
- (Object) versionless
Executes a block that temporarily disables versioning.
Instance Method Details
- (Object) revise
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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mongoid/versioning.rb', line 36 def revise previous = previous_revision if previous && versioned_attributes_changed? new_version = versions.build( previous.versioned_attributes, without_protection: true ) new_version._id = nil if version_max.present? && versions.length > version_max deleted = versions.first if deleted.paranoid? versions.delete_one(deleted) collection.find(atomic_selector). update({ "$pull" => { "versions" => { "version" => deleted.version }}}) else versions.delete(deleted) end end self.version = (version || 1 ) + 1 end end |
- (Object) revise!
Forces the creation of a new version of the Document, regardless of whether a change was actually made.
64 65 66 67 68 69 70 71 |
# File 'lib/mongoid/versioning.rb', line 64 def revise! versions.build( (previous_revision || self).versioned_attributes, without_protection: true ) versions.shift if version_max.present? && versions.length > version_max self.version = (version || 1 ) + 1 save end |
- (Hash) versioned_attributes
Filters the results of attributes by removing any fields that should not be versioned.
89 90 91 |
# File 'lib/mongoid/versioning.rb', line 89 def versioned_attributes only_versioned_attributes(attributes) end |
- (Boolean) versioned_attributes_changed?
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.
100 101 102 |
# File 'lib/mongoid/versioning.rb', line 100 def versioned_attributes_changed? !versioned_changes.empty? end |
- (Hash) versioned_changes
Filters the results of changes by removing any fields that should not be versioned.
79 80 81 |
# File 'lib/mongoid/versioning.rb', line 79 def versioned_changes only_versioned_attributes(changes.except("updated_at")) end |
- (Object) versionless
Executes a block that temporarily disables versioning. This is for cases where you do not want to version on every save.
113 114 115 116 117 118 |
# File 'lib/mongoid/versioning.rb', line 113 def versionless @versionless = true result = yield(self) if block_given? @versionless = false result || self end |