From 858de4913e8a5329dcb21a03c9d355eb877c9845 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Date: Thu, 14 Dec 2023 14:32:36 +0000 Subject: [PATCH] CLDC-3014 scheme csv download improvements (#2091) * feat: optimise locations count * feat: use each to preserve ordering, and optimise scheme downloads --- app/helpers/schemes_helper.rb | 4 ++-- app/services/csv/lettings_log_csv_service.rb | 2 +- app/services/csv/sales_log_csv_service.rb | 2 +- app/services/csv/scheme_csv_service.rb | 23 ++++++++++++-------- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index 98bd2d62f..88022dd53 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -60,10 +60,10 @@ module SchemesHelper when "schemes" "You've selected #{pluralize(scheme_count, 'scheme')}." when "locations" - location_count = schemes.map(&:locations).flatten.count + location_count = Location.where(scheme: schemes).count "You've selected #{pluralize(location_count, 'location')} from #{pluralize(scheme_count, 'scheme')}." when "combined" - location_count = schemes.map(&:locations).flatten.count + location_count = Location.where(scheme: schemes).count "You've selected #{pluralize(scheme_count, 'scheme')} with #{pluralize(location_count, 'location')}. The CSV will have one location per row with scheme details listed for each location." end end diff --git a/app/services/csv/lettings_log_csv_service.rb b/app/services/csv/lettings_log_csv_service.rb index ceb27acbe..889cbf263 100644 --- a/app/services/csv/lettings_log_csv_service.rb +++ b/app/services/csv/lettings_log_csv_service.rb @@ -10,7 +10,7 @@ module Csv CSV.generate(headers: true) do |csv| csv << @attributes - logs.find_each do |log| + logs.each do |log| csv << @attributes.map { |attribute| value(attribute, log) } end end diff --git a/app/services/csv/sales_log_csv_service.rb b/app/services/csv/sales_log_csv_service.rb index 900d3fdb1..89aa490ca 100644 --- a/app/services/csv/sales_log_csv_service.rb +++ b/app/services/csv/sales_log_csv_service.rb @@ -9,7 +9,7 @@ module Csv CSV.generate(headers: true) do |csv| csv << @attributes - logs.find_each do |log| + logs.each do |log| csv << @attributes.map { |attribute| value(attribute, log) } end end diff --git a/app/services/csv/scheme_csv_service.rb b/app/services/csv/scheme_csv_service.rb index a45266f58..e14d0e029 100644 --- a/app/services/csv/scheme_csv_service.rb +++ b/app/services/csv/scheme_csv_service.rb @@ -10,17 +10,22 @@ module Csv def prepare_csv(schemes) CSV.generate(headers: true) do |csv| csv << attributes - schemes.find_each do |scheme| - if @download_type == "schemes" + + case @download_type + when "schemes" + schemes.each do |scheme| csv << scheme_attributes.map { |attribute| scheme_value(attribute, scheme) } - else + end + when "locations" + schemes.each do |scheme| + scheme.locations.each do |location| + csv << [scheme.id_to_display] + location_attributes.map { |attribute| location_value(attribute, location) } + end + end + when "combined" + schemes.each do |scheme| scheme.locations.each do |location| - case @download_type - when "locations" - csv << [scheme.id_to_display] + location_attributes.map { |attribute| location_value(attribute, location) } - when "combined" - csv << scheme_attributes.map { |attribute| scheme_value(attribute, scheme) } + location_attributes.map { |attribute| location_value(attribute, location) } - end + csv << scheme_attributes.map { |attribute| scheme_value(attribute, scheme) } + location_attributes.map { |attribute| location_value(attribute, location) } end end end