Skip to content

Commit 04bdaf7

Browse files
committed
5002 - Filter by Category Added to Donations And Purchases Page
1 parent d1c52ea commit 04bdaf7

8 files changed

Lines changed: 60 additions & 2 deletions

File tree

app/controllers/donations_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def index
2323
.order(created_at: :desc)
2424
.class_filter(filter_params)
2525
.during(helpers.selected_range)
26+
@item_categories = current_organization.item_categories.pluck(:name).uniq
2627
@paginated_donations = @donations.page(params[:page])
2728

2829
@product_drives = current_organization.product_drives.alphabetized
@@ -39,6 +40,7 @@ def index
3940
@selected_storage_location = filter_params[:at_storage_location]
4041
@sources = @donations.collect(&:source).uniq.sort
4142
@selected_source = filter_params[:by_source]
43+
@selected_item_category = filter_params[:by_category]
4244
@donation_sites = @donations.collect(&:donation_site).compact.uniq.sort_by { |site| site.name.downcase }
4345
@selected_donation_site = filter_params[:from_donation_site]
4446
@selected_product_drive = filter_params[:by_product_drive]
@@ -155,7 +157,7 @@ def donation_item_params
155157
def filter_params
156158
return {} unless params.key?(:filters)
157159

158-
params.require(:filters).permit(:at_storage_location, :by_source, :from_donation_site, :by_product_drive, :by_product_drive_participant, :from_manufacturer)
160+
params.require(:filters).permit(:at_storage_location, :by_source, :from_donation_site, :by_product_drive, :by_product_drive_participant, :from_manufacturer, :by_category)
159161
end
160162

161163
# Omits donation_site_id or product_drive_participant_id if those aren't selected as source

app/controllers/purchases_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def index
99
.order(created_at: :desc)
1010
.class_filter(filter_params)
1111
.during(helpers.selected_range)
12+
@item_categories = current_organization.item_categories.pluck(:name).uniq
1213

1314
@paginated_purchases = @purchases.page(params[:page])
1415
# Are these going to be inefficient with large datasets?
@@ -24,6 +25,7 @@ def index
2425
@paginated_fair_market_values = @paginated_purchases.collect(&:value_per_itemizable).sum
2526
# Storage and Vendor
2627
@storage_locations = current_organization.storage_locations.active
28+
@selected_item_category = filter_params[:by_category]
2729
@selected_storage_location = filter_params[:at_storage_location]
2830
@vendors = current_organization.vendors.sort_by { |vendor| vendor.business_name.downcase }
2931
@selected_vendor = filter_params[:from_vendor]
@@ -118,7 +120,7 @@ def purchase_params
118120
def filter_params
119121
return {} unless params.key?(:filters)
120122

121-
params.require(:filters).permit(:at_storage_location, :by_source, :from_vendor)
123+
params.require(:filters).permit(:at_storage_location, :by_source, :from_vendor, :by_category)
122124
end
123125

124126
# If line_items have submitted with empty rows, clear those out first.

app/models/donation.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class Donation < ApplicationRecord
5252
where(manufacturer_id: manufacturer_id)
5353
}
5454

55+
scope :by_category, ->(item_category) {
56+
joins(line_items: {item: :item_category}).where("item_categories.name ILIKE ?", item_category)
57+
}
58+
5559
before_create :combine_duplicates
5660

5761
validates :donation_site, presence:

app/models/purchase.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class Purchase < ApplicationRecord
5151

5252
scope :active, -> { joins(:line_items).joins(:items).where(items: { active: true }) }
5353

54+
scope :by_category, ->(item_category) {
55+
joins(line_items: {item: :item_category}).where("item_categories.name ILIKE ?", item_category)
56+
}
57+
5458
before_create :combine_duplicates
5559

5660
validates :amount_spent_in_cents, numericality: { greater_than: 0 }

app/views/donations/index.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@
7171
<%= filter_select(label: "Filter by Donation Site", scope: :from_donation_site, collection: @donation_sites, key: :id, value: :name, selected: @selected_donation_site) %>
7272
</div>
7373
<% end %>
74+
<% if @item_categories.present? %>
75+
<div class="form-group col-lg-3 col-md-4 col-sm-6 col-xs-12">
76+
<% id = "filter_#{SecureRandom.uuid}" %>
77+
<%= label_tag id, "Filter by Category" %>
78+
<%= select_tag "filters[by_category]",
79+
options_for_select(@item_categories, @selected_item_category),
80+
{ include_blank: true, class: "form-control", id: id } %>
81+
</div>
82+
<% end %>
7483
<div class="form-group col-lg-3 col-md-4 col-sm-6 col-xs-12">
7584
<%= label_tag "Date Range" %>
7685
<%= render partial: "shared/date_range_picker", locals: {css_class: "form-control"} %>

app/views/purchases/index.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
<%= filter_select(label: "Filter by vendor", scope: :from_vendor, collection: @vendors, value: :business_name, selected: @selected_vendor) %>
4747
</div>
4848
<% end %>
49+
<% if @item_categories.present? %>
50+
<div class="form-group col-lg-3 col-md-4 col-sm-6 col-xs-12">
51+
<% id = "filter_#{SecureRandom.uuid}" %>
52+
<%= label_tag id, "Filter by Category" %>
53+
<%= select_tag "filters[by_category]",
54+
options_for_select(@item_categories, @selected_item_category),
55+
{ include_blank: true, class: "form-control", id: id } %>
56+
</div>
57+
<% end %>
4958
<div class="form-group col-lg-4 col-md-4 col-sm-6 col-xs-12">
5059
<%= label_tag "Purchase Date Range" %>
5160
<%= render partial: "shared/date_range_picker", locals: {css_class: "form-control"} %>

spec/system/donation_system_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@
135135
expect(page).to have_css("table tbody tr", count: 1)
136136
end
137137

138+
it "Filters by category" do
139+
category_1 = create(:item_category, name: "Category 1", organization: organization)
140+
category_2 = create(:item_category, name: "Category 2", organization: organization)
141+
item_1 = create(:item, item_category: category_1, name: "Item 1", value_in_cents: 100)
142+
item_2 = create(:item, item_category: category_2, name: "Item 2", value_in_cents: 200)
143+
create(:donation, :with_items, item_quantity: 25, item: item_1)
144+
create(:donation, :with_items, item_quantity: 30, item: item_2)
145+
visit subject
146+
expect(page).to have_css("table tbody tr", count: 2)
147+
select organization.item_categories.first.name, from: "filters[by_category]"
148+
click_button "Filter"
149+
expect(page).to have_css("table tbody tr", count: 1)
150+
end
151+
138152
it_behaves_like "Date Range Picker", Donation, "issued_at"
139153

140154
it "Filters by multiple attributes" do

spec/system/purchase_system_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@
8080
expect(page).to have_css("table tbody tr", count: 1)
8181
end
8282

83+
it "Filters by category" do
84+
category_1 = create(:item_category, name: "Category 1", organization: organization)
85+
category_2 = create(:item_category, name: "Category 2", organization: organization)
86+
item_1 = create(:item, item_category: category_1, name: "Item 1", value_in_cents: 100)
87+
item_2 = create(:item, item_category: category_2, name: "Item 2", value_in_cents: 200)
88+
create(:purchase, :with_items, item_quantity: 25, item: item_1)
89+
create(:purchase, :with_items, item_quantity: 30, item: item_2)
90+
visit subject
91+
expect(page).to have_css("table tbody tr", count: 2)
92+
select organization.item_categories.first.name, from: "filters[by_category]"
93+
click_button "Filter"
94+
expect(page).to have_css("table tbody tr", count: 1)
95+
end
96+
8397
it_behaves_like "Date Range Picker", Purchase, :issued_at
8498
end
8599
end

0 commit comments

Comments
 (0)