| Class | RevisionRecord |
| In: |
lib/revision_record.rb
|
| Parent: | ActiveRecord::Base |
Find a specific revision record.
# File lib/revision_record.rb, line 61
61: def self.find_revision (klass, id, revision)
62: find(:first, :conditions => {:revisionable_type => klass.base_class.to_s, :revisionable_id => id, :revision => revision})
63: end
Create a revision record based on a record passed in. The attributes of the original record will be serialized. If it uses the acts_as_revisionable behavior, associations will be revisioned as well.
# File lib/revision_record.rb, line 9
9: def initialize (record)
10: super({})
11: self.revisionable_type = record.class.base_class.name
12: self.revisionable_id = record.id
13: associations = record.class.revisionable_associations if record.class.respond_to?(:revisionable_associations)
14: self.data = Zlib::Deflate.deflate(Marshal.dump(serialize_attributes(record, associations)))
15: end
Truncate the revisions for a record. Available options are :limit and :max_age.
# File lib/revision_record.rb, line 66
66: def self.truncate_revisions (revisionable_type, revisionable_id, options)
67: return unless options[:limit] or options[:minimum_age]
68:
69: conditions = ['revisionable_type = ? AND revisionable_id = ?', revisionable_type.base_class.to_s, revisionable_id]
70: if options[:minimum_age]
71: conditions.first << ' AND created_at <= ?'
72: conditions << options[:minimum_age].ago
73: end
74:
75: start_deleting_revision = find(:first, :conditions => conditions, :order => 'revision DESC', :offset => options[:limit])
76: if start_deleting_revision
77: delete_all(['revisionable_type = ? AND revisionable_id = ? AND revision <= ?', revisionable_type.base_class.to_s, revisionable_id, start_deleting_revision.revision])
78: end
79: end
Restore the revision to the original record. If any errors are encountered restoring attributes, they will be added to the errors object of the restored record.
# File lib/revision_record.rb, line 26
26: def restore
27: restore_class = self.revisionable_type.constantize
28:
29: # Check if we have a type field, if yes, assume single table inheritance and restore the actual class instead of the stored base class
30: sti_type = self.revision_attributes[restore_class.inheritance_column]
31: if sti_type
32: begin
33: restore_class = restore_class.send(:type_name_with_module, sti_type).constantize
34: rescue NameError
35: raise
36: # Seems our assumption was wrong and we have no STI
37: end
38: end
39:
40: attrs, association_attrs = attributes_and_associations(restore_class, self.revision_attributes)
41:
42: record = restore_class.new
43: attrs.each_pair do |key, value|
44: begin
45: record.send("#{key}=", value)
46: rescue
47: record.errors.add(key.to_sym, "could not be restored to #{value.inspect}")
48: end
49: end
50:
51: association_attrs.each_pair do |association, attribute_values|
52: restore_association(record, association, attribute_values)
53: end
54:
55: record.instance_variable_set(:@new_record, nil)
56:
57: return record
58: end