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
|
2023-03-15 23:01:01 +08:00
|
|
|
include InputSanitizeHelper
|
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-04-05 21:08:31 +08:00
|
|
|
:created_at_formatted, :updated_at_formatted, :added_by, :authors, :keywords, :version,
|
|
|
|
:code, :published, :version_comment, :archived, :linked, :has_draft,
|
2023-05-04 19:34:45 +08:00
|
|
|
:published_on_formatted, :published_by, :created_from_version, :assignable_my_module_id
|
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-04-14 15:56:35 +08:00
|
|
|
object.version_number
|
|
|
|
end
|
2023-04-11 21:00:21 +08:00
|
|
|
|
2023-04-14 15:56:35 +08:00
|
|
|
def created_from_version
|
|
|
|
object.previous_version&.version_number
|
2023-01-17 22:44:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def published
|
|
|
|
object.in_repository_published?
|
2023-01-05 18:42:59 +08:00
|
|
|
end
|
|
|
|
|
2023-04-05 21:08:31 +08:00
|
|
|
def published_on_formatted
|
|
|
|
return if object.published_on.blank?
|
|
|
|
|
|
|
|
I18n.l(object.published_on, format: :full)
|
|
|
|
end
|
|
|
|
|
|
|
|
def published_by
|
|
|
|
{
|
|
|
|
avatar: object.published_by&.avatar_url(:icon_small),
|
|
|
|
name: object.published_by&.full_name
|
|
|
|
}
|
|
|
|
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
|
2023-03-15 23:01:01 +08:00
|
|
|
object.protocol_keywords.map { |i| { label: escape_input(i.name), value: escape_input(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,
|
2023-04-11 18:50:14 +08:00
|
|
|
version_comment_url: version_comment_url,
|
2023-03-07 18:25:59 +08:00
|
|
|
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-21 17:59:31 +08:00
|
|
|
# rubocop:disable Naming/PredicateName
|
|
|
|
def has_draft
|
|
|
|
return false unless object.in_repository_published?
|
2023-03-15 18:14:20 +08:00
|
|
|
|
2023-03-21 17:59:31 +08:00
|
|
|
(object.parent || object).draft.present?
|
2023-03-14 16:12:54 +08:00
|
|
|
end
|
2023-03-21 17:59:31 +08:00
|
|
|
# rubocop:enable Naming/PredicateName
|
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
|
|
|
|
2023-04-25 21:12:19 +08:00
|
|
|
def assignable_my_module_id
|
|
|
|
return if in_repository
|
|
|
|
|
|
|
|
object.my_module&.id
|
|
|
|
end
|
|
|
|
|
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
|
2023-04-06 22:29:19 +08:00
|
|
|
return unless can_manage_protocol_in_module?(object) && object.linked? &&
|
2023-04-25 22:00:20 +08:00
|
|
|
object.parent.active? && object.newer_than_parent? &&
|
|
|
|
can_read_protocol_in_repository?(object.parent)
|
2023-02-07 17:39:49 +08:00
|
|
|
|
|
|
|
revert_modal_protocol_path(object, format: :json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_protocol_url
|
2023-04-06 22:29:19 +08:00
|
|
|
return unless can_manage_protocol_in_module?(object) && object.linked? &&
|
2023-04-25 22:00:20 +08:00
|
|
|
object.parent.active? && object.parent_newer? &&
|
|
|
|
can_read_protocol_in_repository?(object.parent)
|
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
|
|
|
|
2023-04-11 18:50:14 +08:00
|
|
|
def version_comment_url
|
|
|
|
return unless can_publish_protocol_in_repository?(object)
|
|
|
|
|
|
|
|
version_comment_protocol_path(object)
|
|
|
|
end
|
|
|
|
|
2023-02-22 21:59:48 +08:00
|
|
|
def save_as_draft_url
|
2023-03-21 17:59:31 +08:00
|
|
|
return unless can_save_protocol_version_as_draft?(object)
|
2023-02-22 21:59:48 +08:00
|
|
|
|
|
|
|
save_as_draft_protocol_path(object)
|
|
|
|
end
|
2022-05-03 21:18:48 +08:00
|
|
|
end
|