2022-05-03 21:18:48 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ProtocolSerializer < ActiveModel::Serializer
|
|
|
|
include Canaid::Helpers::PermissionsHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
2022-05-11 22:08:04 +08:00
|
|
|
include ApplicationHelper
|
|
|
|
include ActionView::Helpers::TextHelper
|
2022-05-03 21:18:48 +08:00
|
|
|
|
2022-06-06 19:56:22 +08:00
|
|
|
attributes :name, :id, :urls, :description, :description_view, :updated_at, :in_repository,
|
2023-01-17 22:44:16 +08:00
|
|
|
:created_at_formatted, :updated_at_formatted, :added_by, :authors, :keywords, :version, :code,
|
2023-03-14 21:07:52 +08:00
|
|
|
:published, :version_comment, :archived, :linked, :disabled_drafting
|
2022-05-11 22:08:04 +08:00
|
|
|
|
|
|
|
def updated_at
|
|
|
|
object.updated_at.to_i
|
|
|
|
end
|
|
|
|
|
2023-01-05 18:42:59 +08:00
|
|
|
def version
|
2023-01-17 22:44:16 +08:00
|
|
|
object.in_repository_draft? ? I18n.t('protocols.draft') : object.version_number
|
|
|
|
end
|
|
|
|
|
|
|
|
def published
|
|
|
|
object.in_repository_published?
|
2023-01-05 18:42:59 +08:00
|
|
|
end
|
|
|
|
|
2022-06-06 19:56:22 +08:00
|
|
|
def added_by
|
|
|
|
{
|
2022-06-07 18:10:10 +08:00
|
|
|
avatar: object.added_by&.avatar_url(:icon_small),
|
|
|
|
name: object.added_by&.full_name
|
2022-06-06 19:56:22 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_at_formatted
|
|
|
|
I18n.l(object.created_at, format: :full)
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated_at_formatted
|
|
|
|
I18n.l(object.updated_at, format: :full)
|
|
|
|
end
|
|
|
|
|
|
|
|
def keywords
|
2022-06-07 16:02:10 +08:00
|
|
|
object.protocol_keywords.map { |i| { label: i.name, value: i.name } }
|
2022-06-06 19:56:22 +08:00
|
|
|
end
|
|
|
|
|
2023-02-28 00:18:12 +08:00
|
|
|
def code
|
|
|
|
object&.parent&.code || object.code
|
|
|
|
end
|
|
|
|
|
2022-05-11 22:08:04 +08:00
|
|
|
def description_view
|
|
|
|
@user = @instance_options[:user]
|
|
|
|
custom_auto_link(object.tinymce_render('description'),
|
|
|
|
simple_format: false,
|
|
|
|
tags: %w(img),
|
|
|
|
team: object.team)
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
sanitize_input(object.tinymce_render('description'))
|
|
|
|
end
|
2022-05-03 21:18:48 +08:00
|
|
|
|
|
|
|
def urls
|
|
|
|
{
|
|
|
|
load_from_repo_url: load_from_repo_url,
|
|
|
|
save_to_repo_url: save_to_repo_url,
|
|
|
|
export_url: export_url,
|
2023-02-07 17:39:49 +08:00
|
|
|
unlink_url: unlink_url,
|
|
|
|
revert_protocol_url: revert_protocol_url,
|
|
|
|
update_protocol_url: update_protocol_url,
|
2022-06-03 17:52:10 +08:00
|
|
|
steps_url: steps_url,
|
|
|
|
reorder_steps_url: reorder_steps_url,
|
|
|
|
add_step_url: add_step_url,
|
2022-06-06 19:56:22 +08:00
|
|
|
update_protocol_name_url: update_protocol_name_url,
|
|
|
|
update_protocol_description_url: update_protocol_description_url,
|
|
|
|
update_protocol_authors_url: update_protocol_authors_url,
|
2022-06-24 17:21:47 +08:00
|
|
|
update_protocol_keywords_url: update_protocol_keywords_url,
|
2023-02-13 16:50:39 +08:00
|
|
|
delete_steps_url: delete_steps_url,
|
2023-02-22 21:59:48 +08:00
|
|
|
publish_url: publish_url,
|
2023-02-24 23:17:36 +08:00
|
|
|
save_as_draft_url: save_as_draft_url,
|
2023-03-07 18:25:59 +08:00
|
|
|
versions_modal_url: versions_modal_url,
|
|
|
|
print_protocol_url: print_protocol_url
|
2022-05-03 21:18:48 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-05-27 18:49:10 +08:00
|
|
|
def in_repository
|
|
|
|
!object.in_module?
|
|
|
|
end
|
|
|
|
|
2023-03-14 16:12:54 +08:00
|
|
|
def disabled_drafting
|
2023-03-15 18:14:20 +08:00
|
|
|
return false unless can_create_protocols_in_repository?(object.team)
|
|
|
|
|
|
|
|
%(in_repository_published_original in_repository_published_version).include?(object.protocol_type) &&
|
|
|
|
(object.parent || object).draft.present? && !object.archived
|
2023-03-14 16:12:54 +08:00
|
|
|
end
|
2023-03-15 18:14:20 +08:00
|
|
|
|
2023-03-14 20:03:37 +08:00
|
|
|
def linked
|
|
|
|
object.linked?
|
|
|
|
end
|
2023-03-14 16:12:54 +08:00
|
|
|
|
2022-05-03 21:18:48 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_from_repo_url
|
|
|
|
return unless can_manage_protocol_in_module?(object)
|
|
|
|
|
|
|
|
load_from_repository_modal_protocol_path(object, format: :json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_to_repo_url
|
|
|
|
return unless can_read_protocol_in_module?(object) && can_create_protocols_in_repository?(object.team)
|
|
|
|
|
|
|
|
copy_to_repository_modal_protocol_path(object, format: :json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_url
|
|
|
|
return unless can_read_protocol_in_module?(object)
|
|
|
|
|
|
|
|
export_protocols_path(protocol_ids: object.id, my_module_id: object.my_module.id)
|
|
|
|
end
|
2022-06-03 17:52:10 +08:00
|
|
|
|
|
|
|
def steps_url
|
|
|
|
return unless can_read_protocol_in_module?(object) || can_read_protocol_in_repository?(object)
|
|
|
|
|
|
|
|
steps_path(protocol_id: object.id)
|
|
|
|
end
|
|
|
|
|
2023-02-24 23:17:36 +08:00
|
|
|
def versions_modal_url
|
|
|
|
return unless can_read_protocol_in_repository?(object)
|
|
|
|
|
|
|
|
versions_modal_protocol_path(object.parent || object)
|
|
|
|
end
|
|
|
|
|
2023-03-07 18:25:59 +08:00
|
|
|
def print_protocol_url
|
|
|
|
return unless can_read_protocol_in_repository?(object)
|
|
|
|
|
|
|
|
print_protocol_path(object)
|
|
|
|
end
|
|
|
|
|
2022-06-03 17:52:10 +08:00
|
|
|
def reorder_steps_url
|
2023-03-14 16:12:54 +08:00
|
|
|
return unless can_manage_protocol_in_module?(object) || can_manage_protocol_draft_in_repository?(object)
|
2022-06-03 17:52:10 +08:00
|
|
|
|
|
|
|
reorder_protocol_steps_url(protocol_id: object.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_step_url
|
2023-03-14 16:12:54 +08:00
|
|
|
return unless can_manage_protocol_in_module?(object) || can_manage_protocol_draft_in_repository?(object)
|
2022-06-03 17:52:10 +08:00
|
|
|
|
|
|
|
protocol_steps_path(protocol_id: object.id)
|
|
|
|
end
|
|
|
|
|
2023-02-07 17:39:49 +08:00
|
|
|
def unlink_url
|
|
|
|
return unless can_manage_protocol_in_module?(object) && object.linked?
|
|
|
|
|
|
|
|
unlink_modal_protocol_path(object, format: :json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert_protocol_url
|
|
|
|
return unless can_read_protocol_in_module?(object) && object.linked? && object.newer_than_parent?
|
|
|
|
|
|
|
|
revert_modal_protocol_path(object, format: :json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_protocol_url
|
2023-03-07 20:03:01 +08:00
|
|
|
return unless can_read_protocol_in_module?(object) && object.linked? && object.parent_newer?
|
2023-02-07 17:39:49 +08:00
|
|
|
|
|
|
|
update_from_parent_modal_protocol_path(object, format: :json)
|
|
|
|
end
|
|
|
|
|
2022-06-06 19:56:22 +08:00
|
|
|
def update_protocol_name_url
|
2023-03-14 16:12:54 +08:00
|
|
|
if in_repository && can_manage_protocol_draft_in_repository?(object)
|
2022-06-06 19:56:22 +08:00
|
|
|
name_protocol_path(object)
|
|
|
|
elsif can_manage_protocol_in_module?(object)
|
|
|
|
protocol_my_module_path(object.my_module)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_protocol_description_url
|
2023-03-14 16:12:54 +08:00
|
|
|
if in_repository && can_manage_protocol_draft_in_repository?(object)
|
2022-06-06 19:56:22 +08:00
|
|
|
description_protocol_path(object)
|
2022-06-03 21:26:16 +08:00
|
|
|
elsif can_manage_protocol_in_module?(object)
|
|
|
|
protocol_my_module_path(object.my_module)
|
2022-06-03 17:52:10 +08:00
|
|
|
end
|
|
|
|
end
|
2022-06-06 19:56:22 +08:00
|
|
|
|
|
|
|
def update_protocol_authors_url
|
2023-03-14 16:12:54 +08:00
|
|
|
authors_protocol_path(object) if in_repository && can_manage_protocol_draft_in_repository?(object)
|
2022-06-06 19:56:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_protocol_keywords_url
|
2023-03-14 16:12:54 +08:00
|
|
|
keywords_protocol_path(object) if in_repository && can_manage_protocol_draft_in_repository?(object)
|
2022-06-06 19:56:22 +08:00
|
|
|
end
|
2022-06-24 17:21:47 +08:00
|
|
|
|
|
|
|
def delete_steps_url
|
2023-03-14 16:12:54 +08:00
|
|
|
return unless can_manage_protocol_in_module?(object) || can_manage_protocol_draft_in_repository?(object)
|
2022-06-24 17:21:47 +08:00
|
|
|
|
|
|
|
delete_steps_protocol_path(object)
|
|
|
|
end
|
2023-02-13 16:50:39 +08:00
|
|
|
|
|
|
|
def publish_url
|
|
|
|
return unless can_publish_protocol_in_repository?(object)
|
|
|
|
|
|
|
|
publish_protocol_path(object)
|
|
|
|
end
|
2023-02-22 21:59:48 +08:00
|
|
|
|
|
|
|
def save_as_draft_url
|
|
|
|
return unless can_save_protocol_as_draft_in_repository?(object)
|
|
|
|
|
|
|
|
save_as_draft_protocol_path(object)
|
|
|
|
end
|
2022-05-03 21:18:48 +08:00
|
|
|
end
|