diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index b0d46a564..aa658bd7d 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -227,29 +227,38 @@ class StepsController < ApplicationController end def destroy - # Update position on other steps of this module - @protocol.steps.where("position > ?", @step.position).each do |step| - step.position = step.position - 1 - step.save + if @step.can_destroy? + # Update position on other steps of this module + @protocol.steps.where('position > ?', @step.position).each do |step| + step.position = step.position - 1 + step.save + end + + # Calculate space taken by this step + org = @protocol.organization + previous_size = @step.space_taken + + # Destroy the step + @step.destroy(current_user) + + # Release space taken by the step + org.release_space(previous_size) + org.save + + # Update protocol timestamp + update_protocol_ts(@step) + + flash[:success] = t( + 'protocols.steps.destroy.success_flash', + step: (@step.position + 1).to_s + ) + else + flash[:error] = t( + 'protocols.steps.destroy.error_flash', + step: (@step.position + 1).to_s + ) end - # Calculate space taken by this step - org = @protocol.organization - previous_size = @step.space_taken - - # Destroy the step - @step.destroy(current_user) - - # Release space taken by the step - org.release_space(previous_size) - org.save - - # Update protocol timestamp - update_protocol_ts(@step) - - flash[:success] = t( - "protocols.steps.destroy.success_flash", - step: (@step.position + 1).to_s) if @protocol.in_module? redirect_to protocols_my_module_path(@step.my_module) else diff --git a/app/models/step.rb b/app/models/step.rb index 21323b6df..2ade330ae 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -82,6 +82,10 @@ class Step < ActiveRecord::Base super() end + def can_destroy? + !assets.map(&:locked?).any? + end + def my_module protocol.present? ? protocol.my_module : nil end diff --git a/app/views/steps/_step.html.erb b/app/views/steps/_step.html.erb index 2dc3a5e68..07d5ce2e1 100644 --- a/app/views/steps/_step.html.erb +++ b/app/views/steps/_step.html.erb @@ -16,7 +16,7 @@ <% end %> - <% if can_delete_step_in_protocol(@protocol) %> + <% if can_delete_step_in_protocol(@protocol) && step.can_destroy? %> <%= link_to(step_path(step), title: t("protocols.steps.options.delete_title"), method: "delete", class: "btn btn-link", data: {action: "delete-step", confirm: t("protocols.steps.destroy.confirm", step: step.name)}) do %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 5eaf22013..da60b9203 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1444,6 +1444,7 @@ en: destroy: confirm: "Are you sure you want to delete step %{step}?" success_flash: "Step %{step} successfully deleted." + error_flash: "Step %{step} couldn't be deleted. One or more files are locked." edit: head_title: "Edit protocol" no_keywords: "No keywords"