module Archivist::Base::ClassExtensions

Public Instance Methods

copy_from_archive(conditions,delete=true) click to toggle source
# File lib/archivist/base.rb, line 175
def copy_from_archive(conditions,delete=true)
  where = sanitize_sql(conditions)
  where = where.gsub("#{table_name}","archived_#{table_name}") unless where.nil? || where =~ %rarchived/
  unless where == ""
    found = self::Archive.where(where)
  else
    found = self::Archive.all
  end

  found.each do |m|
    self.transaction do
      my_attribute_names = self.new.attribute_names
      #this should be Hash.select but 1.8.7 returns an array from select but a hash from reject... dumb
      attrs = m.attributes.reject{|k,v| !my_attribute_names.include?(k.to_s)} 

      if self.where(:id=>m.id).empty?
        new_m = self.create(attrs)
        connection.execute(%Q{UPDATE #{table_name} 
                              SET #{self.primary_key} = #{m.id} 
                              WHERE #{self.primary_key} = #{new_m.id}
                             })
      else
        self.where(:id=>m.id).first.update_attributes(attrs)
      end
      m.destroy if delete  
    end
  end
end
copy_to_archive(conditions,delete=true,&block) click to toggle source
# File lib/archivist/base.rb, line 165
def copy_to_archive(conditions,delete=true,&block)
  where = sanitize_sql(conditions)
  found = self.where(where)
  
  found.each do |m|
    result = m.copy_self_to_archive(&block)
    m.destroy! if delete && result
  end
end
delete_all(conditions=nil) click to toggle source
# File lib/archivist/base.rb, line 204
def delete_all(conditions=nil)
  copy_to_archive(conditions)
end
restore_all(conditions=nil) click to toggle source
# File lib/archivist/base.rb, line 208
def restore_all(conditions=nil)
  copy_from_archive(conditions)
end