Module: ActsAsArchive::Base::Destroy::ClassMethods

Defined in:
lib/acts_as_archive/base/destroy.rb

Instance Method Summary collapse

Instance Method Details

#copy_to_archive(conditions, import = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/acts_as_archive/base/destroy.rb', line 19

def copy_to_archive(conditions, import=false)
  add_conditions!(where = '', conditions)
  insert_cols = column_names.clone
  select_cols = column_names.clone
  if insert_cols.include?('deleted_at')
    unless import
      select_cols[select_cols.index('deleted_at')] = "'#{Time.now.utc.to_s(:db)}'"
    end
  else
    insert_cols << 'deleted_at'
    select_cols << "'#{Time.now.utc.to_s(:db)}'"
  end

  insert_cols.map! { |col| connection.quote_column_name(col) }
  select_cols.map! { |col| col =~ /^\'/ ? col : connection.quote_column_name(col) }

  connection.execute(%{
    INSERT INTO archived_#{table_name} (#{insert_cols.join(', ')})
      SELECT #{select_cols.join(', ')}
      FROM #{table_name}
      #{where}
  })
  connection.execute("DELETE FROM #{table_name} #{where}")
end

#delete_all(conditions = nil) ⇒ Object



44
45
46
# File 'lib/acts_as_archive/base/destroy.rb', line 44

def delete_all(conditions=nil)
  copy_to_archive(conditions)
end