forked from winton/acts_as_archive
/
acts_as_archive.rb
197 lines (166 loc) · 5.64 KB
/
acts_as_archive.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
require File.dirname(__FILE__) + '/acts_as_archive/gems'
ActsAsArchive::Gems.activate %w(also_migrate mover)
require 'also_migrate'
require 'mover'
$:.unshift File.dirname(__FILE__)
class ActsAsArchive
class <<self
attr_accessor :configuration, :disabled
def deprecate(msg)
if defined?(::ActiveSupport::Deprecation)
::ActiveSupport::Deprecation.warn msg
else
$stdout.puts msg
end
end
def disable(&block)
@mutex ||= Mutex.new
@mutex.synchronize do
self.disabled = true
block.call
end
ensure
self.disabled = false
end
def find(from)
from = [ from ] unless from.is_a?(::Array)
(@configuration || []).select do |hash|
if from[0].is_a?(::String)
from.include?(hash[:from].table_name)
else
from.include?(hash[:from])
end
end
end
def move(config, where, merge_options={})
config[:to].each do |to|
options = config[:options].dup.merge(merge_options)
if options[:conditions]
options[:conditions] += " AND #{where}"
elsif where
options[:conditions] = where
end
config[:from].move_to(to, options)
end
end
def update(*args)
deprecate "ActsAsArchive.update is deprecated and no longer necessary."
end
end
module Base
def self.included(base)
unless base.included_modules.include?(InstanceMethods)
base.extend ClassMethods
base.send :include, InstanceMethods
end
end
module ClassMethods
def acts_as_archive(*args)
return unless ActsAsArchive.find(self).empty?
ActsAsArchive.configuration ||= []
ActsAsArchive.configuration << (config = { :from => self })
options = args.last.is_a?(::Hash) ? args.pop : {}
options[:copy] = true
if options[:archive]
options[:magic] = 'restored_at'
else
options[:magic] = 'deleted_at' if options[:magic].nil?
options[:add] = [[ options[:magic], :datetime ]]
options[:ignore] = options[:magic]
options[:subtract] = 'restored_at'
options[:timestamps] = false if options[:timestamps].nil?
if args.empty?
class_eval <<-EVAL
class Archive < ActiveRecord::Base
set_table_name "archived_#{self.table_name}"
end
EVAL
args << self::Archive
end
args.each do |klass|
klass.class_eval <<-EVAL
record_timestamps = #{options[:timestamps].inspect}
acts_as_archive(#{self}, :archive => true)
EVAL
self.reflect_on_all_associations.each do |association|
if !ActsAsArchive.find(association.klass).empty? && association.options[:dependent]
opts = association.options.dup
opts[:class_name] = "::#{association.class_name}::Archive"
opts[:foreign_key] = association.primary_key_name
klass.send association.macro, association.name, opts
end
end
unless options[:migrate] == false
self.also_migrate klass.table_name, options
end
end
end
config[:to] = args
config[:options] = options
end
def delete_all!(*args)
ActsAsArchive.disable { self.delete_all(*args) }
end
def destroy_all!(*args)
ActsAsArchive.disable { self.destroy_all(*args) }
end
def migrate_from_acts_as_paranoid
time = Benchmark.measure do
ActsAsArchive.find(self).each do |config|
config = config.dup
config[:options][:copy] = false
ActsAsArchive.move(
config,
"`#{config[:options][:magic]}` IS NOT NULL",
:migrate => true
)
end
end
$stdout.puts "-- #{self}.migrate_from_acts_as_paranoid"
$stdout.puts " -> #{"%.4fs" % time.real}"
end
def restore_all(*args)
ActsAsArchive.deprecate "#{self}.restore_all is deprecated, please use #{self}.delete_all."
self.delete_all *args
end
end
module InstanceMethods
def delete!(*args)
ActsAsArchive.disable { self.delete(*args) }
end
def destroy!(*args)
ActsAsArchive.disable { self.destroy(*args) }
end
end
end
module DatabaseStatements
def self.included(base)
unless base.included_modules.include?(InstanceMethods)
base.send :include, InstanceMethods
base.class_eval do
unless method_defined?(:delete_sql_without_archive)
alias_method :delete_sql_without_archive, :delete_sql
alias_method :delete_sql, :delete_sql_with_archive
end
end
end
end
module InstanceMethods
def delete_sql_with_archive(sql, name = nil)
@mutex ||= Mutex.new
@mutex.synchronize do
unless ActsAsArchive.disabled
from, where = /DELETE FROM (.+)/i.match(sql)[1].split(/\s+WHERE\s+/i, 2)
from = from.strip.gsub(/`/, '').gsub('"','').split(/\s*,\s*/)
ActsAsArchive.find(from).each do |config|
ActsAsArchive.move(config, where)
end
end
end
delete_sql_without_archive(sql, name)
end
end
end
end
::ActiveRecord::Base.send(:include, ::ActsAsArchive::Base)
::ActiveRecord::ConnectionAdapters::DatabaseStatements.send(:include, ::ActsAsArchive::DatabaseStatements)