Module: Versioned::Document
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/versioned/versioned.rb
Instance Method Summary collapse
- #destroy_versions ⇒ Object
- #generate_version_id ⇒ Object
- #keep_versions_created_before ⇒ Object
- #prune_versions ⇒ Object
- #push_version ⇒ Object
- #rollback ⇒ Object
- #rolling_back? ⇒ Boolean
- #save(options = {}) ⇒ Object
- #version_doc ⇒ Object
Instance Method Details
#destroy_versions ⇒ Object
73 74 75 |
# File 'lib/versioned/versioned.rb', line 73 def destroy_versions self.versions.destroy_all end |
#generate_version_id ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/versioned/versioned.rb', line 48 def generate_version_id version_id = BSON::ObjectId.new # don't use #save; that'll generate a new version self.write_attribute(:version_id, version_id) self.collection.update({'_id' => self.id}, { 'version_id' => version_id }) self.changed_attributes.clear end |
#keep_versions_created_before ⇒ Object
67 68 69 70 71 |
# File 'lib/versioned/versioned.rb', line 67 def keep_versions_created_before if self.respond_to?(:keep_versions_for) Time.now - self.keep_versions_for end end |
#prune_versions ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/versioned/versioned.rb', line 56 def prune_versions if self.keep_versions_created_before self.versions.destroy_all(:created_at.lt => self.keep_versions_created_before) elsif self.class.max_versions limit = self.versions.count - self.class.max_versions if limit > 0 self.versions.destroy_all(sort: 'id2 asc', limit: limit) end end end |
#push_version ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/versioned/versioned.rb', line 39 def push_version unless self.changes.empty? version = self.versions.create(_id: self.version_id, doc: version_doc, updater: self.updater) self.generate_version_id end ensure self.updater = nil end |
#rollback ⇒ Object
88 89 90 91 92 93 |
# File 'lib/versioned/versioned.rb', line 88 def rollback self.rolling_back = true yield ensure self.rolling_back = false end |
#rolling_back? ⇒ Boolean
30 31 32 |
# File 'lib/versioned/versioned.rb', line 30 def rolling_back? !!@rolling_back end |
#save(options = {}) ⇒ Object
34 35 36 37 |
# File 'lib/versioned/versioned.rb', line 34 def save(={}) self.updater = .delete(:updater) super end |
#version_doc ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/versioned/versioned.rb', line 77 def version_doc {}.tap do |doc| doc.merge!(self.attributes) self.changes.each_pair do |attr, vals| doc[attr] = vals.first end doc.delete('_id') doc.delete('version_id') end end |