Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a way to list the product drive participants for a product drive #3762

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions app/models/product_drive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ProductDrive < ApplicationRecord
}

has_many :donations, dependent: :nullify
has_many :product_drive_participants, -> { distinct }, through: :donations
validates :name, presence:
{ message: "A name must be chosen." }
validates :start_date, presence:
Expand Down
4 changes: 4 additions & 0 deletions app/models/product_drive_participant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def volume
donations.map { |d| d.line_items.total }.reduce(:+)
end

def volume_by_product_drive(product_drive_id)
donations.by_product_drive(product_drive_id).map { |d| d.line_items.total }.sum
patelkrunal31 marked this conversation as resolved.
Show resolved Hide resolved
end

def donation_source_view
return if contact_name.blank?

Expand Down
45 changes: 45 additions & 0 deletions app/views/product_drives/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,48 @@
</div>
</div>
</section>

<section class="content">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a check in the request spec for this to see that the right information is being displayed? If there isn't a request spec, we should create one :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a test in my latest commit.

<div class="container-fluid">
<div class="row">
<div class="col-12">
<!-- Default box -->
<div class="card">
<div class="card-body p-0">
<table class="table">
<thead>
<tr>
<th>Business Name</th>
<th>Contact Name</th>
<th>Phone</th>
<th>Email</th>
<th>Address</th>
<th>Items for this Drive</th>
<th>Total Items (for all drives)</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<% @product_drive.product_drive_participants.each do |participant| %>
<tr>
<td><%= participant.business_name %></td>
<td><%= participant.contact_name %></td>
<td><%= participant.phone %></td>
<td><%= participant.email %></td>
<td><%= participant.address %></td>
<td><%= participant.volume_by_product_drive(@product_drive.id) %></td>
<td><%= participant.volume %></td>
<td><%= participant.comment %></td>
</tr>
<% end%>
</tbody>
</table>
</div>
<!-- /.card-body -->
<!-- /.card-footer-->
</div>
<!-- /.card -->
</div>
</div>
</div>
</section>
15 changes: 14 additions & 1 deletion spec/models/product_drive_participant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,27 @@

context "Methods" do
describe "volume" do
it "retrieves the amount of product that has been donated through this product drive" do
it "retrieves the amount of product that has been donated by participant" do
dd = create(:product_drive)
ddp = create(:product_drive_participant)
create(:donation, :with_items, item_quantity: 10, source: Donation::SOURCES[:product_drive], product_drive: dd, product_drive_participant: ddp)
expect(ddp.volume).to eq(10)
end
end

describe "volume_by_product_drive" do
it "retrieves the amount of product that has been donated through specific product drive" do
drive1 = create(:product_drive)
drive2 = create(:product_drive)
participant = create(:product_drive_participant)
create(:donation, :with_items, item_quantity: 10, source: Donation::SOURCES[:product_drive], product_drive: drive1, product_drive_participant: participant)
create(:donation, :with_items, item_quantity: 9, source: Donation::SOURCES[:product_drive], product_drive: drive2, product_drive_participant: participant)
expect(participant.volume).to eq(19)
expect(participant.volume_by_product_drive(drive1.id)).to eq(10)
expect(participant.volume_by_product_drive(drive2.id)).to eq(9)
end
end

describe "donation_source_view" do
let!(:participant) { create(:product_drive_participant, contact_name: contact_name) }

Expand Down
11 changes: 10 additions & 1 deletion spec/models/product_drive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,23 @@
end

describe "associations" do
let!(:donation) { create(:donation) }
let!(:product_drive_participant) { create(:product_drive_participant) }
let!(:donation) { create(:product_drive_donation, product_drive_participant: product_drive_participant) }
let!(:product_drive2) { create(:product_drive) }
let!(:donation2) { create(:donation, :with_items, item_quantity: 7, product_drive: product_drive2, product_drive_participant: product_drive_participant) }

subject { create(:product_drive) }

it "has_many donations" do
subject.donations << donation

expect(subject.donations).to include(donation)
end
it "has_many product_drive_participants through donations" do
subject.donations << donation
is_expected.to have_many(:product_drive_participants).through(:donations)
expect(subject.product_drive_participants).to include(donation.product_drive_participant)
end
end

describe "distinct_items" do
Expand Down
Loading