Module: Vidibus::Versioning::Mongoid
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/vidibus/versioning/mongoid.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #delete ⇒ Object
- #destroy ⇒ Object
-
#migrate!(number = nil) ⇒ Object
Applies attributes of wanted version on self.
-
#new_version? ⇒ Boolean
Returns true if version requested is a new one.
-
#redo! ⇒ Object
Calls #version!(:next) and migrate!.
-
#reload_version(*args) ⇒ Object
Reloads this record and applies version attributes.
-
#save(*args) ⇒ Object
Saves the record and handles version persistence.
-
#save!(*args) ⇒ Object
Raises a validation error if saving fails.
-
#undo! ⇒ Object
Calls #version!(:previous) and migrate!.
-
#version(*args) ⇒ Object
Returns a copy of this object with versioned attributes applied.
-
#version!(*args) ⇒ Object
Applies versioned attributes on this object.
-
#version_object ⇒ Object
Returns the currently set version object.
Instance Method Details
#delete ⇒ Object
113 114 115 |
# File 'lib/vidibus/versioning/mongoid.rb', line 113 def delete super if remove_version(:delete) == nil end |
#destroy ⇒ Object
117 118 119 |
# File 'lib/vidibus/versioning/mongoid.rb', line 117 def destroy super if remove_version(:destroy) == nil end |
#migrate!(number = nil) ⇒ Object
Applies attributes of wanted version on self. Stores current attributes in a new version.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/vidibus/versioning/mongoid.rb', line 69 def migrate!(number = nil) unless number || version_cache.wanted_version_number raise(MigrationError, 'no version given') end if number && number != version_cache.wanted_version_number version!(number) end if version_cache.self_version raise(MigrationError, 'cannot migrate to current version') end set_original_version_obj self.attributes = version_attributes self.version_number = version_cache.wanted_version_number save! end |
#new_version? ⇒ Boolean
Returns true if version requested is a new one.
134 135 136 |
# File 'lib/vidibus/versioning/mongoid.rb', line 134 def new_version? version_obj and version_obj.new_record? end |
#redo! ⇒ Object
Calls #version!(:next) and migrate!
94 95 96 97 |
# File 'lib/vidibus/versioning/mongoid.rb', line 94 def redo! version!(:next) migrate! end |
#reload_version(*args) ⇒ Object
Reloads this record and applies version attributes.
122 123 124 125 126 |
# File 'lib/vidibus/versioning/mongoid.rb', line 122 def reload_version(*args) reloaded_self = self.reload reloaded_self.version(*version_cache.version_args) if version_cache.version_args reloaded_self end |
#save(*args) ⇒ Object
Saves the record and handles version persistence.
100 101 102 103 104 |
# File 'lib/vidibus/versioning/mongoid.rb', line 100 def save(*args) return false if invalid? saved = persist_version (saved == nil) ? super(*args) : saved end |
#save!(*args) ⇒ Object
Raises a validation error if saving fails.
107 108 109 110 111 |
# File 'lib/vidibus/versioning/mongoid.rb', line 107 def save!(*args) unless save(*args) raise(::Mongoid::Errors::Validations, self) end end |
#undo! ⇒ Object
Calls #version!(:previous) and migrate!
88 89 90 91 |
# File 'lib/vidibus/versioning/mongoid.rb', line 88 def undo! version!(:previous) migrate! end |
#version(*args) ⇒ Object
Returns a copy of this object with versioned attributes applied.
Valid arguments are:
:new returns a new version of self
:next returns the next version of self, may be new as well
:previous returns the previous version of self
48 returns version 48 of self
54 55 56 57 58 |
# File 'lib/vidibus/versioning/mongoid.rb', line 54 def version(*args) self.class.find(_id).tap do |copy| copy.apply_version!(*args) end end |
#version!(*args) ⇒ Object
Applies versioned attributes on this object. Returns nil. For valid arguments, see #version.
63 64 65 |
# File 'lib/vidibus/versioning/mongoid.rb', line 63 def version!(*args) self.apply_version!(*args) end |
#version_object ⇒ Object
Returns the currently set version object.
129 130 131 |
# File 'lib/vidibus/versioning/mongoid.rb', line 129 def version_object version_cache.version_obj end |