Merge pull request #302 from tsujigiri/refactor-genotype-deletion

Fix genotype deletion
This commit is contained in:
Philipp Bayer
2016-06-11 17:31:29 +08:00
committed by GitHub
3 changed files with 18 additions and 13 deletions

View File

@@ -51,18 +51,9 @@ class GenotypesController < ApplicationController
end
def destroy
user = current_user
genotype_count = user.genotypes.count
DeleteGenotype.perform_async(genotype_id: params[:id])
genotype = current_user.genotypes.find(params[:id])
DeleteGenotype.perform_async(genotype_id: genotype.id)
flash[:notice] = 'Your Genotyping will be deleted. This may take a few minutes.'
if genotype_count == 1
# update user-attributes
user.update_attributes(has_sequence: false, sequence_link: nil)
# delete Uploaded Genotyping-achievement
achievement = Achievement.where(award: 'Published genotyping')
UserAchievement.where(achievement: achievement, user: user).destroy_all
end
redirect_to current_user
end

View File

@@ -2,7 +2,7 @@ require 'fileutils'
class Genotype < ActiveRecord::Base
belongs_to :user
has_many :user_snps, dependent: :destroy
has_many :user_snps, dependent: :delete_all
validates_presence_of :user
has_attached_file :genotype, url: '/data/:fs_filename',

View File

@@ -3,6 +3,20 @@ class DeleteGenotype
sidekiq_options queue: :user_snps, retry: 5, unique: true
def perform(params)
Genotype.find_by!(id: params['genotype_id']).destroy
Genotype.transaction do
genotype = Genotype.find(params['genotype_id'])
user = genotype.user
if user.genotypes.count == 1
# update user-attributes
user.update_attributes(has_sequence: false, sequence_link: nil)
# delete Uploaded Genotyping-achievement
achievement = Achievement.where(award: 'Published genotyping')
UserAchievement.where(achievement: achievement, user: user).destroy_all
end
genotype.destroy
end
end
end