From f2100cd2088120649b037dfa8107f6352c7a9514 Mon Sep 17 00:00:00 2001 From: Cory Streiff Date: Thu, 28 Nov 2024 16:17:16 +0100 Subject: [PATCH] Change with_transfers_to/from class methods to be scopes --- app/models/storage_location.rb | 15 ++++---- spec/models/storage_location_spec.rb | 51 ++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/app/models/storage_location.rb b/app/models/storage_location.rb index e2735d9b91..b08e616467 100644 --- a/app/models/storage_location.rb +++ b/app/models/storage_location.rb @@ -55,6 +55,12 @@ class StorageLocation < ApplicationRecord scope :alphabetized, -> { order(:name) } scope :for_csv_export, ->(organization, *) { where(organization: organization) } scope :active_locations, -> { where(discarded_at: nil) } + scope :with_transfers_to, ->(organization) { + joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name) + } + scope :with_transfers_from, ->(organization) { + joins(:transfers_from).where(organization_id: organization.id).distinct.order(:name) + } # @param organization [Organization] # @param inventory [View::Inventory] @@ -72,18 +78,11 @@ def items View::Inventory.items_for_location(self).map(&:db_item) end + # @return [Integer] def item_total(item_id) inventory_items.where(item_id: item_id).pick(:quantity) || 0 end - def self.with_transfers_to(organization) - joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name) - end - def self.with_transfers_from(organization) - joins(:transfers_from).where(organization_id: organization.id).distinct.order(:name) - end - - # @return [Integer] def size View::Inventory.items_for_location(self).map(&:quantity).sum end diff --git a/spec/models/storage_location_spec.rb b/spec/models/storage_location_spec.rb index 1d33b0bec4..f2253b2ffd 100644 --- a/spec/models/storage_location_spec.rb +++ b/spec/models/storage_location_spec.rb @@ -45,6 +45,16 @@ end context "Filtering >" do + it "->containing yields only inventories that have that item" do + item = create(:item) + item2 = create(:item) + storage_location = create(:storage_location, :with_items, item: item, item_quantity: 5) + create(:storage_location, :with_items, item: item2, item_quantity: 5) + results = StorageLocation.containing(item.id) + expect(results.length).to eq(1) + expect(results.first).to eq(storage_location) + end + it "->active_locations yields only storage locations that haven't been discarded" do create(:storage_location, name: "Active Location") create(:storage_location, name: "Inactive Location", discarded_at: Time.zone.now) @@ -52,6 +62,32 @@ expect(results.length).to eq(1) expect(results.first.discarded_at).to be_nil end + + it "->with_transfers_to yields storage locations with transfers to an organization" do + storage_location1 = create(:storage_location, name: "loc1", organization: organization) + storage_location2 = create(:storage_location, name: "loc2", organization: organization) + storage_location3 = create(:storage_location, name: "loc3", organization: organization) + storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization)) + storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization) + create(:transfer, from: storage_location3, to: storage_location1, organization: organization) + create(:transfer, from: storage_location3, to: storage_location2, organization: organization) + create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization) + + expect(StorageLocation.with_transfers_to(organization).to_a).to match_array([storage_location1, storage_location2]) + end + + it "->with_transfers_from yields storage locations with transfers from an organization" do + storage_location1 = create(:storage_location, name: "loc1", organization: organization) + storage_location2 = create(:storage_location, name: "loc2", organization: organization) + storage_location3 = create(:storage_location, name: "loc3", organization: organization) + storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization)) + storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization) + create(:transfer, from: storage_location3, to: storage_location1, organization: organization) + create(:transfer, from: storage_location3, to: storage_location2, organization: organization) + create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization) + + expect(StorageLocation.with_transfers_from(organization).to_a).to match_array([storage_location3]) + end end context "Methods >" do @@ -209,21 +245,6 @@ expect(storage_location.csv_export_attributes).to eq([name, address, square_footage, warehouse_type, sum, quantity3, quantity2, quantity1]) end end - - describe "self.with_transfers_to and self.with_transfers_from" do - it "returns storage locations with transfers to/from for an organization" do - storage_location1 = create(:storage_location, name: "loc1", organization: organization) - storage_location2 = create(:storage_location, name: "loc2", organization: organization) - storage_location3 = create(:storage_location, name: "loc3", organization: organization) - storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization)) - storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization) - create(:transfer, from: storage_location3, to: storage_location1, organization: organization) - create(:transfer, from: storage_location3, to: storage_location2, organization: organization) - create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization) - expect(StorageLocation.with_transfers_to(organization).to_a).to match_array([storage_location1, storage_location2]) - expect(StorageLocation.with_transfers_from(organization).to_a).to match_array([storage_location3]) - end - end end describe "versioning" do