From 22a086ebc1e3418cd754a4d07287683cafc7547a Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 5 Jan 2023 14:27:57 +1100 Subject: [PATCH] Remove report dependency on report controller The ReportsController was referencing the report and the report was referencing the controller. It's unlikely that this circular dependency created a memory leak but it's generally a bad design. And we need to make the reporting independent of the controller to isolate it in a background job. --- app/controllers/admin/reports_controller.rb | 2 +- lib/reporting/report_renderer.rb | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb index cfdabcbbd7..af09cfc77c 100644 --- a/app/controllers/admin/reports_controller.rb +++ b/app/controllers/admin/reports_controller.rb @@ -31,7 +31,7 @@ module Admin private def export_report - send_data @report.render_as(report_format, controller: self), filename: report_filename + send_data @report.render_as(report_format), filename: report_filename end def render_report diff --git a/lib/reporting/report_renderer.rb b/lib/reporting/report_renderer.rb index 274bdf95c2..29afa5dd39 100644 --- a/lib/reporting/report_renderer.rb +++ b/lib/reporting/report_renderer.rb @@ -38,30 +38,29 @@ module Reporting @report.rows.map(&:to_h).as_json end - def render_as(target_format, controller: nil) + def render_as(target_format) unless target_format.to_sym.in?(REPORT_FORMATS) raise ActionController::BadRequest, "report_format should be in #{REPORT_FORMATS}" end - public_send("to_#{target_format}", controller) + public_send("to_#{target_format}") end - def to_csv(_context_controller = nil) + def to_csv SpreadsheetArchitect.to_csv(headers: table_headers, data: table_rows) end - def to_xlsx(_context_controller = nil) + def to_xlsx SpreadsheetArchitect.to_xlsx(spreadsheets_options) 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 } - ) + def to_pdf + html = ApplicationController.render( + template: "admin/reports/_table", + layout: "pdf", + locals: { report: @report } ) + WickedPdf.new.pdf_from_string(html) end private