Skip to content

Commit 851d26d

Browse files
committed
Move Transfer.storage_locations_transferred_to/from_in to StorageLocation model
1 parent c9fad26 commit 851d26d

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

app/controllers/transfers_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def index
1111
.during(helpers.selected_range)
1212
@selected_from = filter_params[:from_location]
1313
@selected_to = filter_params[:to_location]
14-
@from_storage_locations = Transfer.storage_locations_transferred_from_in(current_organization)
15-
@to_storage_locations = Transfer.storage_locations_transferred_to_in(current_organization)
14+
@from_storage_locations = StorageLocation.with_transfers_from(current_organization)
15+
@to_storage_locations = StorageLocation.with_transfers_to(current_organization)
1616
respond_to do |format|
1717
format.html
1818
format.csv { send_data Transfer.generate_csv(@transfers), filename: "Transfers-#{Time.zone.today}.csv" }

app/models/storage_location.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ class StorageLocation < ApplicationRecord
3434
has_many :distributions, dependent: :destroy
3535
has_many :transfers_from, class_name: "Transfer",
3636
inverse_of: :from,
37-
foreign_key: :id,
37+
foreign_key: :from_id,
3838
dependent: :destroy
3939
has_many :transfers_to, class_name: "Transfer",
4040
inverse_of: :to,
41-
foreign_key: :id,
41+
foreign_key: :to_id,
4242
dependent: :destroy
4343
has_many :kit_allocations, dependent: :destroy
4444

@@ -72,6 +72,14 @@ def items
7272
View::Inventory.items_for_location(self).map(&:db_item)
7373
end
7474

75+
def self.with_transfers_to(organization)
76+
joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name)
77+
end
78+
79+
def self.with_transfers_from(organization)
80+
joins(:transfers_from).where(organization_id: organization.id).distinct.order(:name)
81+
end
82+
7583
# @return [Integer]
7684
def size
7785
View::Inventory.items_for_location(self).map(&:quantity).sum

app/models/transfer.rb

-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ class Transfer < ApplicationRecord
2929
}
3030
scope :during, ->(range) { where(created_at: range) }
3131

32-
def self.storage_locations_transferred_to_in(organization)
33-
includes(:to).where(organization_id: organization.id).distinct(:to_id).collect(&:to).uniq.sort_by(&:name)
34-
end
35-
36-
def self.storage_locations_transferred_from_in(organization)
37-
includes(:from).where(organization_id: organization.id).distinct(:from_id).collect(&:from).uniq.sort_by(&:name)
38-
end
39-
4032
validates :from, :to, :organization, presence: true
4133
validate :storage_locations_belong_to_organization
4234
validate :storage_locations_must_be_different

spec/models/storage_location_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@
182182
expect(storage_location.longitude).not_to eq(nil)
183183
end
184184
end
185+
186+
describe "self.with_transfers_to and self.with_transfers_from" do
187+
it "returns storage locations with transfers to/from for an organization" do
188+
storage_location1 = create(:storage_location, name: "loc1", organization: organization)
189+
storage_location2 = create(:storage_location, name: "loc2", organization: organization)
190+
storage_location3 = create(:storage_location, name: "loc3", organization: organization)
191+
storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization))
192+
storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization)
193+
create(:transfer, from: storage_location3, to: storage_location1, organization: organization)
194+
create(:transfer, from: storage_location3, to: storage_location2, organization: organization)
195+
create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization)
196+
expect(StorageLocation.with_transfers_to(organization).to_a).to match_array([storage_location1, storage_location2])
197+
expect(StorageLocation.with_transfers_from(organization).to_a).to match_array([storage_location3])
198+
end
199+
end
185200
end
186201

187202
describe "versioning" do

spec/models/transfer_spec.rb

-15
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,6 @@
6969
end
7070
end
7171

72-
context "Methods >" do
73-
it "`self.storage_locations_transferred_to` and `..._from` constrains appropriately" do
74-
storage_location1 = create(:storage_location, name: "loc1", organization: organization)
75-
storage_location2 = create(:storage_location, name: "loc2", organization: organization)
76-
storage_location3 = create(:storage_location, name: "loc3", organization: organization)
77-
storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization))
78-
storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization)
79-
create(:transfer, from: storage_location3, to: storage_location1, organization: organization)
80-
create(:transfer, from: storage_location3, to: storage_location2, organization: organization)
81-
create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization)
82-
expect(Transfer.storage_locations_transferred_to_in(organization).to_a).to match_array([storage_location1, storage_location2])
83-
expect(Transfer.storage_locations_transferred_from_in(organization).to_a).to match_array([storage_location3])
84-
end
85-
end
86-
8772
describe "versioning" do
8873
it { is_expected.to be_versioned }
8974
end

0 commit comments

Comments
 (0)