Skip to content

Commit 5217bce

Browse files
authored
Resolves #4544. filter manufacturer donations summary report by date (#4558)
* Resolves #4544. filter manufacturer donations summary report by selected range * initiate workflow? * add instance variable to each manufacturer to hold number of donations in current timeframe * set donation count variable and sort using active record query * add back def volume method in manufacturer model
1 parent da098ef commit 5217bce

4 files changed

Lines changed: 62 additions & 13 deletions

File tree

app/controllers/reports_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ def donations_summary
88

99
def manufacturer_donations_summary
1010
@recent_donations_from_manufacturers = current_organization.donations.during(helpers.selected_range).by_source(:manufacturer)
11-
@top_manufacturers = current_organization.manufacturers.by_donation_count
12-
@donations = current_organization.donations.during(helpers.selected_range)
13-
@recent_donations = @donations.recent
11+
@top_manufacturers = current_organization.manufacturers.by_donation_count(10, helpers.selected_range)
1412
end
1513

1614
def purchases_summary

app/models/manufacturer.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ def volume
2626
donations.joins(:line_items).sum(:quantity)
2727
end
2828

29-
def self.by_donation_count(count = 10)
30-
# selects manufacturers that have donation qty > 0
29+
def self.by_donation_count(count = 10, date_range = nil)
30+
# selects manufacturers that have donation qty > 0 in the provided date range
3131
# and sorts them by highest volume of donation
32-
select { |m| m.volume.positive? }.sort.reverse.first(count)
32+
joins(donations: :line_items).where(donations: { issued_at: date_range })
33+
.select('manufacturers.*, sum(line_items.quantity) as donation_count')
34+
.group('manufacturers.id')
35+
.having('sum(line_items.quantity) > 0')
36+
.order('donation_count DESC')
37+
.limit(count)
3338
end
3439

3540
private
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<div class="float-center manufacturer">
2-
<%= link_to manufacturer do %>
3-
<%= manufacturer.name %> (<%= number_with_delimiter(manufacturer.volume) %>)
4-
<% end %>
5-
</div>
1+
<div class="float-center manufacturer">
2+
<%= link_to manufacturer do %>
3+
<%= manufacturer.name %> (<%= number_with_delimiter(manufacturer.donation_count) %>)
4+
<% end %>
5+
</div>

spec/requests/reports/manufacturer_donations_summary_spec.rb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
RSpec.describe "Reports::ManufacturerDonationsSummary", type: :request do
22
let(:organization) { create(:organization) }
33
let(:user) { create(:user, organization: organization) }
4+
let(:manufacturer1) { create(:manufacturer, organization: organization, name: "Manufacturer 1") }
5+
let(:manufacturer2) { create(:manufacturer, organization: organization, name: "Manufacturer 2") }
6+
let(:manufacturer3) { create(:manufacturer, organization: organization, name: "Manufacturer 3") }
47

58
describe "while signed in" do
69
before do
@@ -9,13 +12,56 @@
912

1013
describe "GET #index" do
1114
subject do
12-
get reports_manufacturer_donations_summary_path(format: response_format)
15+
get reports_manufacturer_donations_summary_path(format: "html")
1316
response
1417
end
15-
let(:response_format) { "html" }
1618

1719
it { is_expected.to have_http_status(:success) }
1820
end
21+
22+
context "when visiting the summary page" do
23+
it "has a link to create a new donation" do
24+
get reports_manufacturer_donations_summary_path
25+
26+
expect(response.body).to include("New Donation")
27+
expect(response.body).to include("#{@url_prefix}/donations/new")
28+
end
29+
30+
context "with manufacturer donations in the last year" do
31+
let(:formatted_date_range) { date_range.map { _1.to_formatted_s(:date_picker) }.join(" - ") }
32+
let(:date_range) { [1.year.ago, 0.days.ago] }
33+
let!(:donations) do
34+
[
35+
create(:donation, :with_items, item_quantity: 2, issued_at: 5.days.ago, organization: organization, source: "Manufacturer", manufacturer: manufacturer1),
36+
create(:donation, :with_items, item_quantity: 3, issued_at: 3.months.ago, organization: organization, source: "Manufacturer", manufacturer: manufacturer1),
37+
create(:donation, :with_items, item_quantity: 7, issued_at: 2.years.ago, organization: organization, source: "Manufacturer", manufacturer: manufacturer2),
38+
create(:donation, :with_items, item_quantity: 1, issued_at: 0.days.ago, organization: organization, source: "Manufacturer", manufacturer: manufacturer2),
39+
create(:donation, :with_items, item_quantity: 13, issued_at: 20.days.ago, organization: organization, source: "Manufacturer", manufacturer: manufacturer3),
40+
create(:donation, :with_items, item_quantity: 17, issued_at: 5.years.ago, organization: organization, source: "Manufacturer", manufacturer: manufacturer3)
41+
]
42+
end
43+
44+
it "shows correct total received donations" do
45+
get reports_manufacturer_donations_summary_path(user.organization), params: {filters: {date_range: formatted_date_range}}
46+
47+
expect(response.body).to match(%r{<span class="total_received_donations">\s*19\s*</span>})
48+
end
49+
50+
it "shows correct individual donations for each manufacturer" do
51+
get reports_manufacturer_donations_summary_path(user.organization), params: {filters: {date_range: formatted_date_range}}
52+
53+
expect(response.body).to match(%r{Manufacturer 1 \(5\)})
54+
expect(response.body).to match(%r{Manufacturer 2 \(1\)})
55+
expect(response.body).to match(%r{Manufacturer 3 \(13\)})
56+
end
57+
58+
it "shows top manufacturers in desc. order" do
59+
get reports_manufacturer_donations_summary_path(user.organization), params: {filters: {date_range: formatted_date_range}}
60+
61+
expect(response.body).to match(%r{Manufacturer 3 .* Manufacturer 1 .*Manufacturer 2}m)
62+
end
63+
end
64+
end
1965
end
2066

2167
describe "while not signed in" do

0 commit comments

Comments
 (0)