Module: ActsAsRevisionable::InstanceMethods
- Defined in:
- lib/acts_as_revisionable.rb
Instance Method Summary collapse
-
#create_revision! ⇒ Object
Create a revision record based on this record and save it to the database.
-
#destroy_with_revision ⇒ Object
Destroy the record while recording the revision.
-
#disable_revisioning ⇒ Object
Disable the revisioning behavior inside of a block passed to the method.
-
#last_revision ⇒ Object
Get the last revision record.
-
#restore_revision(revision_number) ⇒ Object
Restore a revision of the record and return it.
-
#restore_revision!(revision_number) ⇒ Object
Restore a revision of the record and save it along with restored associations.
-
#revision(revision_number) ⇒ Object
Get a specified revision record.
- #revision_record_class ⇒ Object
-
#store_revision ⇒ Object
Call this method to implement revisioning.
-
#truncate_revisions!(options = nil) ⇒ Object
Truncate the number of revisions kept for this record.
Instance Method Details
#create_revision! ⇒ Object
Create a revision record based on this record and save it to the database.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/acts_as_revisionable.rb', line 247 def create_revision! = self.class. revision = revision_record_class.new(self, [:encoding]) if [:meta].is_a?(Hash) [:meta].each do |attribute, value| (revision, attribute, value) end elsif [:meta].is_a?(Array) [:meta].each do |attribute| (revision, attribute, attribute.to_sym) end elsif [:meta] (revision, [:meta], [:meta].to_sym) end revision.save! return revision end |
#destroy_with_revision ⇒ Object
Destroy the record while recording the revision.
285 286 287 288 289 |
# File 'lib/acts_as_revisionable.rb', line 285 def destroy_with_revision store_revision do destroy_without_revision end end |
#disable_revisioning ⇒ Object
Disable the revisioning behavior inside of a block passed to the method.
272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/acts_as_revisionable.rb', line 272 def disable_revisioning save_val = @revisions_disabled retval = nil begin @revisions_disabled = true retval = yield if block_given? ensure @revisions_disabled = save_val end return retval end |
#last_revision ⇒ Object
Get the last revision record
200 201 202 |
# File 'lib/acts_as_revisionable.rb', line 200 def last_revision self.class.last_revision(id) end |
#restore_revision(revision_number) ⇒ Object
Restore a revision of the record and return it. The record is not saved to the database. If there is a problem restoring values, errors will be added to the record.
185 186 187 |
# File 'lib/acts_as_revisionable.rb', line 185 def restore_revision(revision_number) self.class.restore_revision(self.id, revision_number) end |
#restore_revision!(revision_number) ⇒ Object
Restore a revision of the record and save it along with restored associations.
190 191 192 |
# File 'lib/acts_as_revisionable.rb', line 190 def restore_revision!(revision_number) self.class.restore_revision!(self.id, revision_number) end |
#revision(revision_number) ⇒ Object
Get a specified revision record
195 196 197 |
# File 'lib/acts_as_revisionable.rb', line 195 def revision(revision_number) self.class.revision(id, revision_number) end |
#revision_record_class ⇒ Object
291 292 293 |
# File 'lib/acts_as_revisionable.rb', line 291 def revision_record_class self.class.revision_record_class end |
#store_revision ⇒ Object
Call this method to implement revisioning. The object changes should happen inside the block.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/acts_as_revisionable.rb', line 205 def store_revision if new_record? || @revisions_disabled return yield else retval = nil revision = nil begin revision_record_class.transaction do begin read_only = self.class.first(:conditions => {self.class.primary_key => self.id}, :readonly => true) if read_only revision = read_only.create_revision! truncate_revisions! end rescue => e logger.warn(e) if logger end disable_revisioning do retval = yield end raise ActiveRecord::Rollback unless errors.empty? revision.trash! if destroyed? end rescue => e # In case the database doesn't support transactions if revision begin revision.destroy rescue => e logger.warn(e) if logger end end raise e end return retval end end |
#truncate_revisions!(options = nil) ⇒ Object
Truncate the number of revisions kept for this record. Available options are :limit and :minimum_age.
266 267 268 269 |
# File 'lib/acts_as_revisionable.rb', line 266 def truncate_revisions!( = nil) = {:limit => self.class.[:limit], :minimum_age => self.class.[:minimum_age]} unless revision_record_class.truncate_revisions(self.class, self.id, ) end |