2018-04-18 22:47:52 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ReportDatatable < CustomDatatable
|
2018-07-10 16:58:39 +08:00
|
|
|
include InputSanitizeHelper
|
2021-03-17 19:54:25 +08:00
|
|
|
include Rails.application.routes.url_helpers
|
2018-07-10 16:58:39 +08:00
|
|
|
|
2018-04-18 22:47:52 +08:00
|
|
|
TABLE_COLUMNS = %w(
|
2018-12-13 18:18:09 +08:00
|
|
|
Report.project_name
|
|
|
|
Report.name
|
2022-12-02 18:41:32 +08:00
|
|
|
Report.code
|
2021-03-17 19:54:25 +08:00
|
|
|
Report.pdf_file
|
|
|
|
Report.docx_file
|
2022-10-13 20:55:24 +08:00
|
|
|
Report.created_by_name
|
|
|
|
Report.modified_by_name
|
2018-12-13 18:18:09 +08:00
|
|
|
Report.created_at
|
|
|
|
Report.updated_at
|
2018-04-18 22:47:52 +08:00
|
|
|
).freeze
|
|
|
|
|
|
|
|
def_delegator :@view, :edit_project_report_path
|
|
|
|
def initialize(view, user, reports)
|
|
|
|
super(view)
|
|
|
|
@user = user
|
|
|
|
@reports = reports
|
|
|
|
end
|
|
|
|
|
|
|
|
def sortable_columns
|
|
|
|
@sortable_columns ||= TABLE_COLUMNS
|
|
|
|
end
|
|
|
|
|
|
|
|
def searchable_columns
|
|
|
|
@searchable_columns ||= TABLE_COLUMNS
|
|
|
|
end
|
|
|
|
|
2021-03-17 19:54:25 +08:00
|
|
|
def sort_records(records)
|
|
|
|
case sort_column(order_params)
|
|
|
|
when 'reports.docx_file'
|
|
|
|
records.left_joins(:docx_file_attachment)
|
|
|
|
.order(active_storage_attachments: sort_direction(order_params))
|
2021-05-11 19:28:28 +08:00
|
|
|
.order(docx_file_status: sort_direction(order_params) == 'ASC' ? :desc : :asc)
|
2021-03-17 19:54:25 +08:00
|
|
|
when 'reports.pdf_file'
|
|
|
|
records.left_joins(:pdf_file_attachment)
|
|
|
|
.order(active_storage_attachments: sort_direction(order_params))
|
2021-05-11 19:28:28 +08:00
|
|
|
.order(pdf_file_status: sort_direction(order_params) == 'ASC' ? :desc : :asc)
|
2022-12-05 17:17:15 +08:00
|
|
|
when 'reports.code'
|
|
|
|
sort_by = "reports.id #{sort_direction(order_params)}"
|
|
|
|
records.order(sort_by)
|
2021-03-17 19:54:25 +08:00
|
|
|
else
|
|
|
|
sort_by = "#{sort_column(order_params)} #{sort_direction(order_params)}"
|
|
|
|
records.order(sort_by)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-18 22:47:52 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def data
|
|
|
|
records.map do |record|
|
|
|
|
{
|
2018-12-13 18:18:09 +08:00
|
|
|
'0' => record.id,
|
2023-03-14 01:00:30 +08:00
|
|
|
'1' => escape_input(record.project_name),
|
|
|
|
'2' => escape_input(record.name),
|
|
|
|
'3' => escape_input(record.code),
|
2022-12-02 18:41:32 +08:00
|
|
|
'4' => pdf_file(record),
|
|
|
|
'5' => docx_file(record),
|
2023-03-14 01:00:30 +08:00
|
|
|
'6' => escape_input(record.created_by_name),
|
|
|
|
'7' => escape_input(record.modified_by_name),
|
2022-12-02 18:41:32 +08:00
|
|
|
'8' => I18n.l(record.created_at, format: :full),
|
|
|
|
'9' => I18n.l(record.updated_at, format: :full),
|
2021-05-28 22:17:20 +08:00
|
|
|
'archived' => record.project.archived?,
|
2021-04-14 20:40:13 +08:00
|
|
|
'edit' => edit_project_report_path(record.project_id, record.id),
|
2021-04-14 21:45:51 +08:00
|
|
|
'status' => status_project_report_path(record.project_id, record.id),
|
|
|
|
'generate_pdf' => generate_pdf_project_report_path(record.project_id, record.id),
|
2021-04-13 22:36:52 +08:00
|
|
|
'generate_docx' => generate_docx_project_report_path(record.project_id, record.id),
|
|
|
|
'save_to_inventory' => save_pdf_to_inventory_modal_report_path(record.id)
|
2018-04-18 22:47:52 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-17 19:54:25 +08:00
|
|
|
def docx_file(report)
|
2021-03-23 22:20:40 +08:00
|
|
|
docx = document_preview_report_path(report, report_type: :docx) if report.docx_file.attached?
|
2021-03-17 19:54:25 +08:00
|
|
|
{
|
2021-05-11 19:28:28 +08:00
|
|
|
processing: report.docx_processing?,
|
2021-03-23 22:20:40 +08:00
|
|
|
preview_url: docx,
|
2021-05-11 19:28:28 +08:00
|
|
|
error: report.docx_error?
|
2021-03-17 19:54:25 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def pdf_file(report)
|
2021-03-23 22:20:40 +08:00
|
|
|
pdf = document_preview_report_path(report, report_type: :pdf) if report.pdf_file.attached?
|
2021-03-17 19:54:25 +08:00
|
|
|
{
|
2021-05-11 19:28:28 +08:00
|
|
|
processing: report.pdf_processing?,
|
2021-03-23 22:20:40 +08:00
|
|
|
preview_url: pdf,
|
2021-05-11 19:28:28 +08:00
|
|
|
error: report.pdf_error?
|
2021-03-17 19:54:25 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-04-18 22:47:52 +08:00
|
|
|
def get_raw_records
|
2018-12-13 18:18:09 +08:00
|
|
|
res = @reports.joins(:project)
|
|
|
|
.joins(
|
|
|
|
'LEFT OUTER JOIN users AS creators ' \
|
|
|
|
'ON reports.user_id = creators.id'
|
|
|
|
).joins(
|
|
|
|
'LEFT OUTER JOIN users AS modifiers '\
|
|
|
|
'ON reports.last_modified_by_id = modifiers.id'
|
|
|
|
)
|
|
|
|
.select('reports.* AS reports')
|
|
|
|
.select('projects.name AS project_name')
|
2022-10-13 20:55:24 +08:00
|
|
|
.select('creators.full_name AS created_by_name')
|
|
|
|
.select('modifiers.full_name AS modified_by_name')
|
2018-12-13 18:18:09 +08:00
|
|
|
Report.from(res, :reports)
|
2018-04-18 22:47:52 +08:00
|
|
|
end
|
|
|
|
|
2021-05-14 03:41:26 +08:00
|
|
|
def filter_records(records)
|
2021-05-17 15:55:59 +08:00
|
|
|
records.where_attributes_like(
|
2022-12-02 18:41:32 +08:00
|
|
|
['project_name', 'reports.name', 'reports.description', "('RP' || reports.id)"],
|
2021-05-17 15:55:59 +08:00
|
|
|
dt_params.dig(:search, :value)
|
|
|
|
)
|
2021-05-14 03:41:26 +08:00
|
|
|
end
|
|
|
|
|
2018-04-18 22:47:52 +08:00
|
|
|
# ==== Insert 'presenter'-like methods below if necessary
|
|
|
|
end
|