From 242e5184dca48c54fbfcc2bef56d4346c384fc2c Mon Sep 17 00:00:00 2001 From: Cory Streiff Date: Mon, 9 Dec 2024 18:20:05 +0100 Subject: [PATCH] Fix regression in item category filter --- app/models/distribution.rb | 4 ++-- app/services/distribution_totals_service.rb | 2 +- spec/requests/distributions_requests_spec.rb | 12 ++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/models/distribution.rb b/app/models/distribution.rb index dabc9158a5..1d11c25d7b 100644 --- a/app/models/distribution.rb +++ b/app/models/distribution.rb @@ -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) } diff --git a/app/services/distribution_totals_service.rb b/app/services/distribution_totals_service.rb index 81a244c63d..28ad15d58e 100644 --- a/app/services/distribution_totals_service.rb +++ b/app/services/distribution_totals_service.rb @@ -50,7 +50,7 @@ def calculate_quantities(distributions) # # @return [Hash] 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") diff --git a/spec/requests/distributions_requests_spec.rb b/spec/requests/distributions_requests_spec.rb index 4a02942ae9..8ce7533ed6 100644 --- a/spec/requests/distributions_requests_spec.rb +++ b/spec/requests/distributions_requests_spec.rb @@ -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