Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
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
7 changes: 6 additions & 1 deletion app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class DistributionsController < ApplicationController
include DateRangeHelper
include DistributionHelper
include Validatable
include ActionController::Live

before_action :enable_turbo!, only: %i[new show]
skip_before_action :authenticate_user!, only: %i(calendar)
Expand Down Expand Up @@ -74,7 +75,11 @@ def index
respond_to do |format|
format.html
format.csv do
send_data Exports::ExportDistributionsCSVService.new(distributions: @distributions.includes(line_items: :item), organization: current_organization, filters: scope_filters).generate_csv, filename: "Distributions-#{Time.zone.today}.csv"
send_stream filename: "Distributions-#{Time.zone.today}.csv" do |stream|
Exports::ExportDistributionsCSVService.new(distributions: @distributions.includes(line_items: :item), organization: current_organization, filters: scope_filters).generate_csv_stream do |row|
stream.write(row)
end
end
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions app/services/exports/export_distributions_csv_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def generate_csv_data
csv_data
end

def generate_csv_stream
yield CSV.generate_line(base_headers + item_headers)

distributions.each do |distribution|
yield CSV.generate_line(build_row_data(distribution))
end
end

private

attr_reader :distributions
Expand Down
41 changes: 32 additions & 9 deletions spec/services/exports/export_distributions_csv_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
let(:include_in_kind_values) { false }
let(:include_packages) { false }

describe '#generate_csv' do
subject { described_class.new(distributions: distributions, organization: organization, filters: filters).generate_csv }
describe '#generate_csv_stream' do
subject do
proc { described_class.new(distributions: distributions, organization: organization, filters: filters).generate_csv_stream }
end

let(:duplicate_item) { create(:item, name: "Dupe Item", value_in_cents: 300, organization: organization, package_size: 2) }

Expand Down Expand Up @@ -60,7 +62,9 @@
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0,2,0,0
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0,0,0,3
CSV
expect(subject).to eq(csv)
subject do |subject_row|
expect(row).to eq(csv.next)
end
end
end

Expand All @@ -75,7 +79,9 @@
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0.00,0,0.00,2,60.00,0,0.00,0,0.00
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0.00,0,0.00,0,0.00,0,0.00,3,120.00
CSV
expect(subject).to eq(csv)
subject do |subject_row|
expect(row).to eq(csv.next)
end
end
end

Expand All @@ -90,7 +96,9 @@
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0,0,0,2,0,0,0,0,0
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0,0,0,0,0,0,0,3,0
CSV
expect(subject).to eq(csv)
subject do |subject_row|
expect(row).to eq(csv.next)
end
end
end

Expand All @@ -106,7 +114,9 @@
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0.00,0,0,0.00,0,2,60.00,0,0,0.00,0,0,0.00,0
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0.00,0,0,0.00,0,0,0.00,0,0,0.00,0,3,120.00,0
CSV
expect(subject).to eq(csv)
subject do |subject_row|
expect(row).to eq(csv.next)
end
end
end

Expand All @@ -128,13 +138,24 @@
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 2,Disposable Diapers,0,0,2,0,0,0
#{partner.name},04/04/2025,04/04/2025,#{storage_location.name},0,0.0,shipped,$15.01,scheduled,"",comment 3,Disposable Diapers,0,0,0,0,3,0
CSV
expect(subject).to eq(csv)

subject do |subject_row|
expect(row).to eq(csv.next)
end
end
end

context 'reporting category column' do
let(:filters) { {} }

subject do
rows = ""
described_class.new(distributions: distributions, organization: organization, filters: filters).generate_csv_stream do |row|
rows << row
end
rows
end

it 'includes a Reporting Category column with the humanized category for each distribution' do
csv = CSV.parse(subject, headers: true)
expect(csv.headers).to include("Reporting Category")
Expand Down Expand Up @@ -174,12 +195,14 @@
end

context 'when there are no distributions but the report is requested' do
subject { described_class.new(distributions: [], organization: organization, filters: filters).generate_csv }
let(:distributions) { [] }
it 'returns a csv with only headers and no rows' do
csv = <<~CSV
Partner,Initial Allocation,Scheduled for,Source Inventory,Total Number of #{item_name},Total Value of #{item_name},Delivery Method,Shipping Cost,Status,Agency Representative,Comments,Reporting Category,Dupe Item
CSV
expect(subject).to eq(csv)
subject do |subject_row|
expect(row).to eq(csv.next)
end
end
end
end
Expand Down
Loading