Skip to content

Commit

Permalink
Define the destroy function for studies
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin De Pelseneer committed Aug 21, 2023
1 parent 60df273 commit facb1b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
1 change: 0 additions & 1 deletion app/controllers/assays_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def destroy

raise "You are don't have permission to delete this assay!" unless current_user.can_delete?

# hkvdghvshdbvjh
project_id = @assay.projects&.first&.id

@assay.sample_type.delete if is_single_page_assay?
Expand Down
33 changes: 32 additions & 1 deletion app/controllers/studies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,33 @@ def update
end
end

def destroy
raise 'This assay is not empty. Unable to delete this assay' unless @study.state_allows_delete?

raise "You are don't have permission to delete this assay!" unless current_user.can_delete?

project_id = @study.projects&.first&.id

if is_single_page_assay?
@study.sample_types.each do |st|
raise "Sample Type '#{st.title}' contains samples. Unable to delete study" unless st.samples.empty?
st.delete
end
end
@study.delete

if is_single_page_assay?
flash[:notice] = "ISA Study successfully deleted!"
redirect_to single_page_path(Project.find(project_id))
else
flash[:notice] = "Study successfully deleted!"
redirect_to new_study_path
end
rescue StandardError => e
flash[:error] = "#{e}. Couldn't delete Study!"
render json: { status: :unprocessable_entity, error: e.message }
end

def show
@study = Study.find(params[:id])

Expand Down Expand Up @@ -201,7 +228,7 @@ def batch_create
study_params = {
title: params[:studies][:title][index],
description: params[:studies][:description][index],
investigation_id: params[:study][:investigation_id],
investigation_id: params[:study][:investigation_id],
custom_metadata: CustomMetadata.new(
custom_metadata_type: metadata_types,
data: metadata
Expand Down Expand Up @@ -351,3 +378,7 @@ def study_params
{ custom_metadata_attributes: determine_custom_metadata_keys })
end
end

def is_single_page_assay?
params[:return_to].start_with? '/single_pages/'
end

0 comments on commit facb1b8

Please sign in to comment.