From 851d26d1a3e03ebb5701c2076aa54a2c283aff8b Mon Sep 17 00:00:00 2001 From: Cory Streiff Date: Fri, 29 Nov 2024 10:32:44 +0100 Subject: [PATCH 1/2] Move Transfer.storage_locations_transferred_to/from_in to StorageLocation model --- app/controllers/transfers_controller.rb | 4 ++-- app/models/storage_location.rb | 12 ++++++++++-- app/models/transfer.rb | 8 -------- spec/models/storage_location_spec.rb | 15 +++++++++++++++ spec/models/transfer_spec.rb | 15 --------------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/app/controllers/transfers_controller.rb b/app/controllers/transfers_controller.rb index e227e16990..e94949a272 100644 --- a/app/controllers/transfers_controller.rb +++ b/app/controllers/transfers_controller.rb @@ -11,8 +11,8 @@ def index .during(helpers.selected_range) @selected_from = filter_params[:from_location] @selected_to = filter_params[:to_location] - @from_storage_locations = Transfer.storage_locations_transferred_from_in(current_organization) - @to_storage_locations = Transfer.storage_locations_transferred_to_in(current_organization) + @from_storage_locations = StorageLocation.with_transfers_from(current_organization) + @to_storage_locations = StorageLocation.with_transfers_to(current_organization) respond_to do |format| format.html format.csv { send_data Transfer.generate_csv(@transfers), filename: "Transfers-#{Time.zone.today}.csv" } diff --git a/app/models/storage_location.rb b/app/models/storage_location.rb index 6ad65c7fa9..48f4798985 100644 --- a/app/models/storage_location.rb +++ b/app/models/storage_location.rb @@ -34,11 +34,11 @@ class StorageLocation < ApplicationRecord has_many :distributions, dependent: :destroy has_many :transfers_from, class_name: "Transfer", inverse_of: :from, - foreign_key: :id, + foreign_key: :from_id, dependent: :destroy has_many :transfers_to, class_name: "Transfer", inverse_of: :to, - foreign_key: :id, + foreign_key: :to_id, dependent: :destroy has_many :kit_allocations, dependent: :destroy @@ -72,6 +72,14 @@ def items View::Inventory.items_for_location(self).map(&:db_item) 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 diff --git a/app/models/transfer.rb b/app/models/transfer.rb index 3945ac776f..4237f5fef0 100644 --- a/app/models/transfer.rb +++ b/app/models/transfer.rb @@ -29,14 +29,6 @@ class Transfer < ApplicationRecord } scope :during, ->(range) { where(created_at: range) } - def self.storage_locations_transferred_to_in(organization) - includes(:to).where(organization_id: organization.id).distinct(:to_id).collect(&:to).uniq.sort_by(&:name) - end - - def self.storage_locations_transferred_from_in(organization) - includes(:from).where(organization_id: organization.id).distinct(:from_id).collect(&:from).uniq.sort_by(&:name) - end - validates :from, :to, :organization, presence: true validate :storage_locations_belong_to_organization validate :storage_locations_must_be_different diff --git a/spec/models/storage_location_spec.rb b/spec/models/storage_location_spec.rb index 332a1eed0b..6b21885a53 100644 --- a/spec/models/storage_location_spec.rb +++ b/spec/models/storage_location_spec.rb @@ -182,6 +182,21 @@ expect(storage_location.longitude).not_to eq(nil) 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 diff --git a/spec/models/transfer_spec.rb b/spec/models/transfer_spec.rb index 2ca192938d..e3f9d17151 100644 --- a/spec/models/transfer_spec.rb +++ b/spec/models/transfer_spec.rb @@ -69,21 +69,6 @@ end end - context "Methods >" do - it "`self.storage_locations_transferred_to` and `..._from` constrains appropriately" 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(Transfer.storage_locations_transferred_to_in(organization).to_a).to match_array([storage_location1, storage_location2]) - expect(Transfer.storage_locations_transferred_from_in(organization).to_a).to match_array([storage_location3]) - end - end - describe "versioning" do it { is_expected.to be_versioned } end From 1c570328788c40407f81e8e501e93a13046bb6de Mon Sep 17 00:00:00 2001 From: Cory Streiff Date: Fri, 29 Nov 2024 10:39:43 +0100 Subject: [PATCH 2/2] Change with_transfers_to/from class methods to be scopes --- app/models/storage_location.rb | 14 ++++------ spec/models/storage_location_spec.rb | 41 ++++++++++++++++++---------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/app/models/storage_location.rb b/app/models/storage_location.rb index 48f4798985..94f2a0a23e 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,14 +78,6 @@ def items View::Inventory.items_for_location(self).map(&:db_item) 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 diff --git a/spec/models/storage_location_spec.rb b/spec/models/storage_location_spec.rb index 6b21885a53..5ebdb20ba2 100644 --- a/spec/models/storage_location_spec.rb +++ b/spec/models/storage_location_spec.rb @@ -52,6 +52,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 @@ -182,21 +208,6 @@ expect(storage_location.longitude).not_to eq(nil) 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