public
Description: ActiveRecord plugin allowing you to hide and restore records without actually deleting them.
Clone URL: git://github.com/technoweenie/acts_as_paranoid.git
finished acts_as_paranoid w/ rdocs and tests

git-svn-id: http://svn.techno-weenie.net/projects/acts_as_paranoid@85 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Sat Sep 17 15:57:36 -0700 2005
commit  f62f28d511c1103899e30d804061841531af34ea
tree    4e2ff5fe95071a26b181cbd5cbc45475b435d669
parent  0355a91625eeb3f0ce1e48083c6698aedf8eed70
...
1
 
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
6
7
8
9
10
11
12
 
13
14
15
...
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
...
57
58
59
60
61
62
63
64
65
66
67
68
 
69
...
 
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
...
47
48
49
 
50
 
 
 
 
51
52
53
54
55
 
 
 
 
 
 
 
 
56
57
58
59
60
61
62
63
64
65
66
67
 
 
68
 
 
69
70
71
72
 
73
 
 
 
 
 
 
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 
94
95
96
...
98
99
100
 
 
 
 
 
 
 
 
101
102
103
0
@@ -1,15 +1,44 @@
0
-module ActiveRecord
0
+module ActiveRecord #:nodoc:
0
   module Acts #:nodoc:
0
+ # Overrides some basic methods for the current model so that calling #destroy sets a 'deleted_at' field to the current timestamp.
0
+ # This assumes the table has a deleted_at date/time field. Most normal model operations will work, but there will be some oddities.
0
+ #
0
+ # class Widget < ActiveRecord::Base
0
+ # acts_as_paranoid
0
+ # end
0
+ #
0
+ # Widget.find(:all)
0
+ # # SELECT * FROM widgets WHERE widgets.deleted_at IS NULL
0
+ #
0
+ # Widget.find(:first, :conditions => ['title = ?', 'test'], :order => 'title')
0
+ # # SELECT * FROM widgets WHERE widgets.deleted_at IS NULL AND title = 'test' ORDER BY title LIMIT 1
0
+ #
0
+ # Widget.find_with_deleted(:all)
0
+ # # SELECT * FROM widgets
0
+ #
0
+ # Widget.count
0
+ # # SELECT COUNT(*) FROM widgets WHERE widgets.deleted_at IS NULL
0
+ #
0
+ # Widget.count ['title = ?', 'test']
0
+ # # SELECT COUNT(*) FROM widgets WHERE widgets.deleted_at IS NULL AND title = 'test'
0
+ #
0
+ # Widget.count_with_deleted
0
+ # # SELECT COUNT(*) FROM widgets
0
+ #
0
+ # @widget.destroy
0
+ # # UPDATE widgets SET deleted_at = '2005-09-17 17:46:36' WHERE id = 1
0
+ #
0
+ # @widget.destroy!
0
+ # # DELETE FROM widgets WHERE id = 1
0
     module Paranoid
0
       def self.included(base) # :nodoc:
0
         base.extend ClassMethods
0
       end
0
 
0
- # Specify this act
0
       module ClassMethods
0
         def acts_as_paranoid
0
           class_eval do
0
- alias_method :destroy!, :destroy
0
+ alias_method :destroy_without_callbacks!, :destroy_without_callbacks
0
             class << self
0
               alias_method :find_with_deleted, :find
0
               alias_method :count_with_deleted, :count
0
@@ -18,38 +47,50 @@ module ActiveRecord
0
           end
0
         end
0
       end
0
- end
0
     
0
- module ParanoidMethods #:nodoc:
0
- def self.included(base) # :nodoc:
0
- base.extend ClassMethods
0
- end
0
+ module ParanoidMethods #:nodoc:
0
+ def self.included(base) # :nodoc:
0
+ base.extend ClassMethods
0
+ end
0
       
0
- module ClassMethods
0
- def find(*args)
0
- if [:all, :first].include?(args.first)
0
- constrain = "#{table_name}.deleted_at IS NULL"
0
- constrains = (scope_constrains.nil? or scope_constrains[:conditions].nil? or scope_constrains[:conditions] == constrain) ?
0
- constrain :
0
- "#{scope_constrains[:conditions]} AND #{constrain}"
0
- constrain(:conditions => constrains) { return find_with_deleted(*args) }
0
+ module ClassMethods
0
+ #
0
+ def find(*args)
0
+ if [:all, :first].include?(args.first)
0
+ constrain = "#{table_name}.deleted_at IS NULL"
0
+ constrains = (scope_constrains.nil? or scope_constrains[:conditions].nil? or scope_constrains[:conditions] == constrain) ?
0
+ constrain :
0
+ "#{scope_constrains[:conditions]} AND #{constrain}"
0
+ constrain(:conditions => constrains) { return find_with_deleted(*args) }
0
+ end
0
+ find_with_deleted(*args)
0
           end
0
- find_with_deleted(*args)
0
- end
0
     
0
- def count(conditions = nil, joins = nil)
0
- constrain(:conditions => "#{table_name}.deleted_at IS NULL") { count_with_deleted(conditions, joins) }
0
+ def count(conditions = nil, joins = nil)
0
+ constrain(:conditions => "#{table_name}.deleted_at IS NULL") { count_with_deleted(conditions, joins) }
0
+ end
0
         end
0
- end
0
       
0
- def destroy
0
- unless new_record?
0
- sql = self.class.send(:sanitize_sql,
0
- ["UPDATE #{self.class.table_name} SET deleted_at = ? WHERE id = ?",
0
- self.class.default_timezone == :utc ? Time.now.utc : Time.now, id])
0
- self.connection.update(sql)
0
+ def destroy_without_callbacks
0
+ unless new_record?
0
+ sql = self.class.send(:sanitize_sql,
0
+ ["UPDATE #{self.class.table_name} SET deleted_at = ? WHERE id = ?",
0
+ self.class.default_timezone == :utc ? Time.now.utc : Time.now, id])
0
+ self.connection.update(sql)
0
+ end
0
+ freeze
0
+ end
0
+
0
+ def destroy_with_callbacks!
0
+ return false if callback(:before_destroy) == false
0
+ result = destroy_without_callbacks!
0
+ callback(:after_destroy)
0
+ result
0
+ end
0
+
0
+ def destroy!
0
+ transaction { destroy_with_callbacks! }
0
         end
0
- freeze
0
       end
0
     end
0
   end
0
@@ -57,11 +98,4 @@ end
0
 
0
 ActiveRecord::Base.class_eval do
0
   include ActiveRecord::Acts::Paranoid
0
-end
0
-
0
-#ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
0
-# alias_method :find_with_deleted, :find
0
-# def find(*args)
0
-# constrain(:conditions => "#{@join_table}.deleted_at IS NULL") { find_with_deleted(*args) }
0
-# end
0
-#end
0
\ No newline at end of file
0
+end
0
\ No newline at end of file

Comments

    No one has commented yet.