mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-09 23:06:06 +00:00
Easily group and create header and summary row Auto format cells when appropriate type (boolean, dates) and render_format (neither csv nor json)
61 lines
1.4 KiB
Ruby
61 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'spreadsheet_architect'
|
|
|
|
module Reporting
|
|
class ReportRenderer
|
|
def initialize(report)
|
|
@report = report
|
|
end
|
|
|
|
def table_headers
|
|
@report.table_headers || []
|
|
end
|
|
|
|
def table_rows
|
|
@report.table_rows || []
|
|
end
|
|
|
|
def as_json
|
|
# columns methods give the headers code, but as not reports are implementing it
|
|
# we fallback with the translated headers with table_headers
|
|
headers = begin
|
|
@report.columns.keys
|
|
rescue NotImplementedError, NoMethodError
|
|
table_headers
|
|
end
|
|
table_rows.map do |row|
|
|
result = {}
|
|
headers.zip(row) { |a, b| result[a.to_sym] = b }
|
|
result
|
|
end.as_json
|
|
end
|
|
|
|
def as_arrays
|
|
@as_arrays ||= [table_headers] + table_rows
|
|
end
|
|
|
|
def to_csv(_context_controller = nil)
|
|
SpreadsheetArchitect.to_csv(headers: table_headers, data: table_rows)
|
|
end
|
|
|
|
def to_ods(_context_controller = nil)
|
|
SpreadsheetArchitect.to_ods(headers: table_headers, data: table_rows)
|
|
end
|
|
|
|
def to_xlsx(_context_controller = nil)
|
|
SpreadsheetArchitect.to_xlsx(headers: table_headers, data: table_rows)
|
|
end
|
|
|
|
def to_pdf(context_controller)
|
|
WickedPdf.new.pdf_from_string(
|
|
context_controller.render_to_string(
|
|
template: 'admin/reports/_table',
|
|
layout: 'pdf',
|
|
locals: { report: @report }
|
|
)
|
|
)
|
|
end
|
|
end
|
|
end
|