Skip to content

Commit

Permalink
Merge pull request #4045 from elasticspoon/3324-add-donation-source-d…
Browse files Browse the repository at this point in the history
…etails

Fix issue #3324 to add donation source details
  • Loading branch information
awwaiid authored Feb 25, 2024
2 parents 98e2f1d + 626d470 commit f383a85
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 28 deletions.
13 changes: 13 additions & 0 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ def self.daily_quantities_by_source(start, stop)
.sum("line_items.quantity")
end

def details
case source
when SOURCES[:product_drive]
product_drive.name
when SOURCES[:manufacturer]
manufacturer.name
when SOURCES[:donation_site]
donation_site.name
when SOURCES[:misc]
comment&.truncate(25, separator: /\s/)
end
end

def remove(item)
# doing this will handle either an id or an object
item_id = item.to_i
Expand Down
6 changes: 3 additions & 3 deletions app/services/exports/export_donations_csv_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ def headers_with_indexes
def base_table
{
"Source" => ->(donation) {
donation.source_view
donation.source
},
"Date" => ->(donation) {
donation.issued_at.strftime("%F")
},
"Donation Site" => ->(donation) {
donation.donation_site.try(:name)
"Details" => ->(donation) {
donation.details
},
"Storage Location" => ->(donation) {
donation.storage_view
Expand Down
20 changes: 11 additions & 9 deletions app/views/donations/_donation_row.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<tr>
<td><%= donation_row.source_view %></td>
<td><%= donation_row.source %></td>
<td class="text-left date"><%= donation_row.issued_at.strftime("%F") %>
<td><%= donation_row.donation_site.try(:name) %></td>
<td><%= donation_row.storage_location.name %></td>
<td class="text-left numeric"><%= donation_row.line_items.total %></td>
<td class="text-left numeric"><%= dollar_value(donation_row.money_raised.to_i) %></td>
<td class="text-left numeric"><%= dollar_value(donation_row.value_per_itemizable) %></td>
<td><%= truncate donation_row.comment, length: 140, separator: /\w+/ %>
<td class="text-right">
<%= view_button_to donation_path(donation_row) %>
<td><%= donation_row.details %></td>
<td><%= donation_row.storage_location.name %></td>
<td class="text-left numeric"><%= donation_row.line_items.total %></td>
<td class="text-left numeric"><%= dollar_value(donation_row.money_raised.to_i) %></td>
<td class="text-left numeric"><%= dollar_value(donation_row.value_per_itemizable) %></td>
<td><%= truncate donation_row.comment, length: 140, separator: /\w+/ %>
<td class="text-right">
<%= view_button_to donation_path(donation_row) %>
</td>
</td>
</td>
</tr>
24 changes: 12 additions & 12 deletions app/views/donations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,20 @@
<div class="card-body table-responsive p-0">
<table class="table table-hover">
<thead>
<tr>
<th>Source</th>
<th>Date</th>
<th>Donation Site</th>
<th>Storage Location</th>
<th>Quantity of Items</th>
<th>Money Raised</th>
<th>In Kind Value</th>
<th>Comments</th>
<th>Actions</th>
</tr>
<tr>
<th>Source</th>
<th>Date</th>
<th>Details</th>
<th>Storage Location</th>
<th>Quantity of Items</th>
<th>Money Raised</th>
<th>In Kind Value</th>
<th>Comments</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<%= render partial: "donation_row", collection: @paginated_donations %>
<%= render partial: "donation_row", collection: @paginated_donations %>
</tbody>
<tfoot>
<tr>
Expand Down
38 changes: 38 additions & 0 deletions spec/models/donation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,44 @@
expect(donation.source_view).to eq(donation.source)
end
end

context "details" do
context "manufacturer" do
let(:manufacturer) { create(:manufacturer) }
let(:donation) { create(:donation, source: "Manufacturer", manufacturer: manufacturer) }

it "returns manufacturer name" do
expect(donation.details).to eq(manufacturer.name)
end
end

context "product drive" do
let(:product_drive) { create(:product_drive) }
let(:donation) { create(:donation, source: "Product Drive", product_drive: product_drive) }

it "returns product_drive name" do
expect(donation.details).to eq(product_drive.name)
end
end

context "donation site" do
let(:donation_site) { create(:donation_site) }
let(:donation) { create(:donation, source: "Donation Site", donation_site: donation_site) }

it "returns donation_site name" do
expect(donation.details).to eq(donation_site.name)
end
end

context "misc" do
let(:donation) { create(:donation, source: "Misc. Donation", comment: Faker::Lorem.paragraph) }

it "returns a truncated comment" do
short_comment = donation.comment.truncate(25, separator: /\s/)
expect(donation.details).to eq(short_comment)
end
end
end
end
end

Expand Down
50 changes: 49 additions & 1 deletion spec/requests/donations_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,59 @@
before do
create(:donation)
end

context "html" do
let(:response_format) { 'html' }

it { is_expected.to be_successful }

it "should have the columns source and details" do
expect(subject.body).to include("<th>Source</th>")
expect(subject.body).to include("<th>Details</th>")
end

context "when given a product drive" do
let(:product_drive) { create(:product_drive) }
let(:donation) { create(:donation, source: "Product Drive", product_drive: product_drive) }

it "should display Product Drive and the name of the drive" do
donation
expect(subject.body).to include("<td>#{donation.source}</td>")
expect(subject.body).to include("<td>#{product_drive.name}</td>")
end
end

context "when given a donation site" do
let(:donation_site) { create(:donation_site) }
let(:donation) { create(:donation, source: "Donation Site", donation_site: donation_site) }

it "should display Donation Site and the name of the site" do
donation
expect(subject.body).to include("<td>#{donation.source}</td>")
expect(subject.body).to include("<td>#{donation_site.name}</td>")
end
end

context "when given a manufacturer" do
let(:manufacturer) { create(:manufacturer) }
let(:donation) { create(:donation, source: "Manufacturer", manufacturer: manufacturer) }

it "should display Manufacturer and the manufacturer name" do
donation
expect(subject.body).to include("<td>#{donation.source}</td>")
expect(subject.body).to include("<td>#{manufacturer.name}</td>")
end
end

context "when given a misc donation" do
let(:donation) { create(:donation, source: "Misc. Donation", comment: Faker::Lorem.paragraph) }

it "should display Misc Donation and a truncated comment" do
donation
short_comment = donation.comment.truncate(25, separator: /\s/)
expect(subject.body).to include("<td>#{donation.source}</td>")
expect(subject.body).to include("<td>#{short_comment}</td>")
end
end
end

context "csv" do
Expand Down
6 changes: 3 additions & 3 deletions spec/services/exports/export_donations_csv_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
[
"Source",
"Date",
"Donation Site",
"Details",
"Storage Location",
"Quantity of Items",
"Variety of Items",
Expand Down Expand Up @@ -78,9 +78,9 @@

donations.zip(total_item_quantities).each_with_index do |(donation, total_item_quantity), idx|
row = [
donation.source_view,
donation.source,
donation.issued_at.strftime("%F"),
donation.donation_site.try(:name),
donation.details,
donation.storage_view,
donation.line_items.total,
total_item_quantity.count(&:positive?),
Expand Down

0 comments on commit f383a85

Please sign in to comment.