Prevent destroying protocols when file is edited

SCI-552 #close
This commit is contained in:
Jure Grabnar 2016-10-06 16:13:35 +02:00
parent b52e596209
commit 1f67aec258
4 changed files with 160 additions and 109 deletions

View file

@ -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"));
}

View file

@ -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

View file

@ -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)

View file

@ -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:"