Module: ActsAsRevisionable::InstanceMethods

Defined in:
lib/acts_as_revisionable.rb

Instance Method Summary collapse

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!
  revision_options = self.class.acts_as_revisionable_options
  revision = revision_record_class.new(self, revision_options[:encoding])
  if revision_options[:meta].is_a?(Hash)
    revision_options[:meta].each do |attribute, value|
      set_revision_meta_attribute(revision, attribute, value)
    end
  elsif revision_options[:meta].is_a?(Array)
    revision_options[:meta].each do |attribute|
      set_revision_meta_attribute(revision, attribute, attribute.to_sym)
    end
  elsif revision_options[:meta]
    set_revision_meta_attribute(revision, revision_options[:meta], revision_options[:meta].to_sym)
  end
  revision.save!
  return revision
end

#destroy_with_revisionObject

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_revisioningObject

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_revisionObject

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_classObject



291
292
293
# File 'lib/acts_as_revisionable.rb', line 291

def revision_record_class
  self.class.revision_record_class
end

#store_revisionObject

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!(options = nil)
  options = {:limit => self.class.acts_as_revisionable_options[:limit], :minimum_age => self.class.acts_as_revisionable_options[:minimum_age]} unless options
  revision_record_class.truncate_revisions(self.class, self.id, options)
end