mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-15 09:34:29 +08:00
Update protocol duplication [SCI-7900] (#4992)
This commit is contained in:
parent
9a57071d7a
commit
2c079cc918
6 changed files with 62 additions and 41 deletions
|
@ -209,14 +209,10 @@ var ProtocolsIndex = (function() {
|
|||
$(row).attr('data-row-id', rowId);
|
||||
|
||||
if (data.DT_CanClone) {
|
||||
$(row).attr('data-can-clone', 'true');
|
||||
$(row).attr('data-clone-url', data.DT_CloneUrl);
|
||||
}
|
||||
if (data.DT_CanMakePrivate) { $(row).attr('data-can-make-private', 'true'); }
|
||||
if (data.DT_CanPublish) { $(row).attr('data-can-publish', 'true'); }
|
||||
if (data.DT_CanArchive) { $(row).attr('data-can-archive', 'true'); }
|
||||
if (data.DT_CanRestore) { $(row).attr('data-can-restore', 'true'); }
|
||||
if (data.DT_CanExport) { $(row).attr('data-can-export', 'true'); }
|
||||
|
||||
// If row ID is in the list of selected row IDs
|
||||
if ($.inArray(rowId, rowsSelected) !== -1) {
|
||||
|
@ -477,6 +473,26 @@ var ProtocolsIndex = (function() {
|
|||
}
|
||||
}
|
||||
|
||||
function initDuplicateProtocols() {
|
||||
$('.protocols-index').on('click', '#duplicateProtocol', function() {
|
||||
duplicateProtocols(rowsSelected[0]);
|
||||
});
|
||||
}
|
||||
|
||||
function duplicateProtocols(id) {
|
||||
var row = $("tr[data-row-id='" + id + "']");
|
||||
animateSpinner();
|
||||
|
||||
$.post(row.attr('data-clone-url'), (data) => {
|
||||
animateSpinner(null, false);
|
||||
HelperModule.flashAlertMsg(data.message, 'success');
|
||||
reloadTable();
|
||||
}).error((data) => {
|
||||
animateSpinner(null, false);
|
||||
HelperModule.flashAlertMsg(data.responseJSON.message, 'danger');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function initLinkedChildrenModal() {
|
||||
// Only do this if the repository is public/private
|
||||
|
@ -990,6 +1006,7 @@ var ProtocolsIndex = (function() {
|
|||
initArchiveProtocols();
|
||||
initRestoreProtocols();
|
||||
initExportProtocols();
|
||||
initDuplicateProtocols();
|
||||
initdeleteDraftModal();
|
||||
|
||||
return {
|
||||
|
|
|
@ -316,28 +316,19 @@ class ProtocolsController < ApplicationController
|
|||
def clone
|
||||
cloned = nil
|
||||
Protocol.transaction do
|
||||
begin
|
||||
cloned = @original.deep_clone_repository(current_user)
|
||||
rescue Exception
|
||||
raise ActiveRecord:: Rollback
|
||||
end
|
||||
cloned = @original.deep_clone_repository(current_user)
|
||||
rescue StandardError
|
||||
raise ActiveRecord::Rollback
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
if !cloned.nil?
|
||||
flash[:success] = t(
|
||||
'protocols.index.clone.success_flash',
|
||||
original: @original.name,
|
||||
new: cloned.name
|
||||
)
|
||||
flash.keep(:success)
|
||||
format.json { render json: {}, status: :ok }
|
||||
else
|
||||
flash[:error] = t(
|
||||
'protocols.index.clone.error_flash',
|
||||
original: @original.name
|
||||
)
|
||||
flash.keep(:error)
|
||||
format.json { render json: {}, status: :bad_request }
|
||||
format.json do
|
||||
if cloned.present?
|
||||
render json: { message: t('protocols.index.clone.success_flash', original: @original.name, new: cloned.name) }
|
||||
else
|
||||
render json: { message: t('protocols.index.clone.error_flash', original: @original.name) },
|
||||
status: :bad_request
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -803,7 +794,7 @@ class ProtocolsController < ApplicationController
|
|||
|
||||
# Create folder and xml file for each protocol and populate it
|
||||
@protocols.each do |protocol|
|
||||
protocol = protocol.published_versions.order(version_number: :desc).first || protocol
|
||||
protocol = protocol.latest_published_version || protocol
|
||||
protocol_dir = get_guid(protocol.id).to_s
|
||||
ostream.put_next_entry("#{protocol_dir}/eln.xml")
|
||||
ostream.print(generate_protocol_xml(protocol))
|
||||
|
@ -1158,7 +1149,8 @@ class ProtocolsController < ApplicationController
|
|||
|
||||
def check_clone_permissions
|
||||
load_team_and_type
|
||||
@original = Protocol.find_by_id(params[:id])
|
||||
protocol = Protocol.find_by(id: params[:id])
|
||||
@original = protocol.latest_published_version || protocol
|
||||
|
||||
if @original.blank? ||
|
||||
!can_clone_protocol_in_repository?(@original) || @type == :archive
|
||||
|
@ -1224,7 +1216,7 @@ class ProtocolsController < ApplicationController
|
|||
|
||||
def check_load_from_repository_permissions
|
||||
@protocol = Protocol.find_by(id: params[:id])
|
||||
@source = Protocol.find_by(id: params[:source_id])&.published_versions&.order(version_number: :desc)&.first
|
||||
@source = Protocol.find_by(id: params[:source_id])&.latest_published_version
|
||||
|
||||
render_403 unless @protocol.present? && @source.present? &&
|
||||
(can_manage_protocol_in_module?(@protocol) &&
|
||||
|
|
|
@ -46,6 +46,14 @@ module Assignable
|
|||
User.joins(:user_assignments).where(user_assignments: { assignable: self })
|
||||
end
|
||||
|
||||
def deep_clone_user_assginments(object)
|
||||
user_assignments.each do |original_assignment|
|
||||
new_assignment = original_assignment.dup
|
||||
new_assignment.assignable = object
|
||||
new_assignment.save!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_users_assignments
|
||||
|
|
|
@ -257,6 +257,10 @@ class Protocol < ApplicationRecord
|
|||
.or(team.protocols.in_repository_published_original.where(id: id))
|
||||
end
|
||||
|
||||
def latest_published_version
|
||||
published_versions.order(version_number: :desc).first
|
||||
end
|
||||
|
||||
def permission_parent
|
||||
in_module? ? my_module : team
|
||||
end
|
||||
|
@ -608,25 +612,25 @@ class Protocol < ApplicationRecord
|
|||
description: description,
|
||||
added_by: current_user,
|
||||
team: team,
|
||||
protocol_type: protocol_type,
|
||||
published_on: in_repository_public? ? Time.now : nil
|
||||
protocol_type: :in_repository_draft,
|
||||
skip_user_assignments: true
|
||||
)
|
||||
|
||||
cloned = deep_clone(clone, current_user)
|
||||
|
||||
if cloned
|
||||
deep_clone_user_assginments(clone)
|
||||
Activities::CreateActivityService
|
||||
.call(activity_type: :copy_protocol_in_repository,
|
||||
owner: current_user,
|
||||
subject: self,
|
||||
team: team,
|
||||
project: nil,
|
||||
message_items: {
|
||||
protocol_new: clone.id,
|
||||
protocol_original: id
|
||||
})
|
||||
owner: current_user,
|
||||
subject: self,
|
||||
team: team,
|
||||
project: nil,
|
||||
message_items: {
|
||||
protocol_new: clone.id,
|
||||
protocol_original: id
|
||||
})
|
||||
end
|
||||
|
||||
cloned
|
||||
end
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<i class="fas fa-door-open"></i>
|
||||
<span class="button-text"><%= t("protocols.index.action_toolbar.access") %></span>
|
||||
</button>
|
||||
<button id="duplicateProtocol" class="btn btn-light multiple-object-action hidden only-active" data-for="copyable">
|
||||
<button id="duplicateProtocol" class="btn btn-light single-object-action hidden only-active" data-for="copyable">
|
||||
<i class="fas fa-copy"></i>
|
||||
<span class="button-text"><%= t("protocols.index.action_toolbar.duplicate") %></span>
|
||||
</button>
|
||||
|
|
|
@ -2758,8 +2758,8 @@ en:
|
|||
message_private: "When you create a new My protocol, it will only be visible to you."
|
||||
submit: "Create"
|
||||
clone:
|
||||
success_flash: "Successfully copied protocol '%{new}' from protocol '%{original}'."
|
||||
error_flash: "Failed to copied protocol '%{original}'."
|
||||
success_flash: "Successfully duplicated protocol template '%{new}' from '%{original}'."
|
||||
error_flash: "Failed to duplicated protocol template '%{original}'."
|
||||
make_private_results:
|
||||
title: "Move to My protocols results"
|
||||
message_failed: "Failed to move %{nr} protocols to My protocols."
|
||||
|
|
Loading…
Add table
Reference in a new issue