Skip to content

Commit

Permalink
Change with_transfers_to/from class methods to be scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
coalest committed Nov 29, 2024
1 parent 851d26d commit 16469cf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
16 changes: 9 additions & 7 deletions app/models/storage_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -72,15 +78,11 @@ 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)
# @return [Integer]
def item_total(item_id)
inventory_items.where(item_id: item_id).pick(:quantity) || 0
end

# @return [Integer]
def size
View::Inventory.items_for_location(self).map(&:quantity).sum
end
Expand Down
53 changes: 53 additions & 0 deletions spec/models/storage_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -183,6 +209,33 @@
end
end

describe "csv_export_attributes" do
it "returns an array of storage location attributes, followed by inventory item quantities that are sorted by alphabetized item names" do
item1 = create(:item, name: "C")
item2 = create(:item, name: "B")
item3 = create(:item, name: "A")
inactive_item = create(:item, name: "inactive item", active: false)
name = "New Storage Location"
address = "1500 Remount Road, Front Royal, VA 22630"
warehouse_type = "Warehouse with loading bay"
square_footage = rand(1000..10000)
storage_location = create(:storage_location, name: name, address: address, warehouse_type: warehouse_type, square_footage: square_footage)
quantity1 = rand(100..1000)
quantity2 = rand(100..1000)
quantity3 = rand(100..1000)
TestInventory.create_inventory(storage_location.organization, {
storage_location.id => {
item1.id => quantity1,
item2.id => quantity2,
item3.id => quantity3,
inactive_item.id => 1
}
})
sum = quantity1 + quantity2 + quantity3
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)
Expand Down

0 comments on commit 16469cf

Please sign in to comment.