Skip to content

Commit 9b888d2

Browse files
committed
Move Transfer.storage_locations_transferred_to/from_in to StorageLocation model
1 parent c1fec92 commit 9b888d2

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
@@ -35,11 +35,11 @@ class StorageLocation < ApplicationRecord
3535
has_many :items, through: :inventory_items
3636
has_many :transfers_from, class_name: "Transfer",
3737
inverse_of: :from,
38-
foreign_key: :id,
38+
foreign_key: :from_id,
3939
dependent: :destroy
4040
has_many :transfers_to, class_name: "Transfer",
4141
inverse_of: :to,
42-
foreign_key: :id,
42+
foreign_key: :to_id,
4343
dependent: :destroy
4444
has_many :kit_allocations, dependent: :destroy
4545

@@ -77,6 +77,14 @@ def self.items_inventoried(organization, inventory = nil)
7777
end
7878
end
7979

80+
def self.with_transfers_to(organization)
81+
joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name)
82+
end
83+
84+
def self.with_transfers_from(organization)
85+
joins(:transfers_from).where(organization_id: organization.id).distinct.order(:name)
86+
end
87+
8088
def item_total(item_id)
8189
inventory_items.where(item_id: item_id).pick(:quantity) || 0
8290
end

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 :line_items_exist_in_inventory
4234
validate :storage_locations_belong_to_organization

spec/models/storage_location_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,21 @@
280280
expect(storage_location.csv_export_attributes).to eq([name, address, square_footage, warehouse_type, sum, quantity3, quantity2, quantity1])
281281
end
282282
end
283+
284+
describe "self.with_transfers_to and self.with_transfers_from" do
285+
it "returns storage locations with transfers to/from for an organization" do
286+
storage_location1 = create(:storage_location, name: "loc1", organization: organization)
287+
storage_location2 = create(:storage_location, name: "loc2", organization: organization)
288+
storage_location3 = create(:storage_location, name: "loc3", organization: organization)
289+
storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization))
290+
storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization)
291+
create(:transfer, from: storage_location3, to: storage_location1, organization: organization)
292+
create(:transfer, from: storage_location3, to: storage_location2, organization: organization)
293+
create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization)
294+
expect(StorageLocation.with_transfers_to(organization).to_a).to match_array([storage_location1, storage_location2])
295+
expect(StorageLocation.with_transfers_from(organization).to_a).to match_array([storage_location3])
296+
end
297+
end
283298
end
284299

285300
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)