GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Rails Plugin - soft-delete your ActiveRecord records with a deleted_at timestamp. This is a much more explicit version of ActsAsParanoid
Homepage: http://6brand.com
Clone URL: git://github.com/JackDanger/permanent_records.git
permanent_records / lib / permanent_records.rb
100644 69 lines (57 sloc) 1.623 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module PermanentRecords
 
  def self.included(base)
    base.extend Scopes
    base.send :include, InstanceMethods
    base.alias_method_chain :destroy, :permanent_record
  end
  
  module Scopes
    def with_deleted
      with_scope :find => {:conditions => "#{quoted_table_name}.deleted_at IS NOT NULL"} do
        yield
      end
    end
    
    def with_not_deleted
      with_scope :find => {:conditions => "#{quoted_table_name}.deleted_at IS NULL"} do
        yield
      end
    end
    
    # this next bit is basically stolen from the scope_out plugin
    [:deleted, :not_deleted].each do |name|
      define_method "find_#{name}" do |*args|
        send("with_#{name}") { find(*args) }
      end
 
      define_method "count_#{name}" do |*args|
        send("with_#{name}") { count(*args) }
      end
 
      define_method "calculate_#{name}" do |*args|
        send("with_#{name}") { calculate(*args) }
      end
 
      define_method "find_all_#{name}" do |*args|
        send("with_#{name}") { find(:all, *args) }
      end
    end
  end
  
  module InstanceMethods
    
    def is_permanent?
      respond_to?(:deleted_at)
    end
    
    def deleted?
      deleted_at if is_permanent?
    end
    
    def revive
      return self unless is_permanent?
      record = self.class.find(id)
      record.update_attribute(:deleted_at, nil)
      record
    end
    
    def destroy_with_permanent_record(force = nil)
      if :force == force || !is_permanent?
        destroy_without_permanent_record
      else
        update_attribute(:deleted_at, Time.now)
        freeze
        self
      end
    end
  end
end