Skip to content

Commit

Permalink
Fix regression in item category filter
Browse files Browse the repository at this point in the history
  • Loading branch information
coalest committed Dec 9, 2024
1 parent 96b5604 commit 242e518
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/models/distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Distribution < ApplicationRecord
enum delivery_method: { pick_up: 0, delivery: 1, shipped: 2 }
scope :active, -> { joins(:line_items).joins(:items).where(items: { active: true }) }
# add item_id scope to allow filtering distributions by item
scope :by_item_id, ->(item_id) { joins(:items).where(items: { id: item_id }) }
scope :by_item_id, ->(item_id) { includes(:items).where(items: { id: item_id }) }
# partner scope to allow filtering by partner
scope :by_item_category_id, ->(item_category_id) { joins(:items).where(items: { item_category_id: item_category_id }) }
scope :by_item_category_id, ->(item_category_id) { includes(:items).where(items: { item_category_id: item_category_id }) }
scope :by_partner, ->(partner_id) { where(partner_id: partner_id) }
# location scope to allow filtering distributions by location
scope :by_location, ->(storage_location_id) { where(storage_location_id: storage_location_id) }
Expand Down
2 changes: 1 addition & 1 deletion app/services/distribution_totals_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def calculate_quantities(distributions)
#
# @return [Hash<Integer, Integer>]
def calculate_values(distributions)
distributions
Distribution
.where(id: distributions.class_filter(filter_params))
.left_joins(line_items: [:item])
.group("distributions.id, line_items.id, items.id")
Expand Down
12 changes: 12 additions & 0 deletions spec/requests/distributions_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@
expect(item_total_header.text).to eq("Total in #{item_category.name}")
expect(item_value_header.text).to eq("Total Value")
end

it "doesn't show duplicate distributions" do
# Add another item in given category so that a JOIN clauses would produce duplicates
item.update(item_category: item_category_2, value_in_cents: 50)

get distributions_path, params: params

page = Nokogiri::HTML(response.body)
distribution_rows = page.css("table tbody tr")

expect(distribution_rows.count).to eq(1)
end
end
end

Expand Down

0 comments on commit 242e518

Please sign in to comment.