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

public
Description: Rails plugin that provides the ability to soft delete models
Clone URL: git://github.com/ajh/acts_as_soft_deletable.git
Add finders on live class which will also return deleted records.

Add find_with_deleted on live class which is similar to find. Also add 
support
for dynamic finders like find_with_deleted_by_name or
find_all_with_deleted_by_name.
ajh (author)
Thu May 29 15:35:37 -0700 2008
commit  88141cacdcb6895a9811a5e6cff150afa0b47cb5
tree    60bad273c83e9ee0fd8450c7692b2cdc404118f5
parent  f521949514865d75d908f3bec6943ec1435e6604
0
...
15
16
17
18
 
 
 
 
 
 
19
20
21
...
15
16
17
 
18
19
20
21
22
23
24
25
26
0
@@ -15,7 +15,12 @@ They can later be restored easily.
0
     model.destroy # removes row from artists table, and adds a row to
0
                     # deleted_artists table
0
 
0
- deleted = Artist::Deleted.find(34)
0
+
0
+ # The deleted record can be retrieved several ways
0
+ deleted = Artist::Deleted.find(34) # using the deleted class
0
+ deleted = Artist.find_with_deleted(34) # using the live class
0
+ deleted = Artist.find_with_deleted_by_id(34) # dynamic finds work too
0
+
0
     deleted.undestroy! # adds the row back to the artists table, and removes
0
                         # if from the deleted_artists table
0
 
...
135
136
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
139
140
...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
0
@@ -135,6 +135,51 @@ module ActiveRecord #:nodoc:
0
           def deleted_class
0
             @deleted_class
0
           end
0
+
0
+ # Same as ActiveRecord::Base#find except that it will also return
0
+ # deleted records as well as live ones.
0
+ #
0
+ # Some of find's options are not supported and will raise an
0
+ # exception. These include: order, limit, and offset.
0
+ def find_with_deleted(*args)
0
+ if args.last.is_a?(Hash)
0
+ [:order, :limit, :offset].each do |option|
0
+ raise ArgumentError.new "#{option} option is not supported" if args.last.key?(option)
0
+ end
0
+ end
0
+
0
+ case args.first
0
+ when :all
0
+ find(*args) + deleted_class.find(*args)
0
+ when :first
0
+ find(*args) || deleted_class.find(*args)
0
+ else
0
+ (live_results = find(*args)) rescue ActiveRecord::RecordNotFound
0
+ (deleted_results = deleted_class.find(*args)) rescue ActiveRecord::RecordNotFound
0
+
0
+ live_results && deleted_results ? \
0
+ live_results + deleted_results :
0
+ live_results || deleted_results
0
+ end
0
+ end
0
+
0
+ # Enables dynamic finders like
0
+ # find_with_deleted_by_user_name(user_name) and
0
+ # find_all_with_deleted_by_user_name_and_password(user_name,
0
+ # password)
0
+ def method_missing(method_id, *arguments)
0
+ if /^find_(all_with_deleted_by|with_deleted_by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
0
+ m = method_id.to_s.sub(%r/with_deleted_/, '')
0
+ live_results = send(m, *arguments)
0
+ deleted_results = deleted_class.send(m, *arguments)
0
+
0
+ live_results && deleted_results ? \
0
+ live_results + deleted_results :
0
+ live_results || deleted_results
0
+ else
0
+ super
0
+ end
0
+ end
0
         end
0
 
0
         # These methods will be available as instance methods for the Model

Comments

    No one has commented yet.