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
|
# File 'lib/acts_as_pack_rat.rb', line 9
def acts_as_pack_rat
class << self
[:find, :count].each do |m|
eval %{
def #{m}(*args)
# puts "acts_as_pack_rat: #{m}!"
do_without_deleted {#{m}_with_deleted(*args)}
end
}
end
[:average, :minimum, :maximum, :sum].each do |m|
eval %{
def #{m}(column_name, options = {})
# puts "acts_as_pack_rat: #{m}!"
do_without_deleted {#{m}_with_deleted(column_name, options)}
end
}
end
def exists_with_deleted?(id_or_conditions)
!find_with_deleted(:first, :select => "#{table_name}.#{primary_key}", :conditions => expand_id_conditions(id_or_conditions)).nil?
rescue ActiveRecord::ActiveRecordError
false
end
end
alias_method :ar_destroy, :destroy
class_eval do
def destroy
unless new_record?
puts "acts_as_pack_rat: destroy"
return false if callback(:before_destroy) == false
if self.respond_to?(:status_id)
self.status = Status.deleted
else
self.deleted_at = Time.now
end
result = self.send(:update_without_callbacks)
callback(:after_destroy)
return result
end
end
def destroy!
ar_destroy
end
def pack_rat?
true
end
end
protected
def do_without_deleted(&block)
if self.column_names.include?("status_id")
with_scope({:find => { :conditions => ["#{table_name}.status_id != ?", Status.deleted.id] } }, :merge, &block)
else
with_scope({:find => { :conditions => ["#{table_name}.deleted_at IS NULL"] } }, :merge, &block)
end
end
end
|