scinote-web/app/services/toolbars/reports_service.rb

91 lines
2.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Toolbars
class ReportsService
attr_reader :current_user
include Canaid::Helpers::PermissionsHelper
include Rails.application.routes.url_helpers
def initialize(current_user, report_ids: [])
@current_user = current_user
@reports = Report.where(id: report_ids)
@report = @reports.first if @reports.length == 1
end
def actions
return [] if @reports.none?
[
edit_action,
update_pdf_action,
save_pdf_to_inventory_action,
generate_docx_action,
delete_action
].compact
end
private
def edit_action
return unless @report && can_manage_report?(@report)
{
name: 'edit',
label: I18n.t('projects.reports.index.edit'),
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-edit',
path: edit_project_report_path(@report.project_id, @report.id),
type: :link
}
end
def update_pdf_action
return unless @report && can_manage_report?(@report)
{
name: 'update_pdf',
label: I18n.t('projects.reports.index.update_pdf'),
icon: 'fas fa-file-pdf',
2024-01-11 02:56:01 +08:00
type: :emit
}
end
def save_pdf_to_inventory_action
return unless @report && can_manage_report?(@report)
{
2024-01-11 02:56:01 +08:00
name: 'save_pdf_to_repository',
label: I18n.t('projects.reports.index.save_pdf_to_inventory'),
icon: 'fas fa-save',
2024-01-11 02:56:01 +08:00
type: :emit
}
end
def generate_docx_action
return unless @report && can_manage_report?(@report)
button_id = @report.docx_file_status == 'docx_empty' ? 'requestDocx' : 'updateDocx'
label = @report.docx_file_status == 'docx_empty' ? I18n.t('projects.reports.index.request_docx') : I18n.t('projects.reports.index.update_docx')
{
2024-01-11 02:56:01 +08:00
name: 'update_docx',
label: label,
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-file-word',
2024-01-11 02:56:01 +08:00
type: :emit
}
end
def delete_action
return unless @reports.all? { |report| can_manage_report?(report) }
{
name: 'delete',
label: I18n.t('projects.reports.index.delete'),
2023-06-08 14:33:37 +08:00
icon: 'sn-icon sn-icon-delete',
2024-01-11 02:56:01 +08:00
path: reports_destroy_path,
type: :emit
}
end
end
end