Skip to content

Commit

Permalink
Fix for failing tests and moving logic to a before_action instead of …
Browse files Browse the repository at this point in the history
…custom destroy function.
  • Loading branch information
Kevin De Pelseneer committed Aug 22, 2023
1 parent facb1b8 commit 23afcd5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 40 deletions.
24 changes: 6 additions & 18 deletions app/controllers/assays_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class AssaysController < ApplicationController
before_action :assays_enabled?
before_action :find_assets, :only=>[:index]
before_action :find_and_authorize_requested_item, :only=>[:edit, :update, :destroy, :manage, :manage_update, :show, :new_object_based_on_existing_one]
before_action :delete_linked_sample_types, only: [:destroy]

#project_membership_required_appended is an alias to project_membership_required, but is necessary to include the actions
#defined in the application controller
Expand Down Expand Up @@ -110,26 +111,11 @@ def create
end
end

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

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

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

@assay.sample_type.delete if is_single_page_assay?
@assay.delete

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

def update
Expand Down Expand Up @@ -199,6 +185,8 @@ def assay_params
end

def is_single_page_assay?
return false unless params.key?(:return_to)

params[:return_to].start_with? '/single_pages/'
end
end
31 changes: 9 additions & 22 deletions app/controllers/studies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class StudiesController < ApplicationController
before_action :studies_enabled?
before_action :find_assets, only: [:index]
before_action :find_and_authorize_requested_item, only: %i[edit update destroy manage manage_update show new_object_based_on_existing_one]
before_action :delete_linked_sample_types, only: [:destroy]

# project_membership_required_appended is an alias to project_membership_required, but is necesary to include the actions
# defined in the application controller
Expand Down Expand Up @@ -88,33 +89,17 @@ def update
end
end

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

raise "You are don't have permission to delete this assay!" unless current_user.can_delete?
@study.sample_types.each do |st|
raise "Sample Type '#{st.title}' contains samples. Unable to delete study" unless st.samples.empty?

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
st.delete
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 @@ -380,5 +365,7 @@ def study_params
end

def is_single_page_assay?
return false unless params.key?(:return_to)

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

0 comments on commit 23afcd5

Please sign in to comment.