Module: ActsAsArchive::Base::ClassMethods

Defined in:
lib/acts_as_archive.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_archive(options = {}) ⇒ Object



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

#delete_all!(*args) ⇒ Object



151
152
153
# File 'lib/acts_as_archive.rb', line 151

def delete_all!(*args)
  ActsAsArchive.disable { self.delete_all(*args) }
end

#destroy_all!(*args) ⇒ Object



155
156
157
# File 'lib/acts_as_archive.rb', line 155

def destroy_all!(*args)
  ActsAsArchive.disable { self.destroy_all(*args) }
end

#migrate_from_acts_as_paranoidObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/acts_as_archive.rb', line 159

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

#restore_all(*args) ⇒ Object



175
176
177
178
# File 'lib/acts_as_archive.rb', line 175

def restore_all(*args)
  ActsAsArchive.deprecate "#{self}.restore_all is deprecated, please use #{self}.delete_all."
  self.delete_all *args
end