Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 3028 convert csv to excel #5209

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: refactor data columns
  • Loading branch information
xeniabarreto committed Oct 26, 2023
commit 8c0207ad37de38d563c55d45c157981efe05a334
26 changes: 4 additions & 22 deletions app/services/case_contacts_export_csv_service.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "csv"
require 'case_contacts_export_data_columns'

class CaseContactsExportCsvService
attr_reader :case_contacts, :filtered_columns
include CaseContactsExportDataColumns

def initialize(case_contacts, filtered_columns = nil)
@filtered_columns = filtered_columns || CaseContactsExportCsvService.DATA_COLUMNS.keys
@filtered_columns = filtered_columns || CaseContactsExportDataColumns.data_columns.keys

@case_contacts = case_contacts.preload({creator: :supervisor}, :contact_types, :casa_case)
end
Expand All @@ -14,30 +16,10 @@ def perform
csv << filtered_columns.map(&:to_s).map(&:titleize)
if case_contacts.present?
case_contacts.decorate.each do |case_contact|
csv << CaseContactsExportCsvService.DATA_COLUMNS(case_contact).slice(*filtered_columns).values
csv << CaseContactsExportDataColumns.data_columns(case_contact).slice(*filtered_columns).values
end
end
end
end

def self.DATA_COLUMNS(case_contact = nil)
# Note: these header labels are for stakeholders and do not match the
# Rails DB names in all cases, e.g. added_to_system_at header is case_contact.created_at
{
internal_contact_number: case_contact&.id,
duration_minutes: case_contact&.report_duration_minutes,
contact_types: case_contact&.report_contact_types,
contact_made: case_contact&.report_contact_made,
contact_medium: case_contact&.medium_type,
occurred_at: I18n.l(case_contact&.occurred_at, format: :full, default: nil),
added_to_system_at: case_contact&.created_at,
miles_driven: case_contact&.miles_driven,
wants_driving_reimbursement: case_contact&.want_driving_reimbursement,
casa_case_number: case_contact&.casa_case&.case_number,
creator_email: case_contact&.creator&.email,
creator_name: case_contact&.creator&.display_name,
supervisor_name: case_contact&.creator&.supervisor&.display_name,
case_contact_notes: case_contact&.notes
}
end
end
24 changes: 24 additions & 0 deletions app/services/case_contacts_export_data_columns.rb
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked how you extracted the same code here so both CSV and Excel services could use it.

But what I don't like (and predates your code) is how this is used for two totally different things:

  1. To get the list of columns for the view
  2. To get a hash for each row of the CSV/Excel file

This is why all the safe operators (&) because in the view it's sending in nil for the case contact. I think a better pattern is to have a list of the columns and use them in the decorator.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module CaseContactsExportDataColumns
def self.data_columns(case_contact = nil)

data = {
internal_contact_number: case_contact&.id,
duration_minutes: case_contact&.report_duration_minutes,
contact_types: case_contact&.report_contact_types,
contact_made: case_contact&.report_contact_made,
contact_medium: case_contact&.medium_type,
occurred_at: I18n.l(case_contact&.occurred_at, format: :full, default: nil),
added_to_system_at: case_contact&.created_at&.strftime('%Y-%m-%d %H:%M:%S %Z'),
miles_driven: case_contact&.miles_driven,
wants_driving_reimbursement: case_contact&.want_driving_reimbursement,
casa_case_number: case_contact&.casa_case&.case_number,
creator_email: case_contact&.creator&.email,
creator_name: case_contact&.creator&.display_name,
supervisor_name: case_contact&.creator&.supervisor&.display_name,
case_contact_notes: case_contact&.notes
}

data
end

end
33 changes: 9 additions & 24 deletions app/services/case_contacts_export_excel_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

# CaseContactsExportExcelService handles the conversion of case contact data to Excel format.
# It provides methods for exporting case contact information to Excel files.
require 'case_contacts_export_data_columns'

class CaseContactsExportExcelService
attr_reader :case_contacts, :filtered_columns
include CaseContactsExportDataColumns

FONT_SIZE = 11
LINE_PADDING = 10

def initialize(case_contacts, filtered_columns = nil)
@filtered_columns = filtered_columns || CaseContactsExportExcelService.DATA_COLUMNS.keys
@filtered_columns = filtered_columns || CaseContactsExportDataColumns.data_columns.keys

@case_contacts = case_contacts.preload({ creator: :supervisor }, :contact_types, :casa_case)
end
Expand Down Expand Up @@ -39,11 +42,13 @@ def perform
def configure_case_contact_notes_width(sheet)
case_contact_notes_index = filtered_columns.index(:case_contact_notes)

sheet.column_info[case_contact_notes_index].width = 140
if case_contact_notes_index
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a good catch from the previous code. It was throwing a nil --> Integer error when the notes weren't a selected column.

sheet.column_info[case_contact_notes_index].width = 140
end
end

def format_row(sheet, case_contact, wrap_style)
row_data = CaseContactsExportExcelService.DATA_COLUMNS(case_contact).slice(*filtered_columns).values
row_data = CaseContactsExportDataColumns.data_columns(case_contact).slice(*filtered_columns).values
row_data << ''
sheet.add_row(row_data, style: wrap_style)
end
Expand All @@ -63,25 +68,5 @@ def string_width(string, row, font_size)
font_scale = font_size / row.worksheet.workbook.font_scale_divisor
(string.to_s.size + 3) * font_scale
end

def self.DATA_COLUMNS(case_contact = nil)
# NOTE: these header labels are for stakeholders and do not match the
# Rails DB names in all cases, e.g. added_to_system_at header is case_contact.created_at
{
internal_contact_number: case_contact&.id,
duration_minutes: case_contact&.report_duration_minutes,
contact_types: case_contact&.report_contact_types,
contact_made: case_contact&.report_contact_made,
contact_medium: case_contact&.medium_type,
occurred_at: I18n.l(case_contact&.occurred_at, format: :full, default: nil),
added_to_system_at: case_contact&.created_at.strftime('%Y-%m-%d %H:%M:%S %Z'),
miles_driven: case_contact&.miles_driven,
wants_driving_reimbursement: case_contact&.want_driving_reimbursement,
casa_case_number: case_contact&.casa_case&.case_number,
creator_email: case_contact&.creator&.email,
creator_name: case_contact&.creator&.display_name,
supervisor_name: case_contact&.creator&.supervisor&.display_name,
case_contact_notes: case_contact&.notes
}
end

end
Loading