Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion app/controllers/donations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def donation_item_params
def filter_params
return {} unless params.key?(:filters)

params.require(:filters).permit(:at_storage_location, :by_source, :from_donation_site, :by_product_drive, :by_product_drive_participant, :from_manufacturer, :by_category)
params.require(:filters).permit(:at_storage_location, :by_source, :from_donation_site, :by_product_drive, :by_product_drive_participant, :from_manufacturer, :by_item_id, :by_item_category_id, :by_category)
end

# Omits donation_site_id or product_drive_participant_id if those aren't selected as source
Expand Down
2 changes: 2 additions & 0 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Donation < ApplicationRecord
where(manufacturer_id: manufacturer_id)
}

scope :by_item_id, ->(item_id) { includes(:items).where(items: { id: item_id }) }
scope :by_item_category_id, ->(item_category_id) { includes(:items).where(items: { item_category_id: item_category_id }) }
scope :by_category, ->(item_category) {
joins(line_items: {item: :item_category}).where("item_categories.name ILIKE ?", item_category)
}
Expand Down
9 changes: 8 additions & 1 deletion app/models/view/donations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module View
Donations = Data.define(
:donations,
:filters,
:items,
:item_categories,
:paginated_donations,
:product_drives,
Expand All @@ -18,7 +19,8 @@ def filter_params(params)
params.require(:filters).permit(
:at_storage_location, :by_source, :from_donation_site,
:by_product_drive, :by_product_drive_participant,
:from_manufacturer, :by_category
:from_manufacturer, :by_item_id, :by_item_category_id,
:by_category
)
else
{}
Expand Down Expand Up @@ -49,6 +51,7 @@ def from_params(params:, organization:, helpers:)
new(
donations: donations,
filters: filters,
items: organization.items.alphabetized.select(:id, :name),
item_categories: organization.item_categories.pluck(:name).uniq,
paginated_donations: paginated_donations,
product_drives: organization.product_drives.alphabetized,
Expand All @@ -68,6 +71,10 @@ def selected_source
filters[:by_source]
end

def selected_item
filters[:by_item_id].presence
end

def selected_item_category
filters[:by_category]
end
Expand Down
5 changes: 5 additions & 0 deletions app/views/donations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
selected: @donation_info.selected_donation_site) %>
</div>
<% end %>
<% if @donation_info.items.present? %>
<div class="form-group col-lg-3 col-md-4 col-sm-6 col-xs-12">
<%= filter_select(label: "Filter by Item", scope: :by_item_id, collection: @donation_info.items, selected: @donation_info.selected_item) %>
</div>
<% end %>
<% if @donation_info.item_categories.present? %>
<div class="form-group col-lg-3 col-md-4 col-sm-6 col-xs-12">
<% id = "filter_#{SecureRandom.uuid}" %>
Expand Down
22 changes: 22 additions & 0 deletions spec/models/donation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@
end
end
end

describe "by_item_id >" do
it "returns only donations with the specified item" do
item1 = create(:item, organization: organization)
item2 = create(:item, organization: organization)
create(:donation, :with_items, item: item1, organization: organization)
create(:donation, :with_items, item: item2, organization: organization)
expect(Donation.by_item_id(item1.id).count).to eq(1)
end
end

describe "by_item_category_id >" do
it "returns only donations with items in the specified category" do
category = create(:item_category, organization: organization)
other_category = create(:item_category, organization: organization)
item1 = create(:item, item_category: category, organization: organization)
item2 = create(:item, item_category: other_category, organization: organization)
create(:donation, :with_items, item: item1, organization: organization)
create(:donation, :with_items, item: item2, organization: organization)
expect(Donation.by_item_category_id(category.id).count).to eq(1)
end
end
end

context "Associations >" do
Expand Down
12 changes: 12 additions & 0 deletions spec/system/donation_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@
expect(page).to have_css("table tbody tr", count: 1)
end

it "Filters by item" do
item_1 = create(:item, name: "Good item", organization: organization)
item_2 = create(:item, name: "Bad item", organization: organization)
create(:donation, :with_items, item: item_1)
create(:donation, :with_items, item: item_2)
visit subject
expect(page).to have_css("table tbody tr", count: 2)
select item_1.name, from: "filters[by_item_id]"
click_button "Filter"
expect(page).to have_css("table tbody tr", count: 1)
end

it "Filters by category" do
category_1 = create(:item_category, name: "Category 1", organization: organization)
category_2 = create(:item_category, name: "Category 2", organization: organization)
Expand Down