Skip to content

Commit 95cae51

Browse files
authored
Merge pull request #5478 from rubyforgood/speed-up-distribution-export
Switch to in-memory item selection to speed up export
2 parents 360811f + 4f251c4 commit 95cae51

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

app/services/exports/export_distributions_csv_service.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Don't technically need to pull in csv for the code itself,
2+
# but this ensures that the unit test can run in isolation
3+
require 'csv'
4+
15
module Exports
26
class ExportDistributionsCSVService
37
include DistributionHelper
@@ -135,7 +139,8 @@ def build_row_data(distribution)
135139
row = base_table.values.map { |closure| closure.call(distribution) }
136140

137141
item_names.each do |item_name|
138-
line_items = distribution.line_items.where(item: @organization.items.find_by(name: item_name))
142+
# We are doing this in-memory so that we can use the already-loaded line item records
143+
line_items = distribution.line_items.select { |item| item.name == item_name }
139144

140145
row << line_items.sum(&:quantity)
141146
row << Money.new(line_items.sum(&:value_per_line_item)) if @organization.include_in_kind_values_in_exported_files

0 commit comments

Comments
 (0)