Add save as draft action

This commit is contained in:
Anton 2023-02-22 14:59:48 +01:00
parent 5d3e6b7226
commit 826cb162da
12 changed files with 95 additions and 23 deletions
app
assets
javascripts/protocols
stylesheets/protocols
controllers
datatables
javascript/vue/protocol
jobs/user_assignments
models
permissions
serializers
views/protocols
config

View file

@ -223,12 +223,6 @@ var ProtocolsIndex = (function() {
$(row).attr('data-row-id', rowId);
if (data.DT_CanClone) {
$(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 row ID is in the list of selected row IDs
if ($.inArray(rowId, rowsSelected) !== -1) {
$(row).find("input[type='checkbox']").prop('checked', true);

View file

@ -71,6 +71,10 @@
flex-grow: 1;
justify-content: flex-end;
.btn {
margin-left: .25em;
}
.caret {
margin-left: 25px;
}

View file

@ -36,7 +36,6 @@ class ProtocolsController < ApplicationController
update_description
update_version_comment
update_name
destroy_draft
update_authors
edit_name_modal
edit_keywords_modal
@ -75,6 +74,8 @@ class ProtocolsController < ApplicationController
before_action :check_publish_permission, only: :publish
before_action :check_import_permissions, only: :import
before_action :check_export_permissions, only: :export
before_action :check_delete_draft_permissions, only: :destroy_draft
before_action :check_save_as_draft_permissions, only: :save_as_draft
before_action :check_protocolsio_import_permissions,
only: %i(protocolsio_import_create protocolsio_import_save)
@ -119,7 +120,8 @@ class ProtocolsController < ApplicationController
end
def versions_modal
return render_403 unless @protocol.in_repository_published_original?
return render_403 unless @protocol.in_repository_published_original? ||
(@protocol.in_repository_draft? && @protocol.parent_id.blank?)
@published_versions = @protocol.published_versions.order(version_number: :desc)
render json: {
@ -369,6 +371,23 @@ class ProtocolsController < ApplicationController
end
end
def save_as_draft
Protocol.transaction do
draft = @protocol.save_as_draft(current_user)
if draft.invalid?
flash[:error] = draft.errors.full_messages.join(', ')
redirect_to protocols_path
else
redirect_to protocol_path(draft)
end
rescue StandardError => e
Rails.logger.error(e.message)
Rails.logger.error(e.backtrace.join("\n"))
raise ActiveRecord::Rollback
end
end
def unlink
respond_to do |format|
transaction_error = false
@ -1132,7 +1151,7 @@ class ProtocolsController < ApplicationController
end
def check_view_permissions
@protocol = Protocol.find_by_id(params[:id])
@protocol = Protocol.find_by(id: params[:id])
current_team_switch(@protocol.team) if current_team != @protocol.team
unless @protocol.present? &&
(can_read_protocol_in_module?(@protocol) ||
@ -1161,12 +1180,24 @@ class ProtocolsController < ApplicationController
end
def check_manage_permissions
@protocol = Protocol.find_by_id(params[:id])
@protocol = Protocol.find_by(id: params[:id])
render_403 unless @protocol.present? &&
(can_manage_protocol_in_module?(@protocol) ||
can_manage_protocol_in_repository?(@protocol))
end
def check_save_as_draft_permissions
@protocol = Protocol.find_by(id: params[:id])
render_403 unless @protocol.present? &&
can_save_protocol_as_draft_in_repository?(@protocol)
end
def check_delete_draft_permissions
@protocol = Protocol.find_by(id: params[:id])
render_403 unless @protocol.present? &&
can_delete_protocol_draft_in_repository?(@protocol)
end
def check_manage_parent_in_repository_permissions
@protocol = Protocol.find_by_id(params[:id])
render_403 unless @protocol.present? &&

View file

@ -91,13 +91,6 @@ class ProtocolsDatatable < CustomDatatable
DT_RowAttr: {
'data-permissions-url': permissions_protocol_path(record)
},
DT_CanClone: can_clone_protocol_in_repository?(record),
DT_CloneUrl: if can_clone_protocol_in_repository?(record)
clone_protocol_path(record, team: @team, type: @type)
end,
DT_RowAttr: {
'data-permissions-url': permissions_protocol_path(record)
},
'1': name_html(record),
'2': record.code,
'3': versions_html(record),

View file

@ -18,7 +18,7 @@
</a>
<button class="btn btn-light">{{ i18n.t("protocols.header.versions") }}</button>
<button v-if="!protocol.attributes.published" @click="$emit('publish')" class="btn btn-primary">{{ i18n.t("protocols.header.publish") }}</button>
<button v-else class="btn btn-secondary">{{ i18n.t("protocols.header.save_as_draft") }}</button>
<button v-else @click="saveAsdraft" class="btn btn-secondary">{{ i18n.t("protocols.header.save_as_draft") }}</button>
</div>
</div>
<div id="details-container" class="protocol-details collapse in">
@ -91,6 +91,9 @@
},
},
methods: {
saveAsdraft() {
$.post(this.protocol.attributes.urls.save_as_draft_url)
},
updateAuthors(authors) {
$.ajax({
type: 'PATCH',

View file

@ -4,7 +4,7 @@ module UserAssignments
class ProtocolGroupAssignmentJob < ApplicationJob
queue_as :high_priority
def perform(team, protocols, assigned_by)
def perform(team, protocol, assigned_by)
@team = team
@assigned_by = assigned_by
@ -12,7 +12,7 @@ module UserAssignments
team.users.where.not(id: assigned_by).find_each do |user|
user_assignment = UserAssignment.find_or_initialize_by(
user: user,
assignable: protocols
assignable: protocol
)
next if user_assignment.manually_assigned?

View file

@ -244,6 +244,8 @@ class Protocol < ApplicationRecord
end
def draft
return self if in_repository_draft? && parent_id.blank?
return nil unless in_repository_published_original?
team.protocols.in_repository_draft.find_by(parent: self)
@ -559,6 +561,26 @@ class Protocol < ApplicationRecord
clone
end
def save_as_draft(current_user)
version = (parent || self).latest_published_version.version_number + 1
draft = dup
draft.version_number = version
draft.protocol_type = :in_repository_draft
draft.parent = (parent || self)
draft.published_by = nil
draft.published_on = nil
draft.version_comment = nil
return draft if draft.invalid?
ActiveRecord::Base.no_touching do
draft = deep_clone(draft, current_user)
end
draft
end
def deep_clone_my_module(my_module, current_user)
clone = Protocol.new_blank_for_module(my_module)
clone.name = name

View file

@ -117,6 +117,16 @@ Canaid::Permissions.register_for(Protocol) do
protocol.in_repository_draft? &&
protocol.permission_granted?(user, ProtocolPermissions::MANAGE)
end
can :delete_protocol_draft_in_repository do |user, protocol|
protocol.parent_id.present? &&
can_manage_protocol_draft_in_repository?(user, protocol)
end
can :save_protocol_as_draft_in_repository do |user, protocol|
(protocol.in_repository_published_original? || protocol.in_repository_published_version?) &&
can_create_protocols_in_repository?(user, protocol.team)
end
end
Canaid::Permissions.register_for(Report) do

View file

@ -69,7 +69,8 @@ class ProtocolSerializer < ActiveModel::Serializer
update_protocol_authors_url: update_protocol_authors_url,
update_protocol_keywords_url: update_protocol_keywords_url,
delete_steps_url: delete_steps_url,
publish_url: publish_url
publish_url: publish_url,
save_as_draft_url: save_as_draft_url
}
end
@ -169,4 +170,10 @@ class ProtocolSerializer < ActiveModel::Serializer
publish_protocol_path(object)
end
def save_as_draft_url
return unless can_save_protocol_as_draft_in_repository?(object)
save_as_draft_protocol_path(object)
end
end

View file

@ -71,9 +71,15 @@
</div>
<div class="protocol-actions">
<% unless @protocol.draft.present? %>
<button class="btn btn-light icon-btn save-as-draft" data-placement="left" title="<%= t('protocols.index.versions.save_as_draft') %>" data-toggle="tooltip">
<%= button_to save_as_draft_protocol_path(protocol_version),
class: "btn btn-light icon-btn save-as-draft",
title: t('protocols.index.versions.save_as_draft'),
data: {
placement: :left,
toggle: :tooltip
} do %>
<i class="fas fa-clone"></i>
</button>
<% end %>
<% end %>
</div>
<div class="protocol-version-comment">

View file

@ -45,6 +45,7 @@
<!-- Create new office file modal -->
<%= render partial: 'assets/wopi/create_wopi_file_modal.html.erb' %>
<%= render partial: "my_modules/protocols/print_protocol_modal", locals: { protocol: @protocol, comments_enabled: true} %>
<%= javascript_include_tag "handsontable.full" %>
<%= render partial: "shared/formulas_libraries.html.erb" %>

View file

@ -572,6 +572,7 @@ Rails.application.routes.draw do
member do
post :publish
post :destroy_draft
post :save_as_draft
get 'print', to: 'protocols#print'
get 'linked_children', to: 'protocols#linked_children'
post 'linked_children_datatable',