Module: ActsAsRevisionable::ClassMethods

Defined in:
lib/acts_as_revisionable.rb

Instance Method Summary collapse

Instance Method Details

#empty_trash(max_age) ⇒ Object

Delete all revision records for deleted items that are older than the specified maximum age in seconds.



139
140
141
# File 'lib/acts_as_revisionable.rb', line 139

def empty_trash(max_age)
  revision_record_class.empty_trash(self, max_age)
end

#last_revision(id) ⇒ Object

Get the last revision for a specified id.



77
78
79
# File 'lib/acts_as_revisionable.rb', line 77

def last_revision(id)
  revision_record_class.last_revision(self, id)
end

#restore_last_revision(id) ⇒ Object

Load the last revision for a record with the specified id. Associations added since the revision was created will still be in the restored record. If you want to save a revision with associations properly, use restore_last_revision!



104
105
106
107
# File 'lib/acts_as_revisionable.rb', line 104

def restore_last_revision(id)
  revision_record = last_revision(id)
  return revision_record.restore if revision_record
end

#restore_last_revision!(id) ⇒ Object

Load the last revision for a record with the specified id and save it to the database. You should always use this method to save a revision if it has associations.



111
112
113
114
115
116
117
118
119
# File 'lib/acts_as_revisionable.rb', line 111

def restore_last_revision!(id)
  record = restore_last_revision(id)
  if record
    record.store_revision do
      save_restorable_associations(record, revisionable_associations)
    end
  end
  return record
end

#restore_revision(id, revision_number) ⇒ Object

Load a revision for a record with a particular id. Associations added since the revision was created will still be in the restored record. If you want to save a revision with associations properly, use restore_revision!



84
85
86
87
# File 'lib/acts_as_revisionable.rb', line 84

def restore_revision(id, revision_number)
  revision_record = revision(id, revision_number)
  return revision_record.restore if revision_record
end

#restore_revision!(id, revision_number) ⇒ Object

Load a revision for a record with a particular id and save it to the database. You should always use this method to save a revision if it has associations.



91
92
93
94
95
96
97
98
99
# File 'lib/acts_as_revisionable.rb', line 91

def restore_revision!(id, revision_number)
  record = restore_revision(id, revision_number)
  if record
    record.store_revision do
      save_restorable_associations(record, revisionable_associations)
    end
  end
  return record
end

#revision(id, revision_number) ⇒ Object

Get a revision for a specified id.



72
73
74
# File 'lib/acts_as_revisionable.rb', line 72

def revision(id, revision_number)
  revision_record_class.find_revision(self, id, revision_number)
end

#revision_record_classObject



143
144
145
# File 'lib/acts_as_revisionable.rb', line 143

def revision_record_class
  acts_as_revisionable_options[:class_name].constantize
end

#revisionable_associations(options = ) ⇒ Object

Returns a hash structure used to identify the revisioned associations.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/acts_as_revisionable.rb', line 122

def revisionable_associations(options = acts_as_revisionable_options[:associations])
  return nil unless options
  options = [options] unless options.kind_of?(Array)
  associations = {}
  options.each do |association|
    if association.kind_of?(Symbol)
      associations[association] = true
    elsif association.kind_of?(Hash)
      association.each_pair do |key, value|
        associations[key] = revisionable_associations(value)
      end
    end
  end
  return associations
end