Module: Mongoid::Versioning
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid/core_ext/versioning.rb,
lib/mongoid/versioning.rb,
lib/mongoid/versioning/version.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
Constant Summary collapse
- VERSION =
'2.0.0'.freeze
Instance Method Summary collapse
-
#revise ⇒ Object
Create a new version of the
Document
. -
#revise! ⇒ Object
Forces the creation of a new version of the
Document
, regardless of whether a change was actually made. -
#versioned_attributes ⇒ Hash
Filters the results of
attributes
by removing any fields that should not be versioned. -
#versioned_attributes_changed? ⇒ Boolean
Check if any versioned fields have been modified.
-
#versioned_changes ⇒ Hash
Filters the results of
changes
by removing any fields that should not be versioned. -
#versionless ⇒ Object
Executes a block that temporarily disables versioning.
Instance Method Details
#revise ⇒ Object
Remove Mongoid 4 support.
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 56 57 58 59 |
# File 'lib/mongoid/core_ext/versioning.rb', line 36 def revise previous = previous_revision if previous && versioned_attributes_changed? new_version = versions.build( previous.versioned_attributes ) new_version._id = nil if version_max.present? && versions.length > version_max to_delete = versions.first version_to_delete = to_delete.version if to_delete.respond_to?(:paranoid?) && to_delete.paranoid? versions.delete_one(to_delete) query = collection.find(atomic_selector) query.respond_to?(:update_one) ? query.update_one('$pull' => { 'versions' => { 'version' => version_to_delete } }) : query.update('$pull' => { 'versions' => { 'version' => version_to_delete } }) else versions.where(version: version_to_delete).delete_all end 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.
68 69 70 71 72 73 74 75 |
# File 'lib/mongoid/core_ext/versioning.rb', line 68 def revise! versions.build( (previous_revision || self).versioned_attributes ) versions.shift if version_max.present? && versions.length > version_max self.version = (version || 1) + 1 save end |
#versioned_attributes ⇒ Hash
Filters the results of attributes
by removing any fields that should not be versioned.
93 94 95 |
# File 'lib/mongoid/core_ext/versioning.rb', line 93 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.
104 105 106 |
# File 'lib/mongoid/core_ext/versioning.rb', line 104 def versioned_attributes_changed? !versioned_changes.empty? end |
#versioned_changes ⇒ Hash
Filters the results of changes
by removing any fields that should not be versioned.
83 84 85 |
# File 'lib/mongoid/core_ext/versioning.rb', line 83 def versioned_changes only_versioned_attributes(changes.except('updated_at')) end |
#versionless ⇒ Object
Executes a block that temporarily disables versioning. This is for cases where you do not want to version on every save.
117 118 119 120 121 122 |
# File 'lib/mongoid/core_ext/versioning.rb', line 117 def versionless @versionless = true result = yield(self) if block_given? @versionless = false result || self end |