mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add report name and details to CSV files
This commit is contained in:
committed by
Maikel Linke
parent
f5a9ec7fa9
commit
0a9eb173ea
78
lib/reporting/report_metadata_builder.rb
Normal file
78
lib/reporting/report_metadata_builder.rb
Normal file
@@ -0,0 +1,78 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Reporting
|
||||
class ReportMetadataBuilder
|
||||
attr_reader :report, :current_user
|
||||
|
||||
def initialize(report, current_user = nil)
|
||||
@report = report
|
||||
@current_user = current_user
|
||||
end
|
||||
|
||||
def rows
|
||||
rows = []
|
||||
rows.concat(title_rows)
|
||||
rows.concat(date_range_rows)
|
||||
rows.concat(printed_rows)
|
||||
rows.concat(other_filter_rows)
|
||||
rows << [] if rows.any? # spacer only if something was added
|
||||
rows
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
DATE_FROM_KEYS = %i[completed_at_gt created_at_gt updated_at_gt].freeze
|
||||
DATE_TO_KEYS = %i[completed_at_lt created_at_lt updated_at_lt].freeze
|
||||
|
||||
def title_rows
|
||||
type = params[:report_type]
|
||||
sub = params[:report_subtype]
|
||||
return [] if type.blank?
|
||||
|
||||
label = I18n.t("admin.reports.metadata.report_title", default: "Report Title")
|
||||
type_name = I18n.t("admin.reports.#{type}.name",
|
||||
default: I18n.t("admin.reports.#{type}",
|
||||
default: type.to_s.tr('_', ' ').titleize))
|
||||
|
||||
sub_name = sub.present? ? sub.to_s.tr('_', ' ').titleize : nil
|
||||
|
||||
title = [type_name, sub_name].compact.join(' - ')
|
||||
[[label, title]]
|
||||
end
|
||||
|
||||
def date_range_rows
|
||||
q = indifferent_ransack
|
||||
from = first_present(q, DATE_FROM_KEYS)
|
||||
to = first_present(q, DATE_TO_KEYS)
|
||||
return [] unless from || to
|
||||
|
||||
label = I18n.t("date_range", default: "Date Range")
|
||||
[[label, [from, to].compact.join(' - ')]] # en dash
|
||||
end
|
||||
|
||||
def first_present(hash, keys)
|
||||
keys.map { |k| hash[k] }.find(&:present?)
|
||||
end
|
||||
|
||||
def indifferent_ransack
|
||||
(report.ransack_params || {}).with_indifferent_access
|
||||
end
|
||||
|
||||
def printed_rows
|
||||
[[I18n.t("printed", default: "Printed"), Time.now.utc.strftime('%F %T %Z')]]
|
||||
end
|
||||
|
||||
def other_filter_rows
|
||||
q = indifferent_ransack.except(*DATE_FROM_KEYS, *DATE_TO_KEYS)
|
||||
q.each_with_object([]) do |(k, v), rows|
|
||||
next if v.blank?
|
||||
|
||||
rows << [k.to_s.humanize, v.is_a?(Array) ? v.join(', ') : v.to_s]
|
||||
end
|
||||
end
|
||||
|
||||
def params
|
||||
(report.params || {}).with_indifferent_access
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'csv'
|
||||
require 'spreadsheet_architect'
|
||||
|
||||
module Reporting
|
||||
@@ -24,6 +25,10 @@ module Reporting
|
||||
@report.params[:report_format].in?([nil, '', 'pdf'])
|
||||
end
|
||||
|
||||
def display_metadata_rows?
|
||||
@report.params[:display_metadata_rows].present? && raw_render?
|
||||
end
|
||||
|
||||
def display_header_row?
|
||||
@report.params[:display_header_row].present? && !raw_render?
|
||||
end
|
||||
@@ -33,13 +38,22 @@ module Reporting
|
||||
end
|
||||
|
||||
def table_headers
|
||||
@report.table_headers || []
|
||||
base = @report.table_headers || []
|
||||
return base unless display_metadata_rows?
|
||||
|
||||
[*metadata_headers, base]
|
||||
end
|
||||
|
||||
def table_rows
|
||||
@report.table_rows || []
|
||||
end
|
||||
|
||||
def metadata_headers
|
||||
return [] unless display_metadata_rows?
|
||||
|
||||
Reporting::ReportMetadataBuilder.new(@report, @report.try(:user)).rows
|
||||
end
|
||||
|
||||
def as_json(_context_controller = nil)
|
||||
@report.rows.map(&:to_h).as_json
|
||||
end
|
||||
|
||||
@@ -10,6 +10,10 @@ module Reporting
|
||||
@formatted_rules ||= @report.rules.map { |rule| format_rule(rule) }
|
||||
end
|
||||
|
||||
def metadata_option?
|
||||
true
|
||||
end
|
||||
|
||||
def header_option?
|
||||
formatted_rules.find { |rule| rule[:header].present? }
|
||||
end
|
||||
|
||||
@@ -13,7 +13,7 @@ module Reporting
|
||||
delegate :available_headers, :table_headers, :fields_to_hide, :fields_to_show,
|
||||
to: :headers_builder
|
||||
|
||||
delegate :formatted_rules, :header_option?, :summary_row_option?, to: :ruler
|
||||
delegate :formatted_rules, :header_option?, :summary_row_option?, :metadata_option?, to: :ruler
|
||||
|
||||
def initialize(user, params = {}, render: false)
|
||||
unless render
|
||||
|
||||
Reference in New Issue
Block a user