Skip to content
Open
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
9 changes: 9 additions & 0 deletions app/controllers/product_drives_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ def show
@selected_name_filter = filter_params[:by_name]
@selected_item_category = filter_params[:by_item_category_id]
@product_drive = current_organization.product_drives.includes(:donations).find(params[:id])

respond_to do |format|
format.html
format.csv do
send_data Exports::ExportProductDriveParticipantsCSVService.new(
@product_drive
).generate_csv, filename: "Product-Drive-Participants-#{@product_drive.name}-#{Time.zone.today}.csv"
end
end
end

def update
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Exports
class ExportProductDriveParticipantsCSVService
include ItemsHelper

HEADERS = ["Donation Id", "Product Drive Participant", "Storage Location", "Quantity", "In Kind Value"].freeze

def initialize(product_drive)
Comment thread
mausamp marked this conversation as resolved.
Outdated
@product_drive = product_drive
end

def generate_csv
CSV.generate do |csv|
csv << generate_headers

product_drive.donations.includes(:product_drive_participant).find_each do |donation|
csv << generate_row(donation)
end
end
end

private

attr_reader :product_drive

def generate_row(donation)
[
donation.id.to_s,
donation.product_drive_participant_id ? donation.product_drive_participant.business_name : nil,
donation.storage_location.name,
donation.line_items.count(&:quantity),
dollar_value(donation.value_per_itemizable)
]
end

def generate_headers
HEADERS
end
end
end
12 changes: 12 additions & 0 deletions app/views/product_drives/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
To add additional donations to this product drive, please edit that donation and select this product drive from the appropriate dropdown.
</p>
<!-- /.card-footer-->
<div class="card-footer">
<span class="float-right">
<%=
if @product_drive.donations.length > 0
download_button_to(
product_drive_path(@product_drive, format: :csv),
text: "Export Product Drive Participants"
)
end
%>
</span>
</div>
</div>
<!-- /.card -->
</div>
Expand Down
28 changes: 28 additions & 0 deletions spec/requests/product_drives_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,34 @@

expect(response.body).to include("4862167")
end

context "csv" do
it 'is successful' do
product_drive = create(:product_drive, organization: organization)

get product_drive_path(id: product_drive.id, format: :csv)

expect(response).to be_successful
expect(response.header['Content-Type']).to include 'text/csv'

expected_headers = Exports::ExportProductDriveParticipantsCSVService::HEADERS
expect(response.body.chomp.split(",")).to eq(expected_headers)
end

it 'returns ONLY the associated product drive participants' do
product_drive = create(:product_drive, organization: organization)
participant = create(:product_drive_participant, business_name: "Associated Participant")
create(:donation, product_drive: product_drive, product_drive_participant: participant)

other_participant = create(:product_drive_participant, business_name: "Unassociated Participant")
create(:donation, product_drive: create(:product_drive, organization: organization), product_drive_participant: other_participant)

get product_drive_path(id: product_drive.id, format: :csv)

expect(response.body).to include("Associated Participant")
expect(response.body).not_to include("Unassociated Participant")
end
end
end

describe "DELETE #destroy" do
Expand Down
Loading