Browse Source

CLDC-3014 scheme csv download improvements (#2091)

* feat: optimise locations count

* feat: use each to preserve ordering, and optimise scheme downloads
pull/2092/head
natdeanlewissoftwire 1 year ago committed by GitHub
parent
commit
858de4913e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/helpers/schemes_helper.rb
  2. 2
      app/services/csv/lettings_log_csv_service.rb
  3. 2
      app/services/csv/sales_log_csv_service.rb
  4. 23
      app/services/csv/scheme_csv_service.rb

4
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

2
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

2
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

23
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

Loading…
Cancel
Save