Yes, I have good reasons not to keep the removed BOMs in the same collection as the current ones.
# Copies Bom to removed_boms and its Products to removed_products
# Then deletes Bom and Products if successful
# Does not copy Products of Boms with load_error, just deletes them
# Uses Mongo driver directly so we don't lose the file data (fs.files, fs.chunks)
def remove(remover = nil)
removed_at = Time.now.utc
mongofied = self.to_mongo.merge({:remover_id => remover.try(:id), :removed_at => removed_at})
mongofied.delete("_type") # boms get marked with _type: "Bom" since RemovedBom inherits. Yargh
if self.load_error?
if RemovedBom.collection.save(mongofied)
self.destroy_products
self.collection.remove(:_id =>
self.id)
true
else #clean up
RemovedBom.delete_all(:_id =>
self.id)
false
end
else
if RemovedBom.collection.save(mongofied) &&
Product.where(:load_name => load_name).all.collect{|p| RemovedProduct.collection.save(p.to_mongo.merge({:removed_bom_id =>
self.id, :removed_at => removed_at}))}.all?
self.destroy_products
self.collection.remove(:_id =>
self.id)
true
else #clean up
RemovedBom.delete_all(:_id =>
self.id)
RemovedProduct.delete_all(:removed_bom_id =>
self.id)
false
end
end
end