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
|
# File 'lib/acts_as_archive.rb', line 87
def acts_as_archive(options={})
return unless ActsAsArchive.find(self).empty?
ActsAsArchive.configuration ||= []
ActsAsArchive.configuration << (config = { :from => self })
options[:copy] = true
if options[:archive]
options[:magic] = 'restored_at'
klass = options[:class]
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?
unless options[:class]
options[:class] = "#{self}::Archive"
end
unless options[:table]
options[:table] = "archived_#{self.table_name}"
end
klass = eval(options[:class]) rescue nil
if klass
klass.send :set_table_name, options[:table]
else
eval <<-EVAL
class ::#{options[:class]} < ActiveRecord::Base
set_table_name "#{options[:table]}"
end
EVAL
klass = eval("::#{options[:class]}")
end
klass.record_timestamps = options[:timestamps].inspect
klass.acts_as_archive(:class => self, :archive => true)
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
AlsoMigrate.configuration ||= []
AlsoMigrate.configuration << options.merge(
:source => self.table_name,
:destination => klass.table_name
)
end
end
config[:to] = klass
config[:options] = options
end
|