forked from softcraft-development/rails3_acts_as_paranoid
/
rails3_acts_as_paranoid.rb
280 lines (227 loc) · 8.06 KB
/
rails3_acts_as_paranoid.rb
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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
require 'active_record'
require 'validations/uniqueness_without_deleted'
module ActiveRecord
class Relation
def paranoid?
klass.try(:paranoid?) ? true : false
end
def paranoid_deletion_attributes
{ klass.paranoid_column => klass.delete_now_value }
end
alias_method :destroy!, :destroy
def destroy(id)
if paranoid?
update_all(paranoid_deletion_attributes, {:id => id})
else
destroy!(id)
end
end
alias_method :really_delete_all!, :delete_all
def delete_all!(conditions = nil)
if conditions
# This idea comes out of Rails 3.1 ActiveRecord::Record.delete_all
where(conditions).delete_all!
else
really_delete_all!
end
end
def delete_all(conditions = nil)
if paranoid?
update_all(paranoid_deletion_attributes, conditions)
else
delete_all!(conditions)
end
end
def arel=(a)
@arel = a
end
def with_deleted
wd = self.clone
wd.default_scoped = false
wd.arel = self.build_arel
wd
end
end
end
module ActsAsParanoid
def paranoid?
self.included_modules.include?(InstanceMethods)
end
def validates_as_paranoid
extend ParanoidValidations::ClassMethods
end
def acts_as_paranoid(options = {})
raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
class_attribute :paranoid_configuration, :paranoid_column_reference
self.paranoid_configuration = { :column => "deleted_at", :column_type => "time", :recover_dependent_associations => true, :dependent_recovery_window => 2.minutes }
self.paranoid_configuration.merge!({ :deleted_value => "deleted" }) if options[:column_type] == "string"
self.paranoid_configuration.merge!(options) # user options
raise ArgumentError, "'time', 'boolean' or 'string' expected for :column_type option, got #{paranoid_configuration[:column_type]}" unless ['time', 'boolean', 'string'].include? paranoid_configuration[:column_type]
self.paranoid_column_reference = "#{self.table_name}.#{paranoid_configuration[:column]}"
return if paranoid?
# Magic!
default_scope where("#{paranoid_column_reference} IS ?", nil)
scope :paranoid_deleted_around_time, lambda {|value, window|
if self.class.respond_to?(:paranoid?) && self.class.paranoid?
if self.class.paranoid_column_type == 'time' && ![true, false].include?(value)
self.where("#{self.class.paranoid_column} > ? AND #{self.class.paranoid_column} < ?", (value - window), (value + window))
else
self.only_deleted
end
end if paranoid_configuration[:column_type] == 'time'
}
include InstanceMethods
extend ClassMethods
end
module ClassMethods
def self.extended(base)
base.define_callbacks :recover
end
def before_recover(method)
set_callback :recover, :before, method
end
def after_recover(method)
set_callback :recover, :after, method
end
def with_deleted
self.unscoped
end
def only_deleted
self.unscoped.where("#{paranoid_column_reference} IS NOT ?", nil)
end
def deletion_conditions(id_or_array)
["id in (?)", [id_or_array].flatten]
end
def delete!(id_or_array)
delete_all!(deletion_conditions(id_or_array))
end
def delete(id_or_array)
delete_all(deletion_conditions(id_or_array))
end
def delete_all!(conditions = nil)
self.unscoped.delete_all!(conditions)
end
def delete_all(conditions = nil)
update_all ["#{paranoid_configuration[:column]} = ?", delete_now_value], conditions
end
def paranoid_column
paranoid_configuration[:column].to_sym
end
def paranoid_column_type
paranoid_configuration[:column_type].to_sym
end
def dependent_associations
self.reflect_on_all_associations.select {|a| [:destroy, :delete_all].include?(a.options[:dependent]) }
end
def delete_now_value
case paranoid_configuration[:column_type]
when "time" then Time.now
when "boolean" then true
when "string" then paranoid_configuration[:deleted_value]
end
end
end
module InstanceMethods
def paranoid_value
self.send(self.class.paranoid_column)
end
def destroy!
with_transaction_returning_status do
run_callbacks :destroy do
act_on_dependent_destroy_associations
self.class.delete_all!(self.class.primary_key.to_sym => self.id)
self.paranoid_value = self.class.delete_now_value
freeze
end
end
end
def destroy
if paranoid_value.nil?
with_transaction_returning_status do
run_callbacks :destroy do
self.class.delete_all(self.class.primary_key.to_sym => self.id)
self.paranoid_value = self.class.delete_now_value
self
end
end
else
destroy!
end
end
def delete!
with_transaction_returning_status do
act_on_dependent_destroy_associations
self.class.delete_all!(self.class.primary_key.to_sym => self.id)
self.paranoid_value = self.class.delete_now_value
freeze
end
end
def delete
if paranoid_value.nil?
with_transaction_returning_status do
self.class.delete_all(self.class.primary_key.to_sym => self.id)
self.paranoid_value = self.class.delete_now_value
self
end
else
delete!
end
end
def recover(options={})
options = {
:recursive => self.class.paranoid_configuration[:recover_dependent_associations],
:recovery_window => self.class.paranoid_configuration[:dependent_recovery_window]
}.merge(options)
self.class.transaction do
run_callbacks :recover do
recover_dependent_associations(options[:recovery_window], options) if options[:recursive]
self.paranoid_value = nil
self.save
end
end
end
def recover_dependent_associations(window, options)
self.class.dependent_associations.each do |association|
if association.collection? && self.send(association.name).paranoid?
self.send(association.name).unscoped do
self.send(association.name).paranoid_deleted_around_time(paranoid_value, window).each do |object|
object.recover(options) if object.respond_to?(:recover)
end
end
elsif association.macro == :has_one && association.klass.paranoid?
association.klass.unscoped do
object = association.klass.paranoid_deleted_around_time(paranoid_value, window).send('find_by_'+association.foreign_key, self.id)
object.recover(options) if object && object.respond_to?(:recover)
end
elsif association.klass.paranoid?
association.klass.unscoped do
id = self.send(association.foreign_key)
object = association.klass.paranoid_deleted_around_time(paranoid_value, window).find_by_id(id)
object.recover(options) if object && object.respond_to?(:recover)
end
end
end
end
def act_on_dependent_destroy_associations
self.class.dependent_associations.each do |association|
if association.collection? && self.send(association.name).paranoid?
association.klass.with_deleted.instance_eval("find_all_by_#{association.foreign_key}(#{self.id.to_json})").each do |object|
object.destroy!
end
end
end
end
def deleted?
!paranoid_value.nil?
end
alias_method :destroyed?, :deleted?
private
def paranoid_value=(value)
self.send("#{self.class.paranoid_column}=", value)
end
end
end
# Extend ActiveRecord's functionality
ActiveRecord::Base.send :extend, ActsAsParanoid
# Push the recover callback onto the activerecord callback list
ActiveRecord::Callbacks::CALLBACKS.push(:before_recover, :after_recover)