Report abuse


			
Index: /Users/cph/work/test/vendor/plugins/acts_as_audited/lib/acts_as_audited.rb
===================================================================
--- /Users/cph/work/test/vendor/plugins/acts_as_audited/lib/acts_as_audited.rb	(revision 42)
+++ /Users/cph/work/test/vendor/plugins/acts_as_audited/lib/acts_as_audited.rb	(working copy)
@@ -73,6 +73,7 @@

             after_create :audit_create
             after_update :audit_update
+            before_destroy :audit_before_destroy
             after_destroy :audit_destroy
             after_save :clear_changed_attributes

@@ -158,7 +159,18 @@
           def audit_update
             write_audit(:update) if changed?
           end
-  
+
+          # Populates the @changed_attributes before destroying the record so that its full state will be recorded as an audit.
+          def audit_before_destroy
+            attributes.each_pair do |attr_name, attr_value|
+              attr_name = attr_name.to_s
+              @changed_attributes ||= {}
+              old_value = @changed_attributes[attr_name] ?
+                @changed_attributes[attr_name].first : self[attr_name]
+              @changed_attributes[attr_name] = [old_value, nil]
+            end
+          end
+
           def audit_destroy
             write_audit(:destroy)
           end
@@ -198,6 +210,19 @@
       end # InstanceMethods

       module SingletonMethods
+        # Revives a previously destroyed model.
+        def revive(deleted_id)
+          deleted_revision = Audit.find_by_auditable_type_and_auditable_id_and_action self.to_s, deleted_id, "destroy" rescue nil
+          return if deleted_revision.nil?
+          returning self.new do |model|
+            model.attributes.keys.each do |attr_name|
+              model.send "#{attr_name}=", deleted_revision.attributes["changes"][attr_name.to_s].first
+            end
+            model.id = deleted_id
+            model.save
+          end
+        end
+        
         # Returns an array of columns that are audited.  See non_audited_columns
         def audited_columns
           self.columns.select { |c| !non_audited_columns.include?(c.name) }