Module: Archivist::Base::ClassExtensions

Defined in:
lib/archivist/base.rb

Overview

these can’t get included in the class def until after all aliases are done

Instance Method Summary collapse

Instance Method Details

#copy_from_archive(conditions, delete = true) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# 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 =~ /archived/
  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) ⇒ Object



165
166
167
168
169
170
171
172
173
# 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) ⇒ Object



204
205
206
# File 'lib/archivist/base.rb', line 204

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

#restore_all(conditions = nil) ⇒ Object



208
209
210
# File 'lib/archivist/base.rb', line 208

def restore_all(conditions=nil)
  copy_from_archive(conditions)
end