diff --git a/app/assets/javascripts/my_modules/protocols.js b/app/assets/javascripts/my_modules/protocols.js index ee4c41e55..d023c2883 100644 --- a/app/assets/javascripts/my_modules/protocols.js +++ b/app/assets/javascripts/my_modules/protocols.js @@ -528,6 +528,8 @@ function initImport() { if (data.status === "ok") { // Simply reload page location.reload(); + } else if (data.status === 'locked') { + alert(I18n.t("my_modules.protocols.load_from_file_error_locked")); } else { alert(I18n.t("my_modules.protocols.load_from_file_error")); } diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 29705c8ba..3f25ee639 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -313,125 +313,157 @@ class ProtocolsController < ApplicationController def revert respond_to do |format| - transaction_error = false - Protocol.transaction do - begin - # Revert is basically update from parent - @protocol.update_from_parent(current_user) - rescue Exception - transaction_error = true - raise ActiveRecord:: Rollback + if @protocol.can_destroy? + transaction_error = false + Protocol.transaction do + begin + # Revert is basically update from parent + @protocol.update_from_parent(current_user) + rescue Exception + transaction_error = true + raise ActiveRecord:: Rollback + end end - end - if transaction_error - # Bad request error - format.json { - render json: { - message: t("my_modules.protocols.revert_error") - }, - status: :bad_request - } + if transaction_error + # Bad request error + format.json do + render json: { + message: t('my_modules.protocols.revert_error') + }, + status: :bad_request + end + else + # Everything good, display flash & render 200 + flash[:success] = t( + 'my_modules.protocols.revert_flash' + ) + flash.keep(:success) + format.json { render json: {}, status: :ok } + end else - # Everything good, display flash & render 200 - flash[:success] = t( - "my_modules.protocols.revert_flash", - ) - flash.keep(:success) - format.json { render json: {}, status: :ok } + format.json do + render json: { + message: t('my_modules.protocols.revert_error_locked') + }, status: :bad_request + end end end end def update_parent respond_to do |format| - transaction_error = false - Protocol.transaction do - begin - @protocol.update_parent(current_user) - rescue Exception - transaction_error = true - raise ActiveRecord:: Rollback + if @protocol.parent.can_destroy? + transaction_error = false + Protocol.transaction do + begin + @protocol.update_parent(current_user) + rescue Exception + transaction_error = true + raise ActiveRecord:: Rollback + end end - end - if transaction_error - # Bad request error - format.json { - render json: { - message: t("my_modules.protocols.update_parent_error") - }, - status: :bad_request - } + if transaction_error + # Bad request error + format.json { + render json: { + message: t("my_modules.protocols.update_parent_error") + }, + status: :bad_request + } + else + # Everything good, display flash & render 200 + flash[:success] = t( + "my_modules.protocols.update_parent_flash", + ) + flash.keep(:success) + format.json { render json: {}, status: :ok } + end else - # Everything good, display flash & render 200 - flash[:success] = t( - "my_modules.protocols.update_parent_flash", - ) - flash.keep(:success) - format.json { render json: {}, status: :ok } + format.json do + render json: { + message: t('my_modules.protocols.update_parent_error_locked') + }, status: :bad_request + end end end end def update_from_parent respond_to do |format| - transaction_error = false - Protocol.transaction do - begin - @protocol.update_from_parent(current_user) - rescue Exception - transaction_error = true - raise ActiveRecord:: Rollback + if @protocol.can_destroy? + transaction_error = false + Protocol.transaction do + begin + @protocol.update_from_parent(current_user) + rescue Exception + transaction_error = true + raise ActiveRecord:: Rollback + end end - end - if transaction_error - # Bad request error - format.json { - render json: { - message: t("my_modules.protocols.update_from_parent_error") - }, - status: :bad_request - } + if transaction_error + # Bad request error + format.json { + render json: { + message: t("my_modules.protocols.update_from_parent_error") + }, + status: :bad_request + } + else + # Everything good, display flash & render 200 + flash[:success] = t( + "my_modules.protocols.update_from_parent_flash", + ) + flash.keep(:success) + format.json { render json: {}, status: :ok } + end else - # Everything good, display flash & render 200 - flash[:success] = t( - "my_modules.protocols.update_from_parent_flash", - ) - flash.keep(:success) - format.json { render json: {}, status: :ok } + format.json do + render json: { + message: t('my_modules.protocols.update_from_parent_error_locked') + }, status: :bad_request + end end end end def load_from_repository respond_to do |format| - transaction_error = false - Protocol.transaction do - begin - @protocol.load_from_repository(@source, current_user) - rescue Exception - transaction_error = true - raise ActiveRecord:: Rollback + if @protocol.can_destroy? + transaction_error = false + Protocol.transaction do + begin + @protocol.load_from_repository(@source, current_user) + rescue Exception + transaction_error = true + raise ActiveRecord:: Rollback + end end - end - if transaction_error - # Bad request error - format.json { - render json: { - message: t("my_modules.protocols.load_from_repository_error") - }, - status: :bad_request - } + if transaction_error + # Bad request error + format.json { + render json: { + message: t("my_modules.protocols.load_from_repository_error") + }, + status: :bad_request + } + else + # Everything good, display flash & render 200 + flash[:success] = t( + "my_modules.protocols.load_from_repository_flash", + ) + flash.keep(:success) + format.json { render json: {}, status: :ok } + end else - # Everything good, display flash & render 200 - flash[:success] = t( - "my_modules.protocols.load_from_repository_flash", - ) - flash.keep(:success) - format.json { render json: {}, status: :ok } + format.json do + render json: { + message: t('my_modules.protocols.load_from_repository_error_locked') + }, status: :bad_request + end end end end @@ -439,29 +471,35 @@ class ProtocolsController < ApplicationController def load_from_file # This is actually very similar to import respond_to do |format| - transaction_error = false - Protocol.transaction do - begin - import_into_existing(@protocol, @protocol_json, current_user) - rescue Exception - transaction_error = true - raise ActiveRecord:: Rollback + if @protocol.can_destroy? + transaction_error = false + Protocol.transaction do + begin + import_into_existing(@protocol, @protocol_json, current_user) + rescue Exception + transaction_error = true + raise ActiveRecord:: Rollback + end end - end - if transaction_error - format.json { - render json: { status: :error }, status: :bad_request - } + if transaction_error + format.json { + render json: { status: :error }, status: :bad_request + } + else + # Everything good, display flash & render 200 + flash[:success] = t( + "my_modules.protocols.load_from_file_flash", + ) + flash.keep(:success) + format.json { + render json: { status: :ok }, status: :ok + } + end else - # Everything good, display flash & render 200 - flash[:success] = t( - "my_modules.protocols.load_from_file_flash", - ) - flash.keep(:success) - format.json { - render json: { status: :ok }, status: :ok - } + format.json do + render json: { status: :locked }, status: :bad_request + end end end end diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 9fc7230a3..30c302941 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -368,6 +368,8 @@ class Protocol < ActiveRecord::Base end def archive(user) + return nil unless can_destroy? + # Don't update "updated_at" timestamp self.record_timestamps = false @@ -588,6 +590,10 @@ class Protocol < ActiveRecord::Base self.reload end + def can_destroy? + steps.map(&:can_destroy?).all? + end + private def deep_clone(clone, current_user) diff --git a/config/locales/en.yml b/config/locales/en.yml index f1484e517..5586625c1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -545,12 +545,16 @@ en: unlink_error: "Failed to unlink protocol." revert_flash: "Protocol was successfully reverted to protocol version." revert_error: "Failed to revert protocol." + revert_error_locked: "Failed to revert protocol. One or more files in the protocol are currently being edited." update_parent_flash: "Protocol in repository was successfully updated with the version from the task." update_parent_error: "Failed to update repository version of the protocol." + update_parent_error_locked: "Failed to update repository version of the protocol. One or more files in the protocol are currently being edited." update_from_parent_flash: "Version in the repository was successfully updated." update_from_parent_error: "Failed to update the protocol with the version in the repository." + update_from_parent_error_locked: "Failed to update the protocol with the version in the repository. One or more files in the protocol are currently being edited." load_from_repository_flash: "Successfully loaded the protocol from the repository." load_from_repository_error: "Failed to load the protocol from the repository." + load_from_repository_error_locked: "Failed to load the protocol from the repository. One or more files in the protocol are currently being edited." load_from_repository_modal: title: "Load protocol from repository" text: "Choose the protocol to be loaded to the task." @@ -582,6 +586,7 @@ en: confirm: "Copy to repository" load_from_file_flash: "Successfully loaded the protocol from the file." load_from_file_error: "Failed to load the protocol from file." + load_from_file_error_locked: "Failed to load the protocol from file. One or more files are currently being edited." results: head_title: "%{project} | %{module} | Results" add_label: "Add new result:"