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 "        class ::\#{options[:class]} < ActiveRecord::Base\n          set_table_name \"\#{options[:table]}\"\n        end\n      EVAL\n      klass = eval(\"::\#{options[:class]}\")\n    end\n\n    klass.record_timestamps = options[:timestamps].inspect\n    klass.acts_as_archive(:class => self, :archive => true)\n\n    self.reflect_on_all_associations.each do |association|\n      if association.options[:dependent] && !association.options[:polymorphic] && !ActsAsArchive.find(association.klass).empty?\n        opts = association.options.dup\n        opts[:class_name] = \"::\#{association.class_name}::Archive\"\n        opts[:foreign_key] = association.primary_key_name\n        klass.send association.macro, association.name, opts\n      end\n    end\n\n    unless options[:migrate] == false\n      AlsoMigrate.configuration ||= []\n      AlsoMigrate.configuration << options.merge(\n        :source => self.table_name,\n        :destination => klass.table_name\n      )\n    end\n  end\n\n  config[:to] = klass\n  config[:options] = options\nend\n"

#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