Skip to content

Commit 76abbcb

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

File tree

5 files changed

+57
-27
lines changed

5 files changed

+57
-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

+13-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,17 @@ def items
7272
View::Inventory.items_for_location(self).map(&:db_item)
7373
end
7474

75+
def item_total(item_id)
76+
inventory_items.where(item_id: item_id).pick(:quantity) || 0
77+
end
78+
def self.with_transfers_to(organization)
79+
joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name)
80+
end
81+
82+
def self.with_transfers_from(organization)
83+
joins(:transfers_from).where(organization_id: organization.id).distinct.order(:name)
84+
end
85+
7586
# @return [Integer]
7687
def size
7788
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

+42
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,48 @@
182182
expect(storage_location.longitude).not_to eq(nil)
183183
end
184184
end
185+
186+
describe "csv_export_attributes" do
187+
it "returns an array of storage location attributes, followed by inventory item quantities that are sorted by alphabetized item names" do
188+
item1 = create(:item, name: "C")
189+
item2 = create(:item, name: "B")
190+
item3 = create(:item, name: "A")
191+
inactive_item = create(:item, name: "inactive item", active: false)
192+
name = "New Storage Location"
193+
address = "1500 Remount Road, Front Royal, VA 22630"
194+
warehouse_type = "Warehouse with loading bay"
195+
square_footage = rand(1000..10000)
196+
storage_location = create(:storage_location, name: name, address: address, warehouse_type: warehouse_type, square_footage: square_footage)
197+
quantity1 = rand(100..1000)
198+
quantity2 = rand(100..1000)
199+
quantity3 = rand(100..1000)
200+
TestInventory.create_inventory(storage_location.organization, {
201+
storage_location.id => {
202+
item1.id => quantity1,
203+
item2.id => quantity2,
204+
item3.id => quantity3,
205+
inactive_item.id => 1
206+
}
207+
})
208+
sum = quantity1 + quantity2 + quantity3
209+
expect(storage_location.csv_export_attributes).to eq([name, address, square_footage, warehouse_type, sum, quantity3, quantity2, quantity1])
210+
end
211+
end
212+
213+
describe "self.with_transfers_to and self.with_transfers_from" do
214+
it "returns storage locations with transfers to/from for an organization" do
215+
storage_location1 = create(:storage_location, name: "loc1", organization: organization)
216+
storage_location2 = create(:storage_location, name: "loc2", organization: organization)
217+
storage_location3 = create(:storage_location, name: "loc3", organization: organization)
218+
storage_location4 = create(:storage_location, name: "loc4", organization: create(:organization))
219+
storage_location5 = create(:storage_location, name: "loc5", organization: storage_location4.organization)
220+
create(:transfer, from: storage_location3, to: storage_location1, organization: organization)
221+
create(:transfer, from: storage_location3, to: storage_location2, organization: organization)
222+
create(:transfer, from: storage_location5, to: storage_location4, organization: storage_location4.organization)
223+
expect(StorageLocation.with_transfers_to(organization).to_a).to match_array([storage_location1, storage_location2])
224+
expect(StorageLocation.with_transfers_from(organization).to_a).to match_array([storage_location3])
225+
end
226+
end
185227
end
186228

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