Skip to content

Commit

Permalink
Allow column formatting and header/footer stripping to be specified i…
Browse files Browse the repository at this point in the history
…ndependently

This will allow us to change the options in the following commit.
  • Loading branch information
dacook committed Feb 21, 2023
1 parent ab9f716 commit 172cce4
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/reporting/report_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ def initialize(report)
@report = report
end

# Strip headers and footers for these formats
def raw_render?
@report.params[:report_format].in?(['json', 'csv'])
end

# Do not format values for these output formats
def unformatted_render?
@report.params[:report_format].in?(['json', 'csv'])
end

def html_render?
@report.params[:report_format].in?([nil, '', 'pdf'])
end
Expand Down
2 changes: 1 addition & 1 deletion lib/reporting/report_row_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def build_row(item)
def slice_and_format_row(row)
result = row.to_h.select { |k, _v| k.in?(report.fields_to_show) }

unless report.raw_render?
unless report.unformatted_render?
result = result.map { |k, v| [k, format_cell(v, k)] }.to_h
end
OpenStruct.new(result)
Expand Down
2 changes: 1 addition & 1 deletion lib/reporting/report_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ReportTemplate
attr_accessor :user, :params, :ransack_params

delegate :render_as, :as_json, :to_html, :to_csv, :to_xlsx, :to_pdf, :to_json, to: :renderer
delegate :raw_render?, :html_render?, :display_header_row?, :display_summary_row?, to: :renderer
delegate :unformatted_render?, :html_render?, :display_header_row?, :display_summary_row?, to: :renderer

delegate :rows, :table_rows, :grouped_data, to: :rows_builder
delegate :available_headers, :table_headers, :fields_to_hide, :fields_to_show,
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/reports/order_cycle_management_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ module OrderCycleManagement
end

it 'returns rows with payment information' do
allow(subject).to receive(:raw_render?).and_return(true)
allow(subject).to receive(:unformatted_render?).and_return(true)
expect(subject.table_rows).to eq([[
order.billing_address.firstname,
order.billing_address.lastname,
Expand All @@ -192,7 +192,7 @@ module OrderCycleManagement
end

it 'returns rows with delivery information' do
allow(subject).to receive(:raw_render?).and_return(true)
allow(subject).to receive(:unformatted_render?).and_return(true)
expect(subject.table_rows).to eq([[
order.ship_address.firstname,
order.ship_address.lastname,
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/reports/orders_and_distributors_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module OrdersAndDistributors

it 'should denormalise order and distributor details for display as csv' do
subject = Base.new create(:admin_user), {}
allow(subject).to receive(:raw_render?).and_return(true)
allow(subject).to receive(:unformatted_render?).and_return(true)
table = subject.table_rows

expect(table.size).to eq 1
Expand Down

0 comments on commit 172cce4

Please sign in to comment.