Skip to content

Commit d5c4aed

Browse files
author
Olexii Kasianenko
committed
Remove unused inventory storage methods and related tests for cleaner code
1 parent 6e923a6 commit d5c4aed

File tree

6 files changed

+55
-125
lines changed

6 files changed

+55
-125
lines changed

app/controllers/storage_locations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def filter_params
163163
end
164164

165165
def date_range
166-
return unless filter_params[:date_range].present?
166+
return if filter_params[:date_range].blank?
167167

168168
date_range = filter_params[:date_range].split(" - ")
169169
start_date = Date.parse(date_range[0])

app/models/item.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Item < ApplicationRecord
6969
scope :period_supplies, -> {
7070
where(reporting_category: [:pads, :tampons, :period_liners, :period_underwear, :period_other])
7171
}
72-
scope :created_between, ->(start_date, end_date) { where(created_at: start_date..end_date) }
72+
7373
enum :reporting_category, {
7474
adult_incontinence: "adult_incontinence",
7575
cloth_diapers: "cloth_diapers",
@@ -193,18 +193,6 @@ def sync_request_units!(unit_ids)
193193
end
194194
end
195195

196-
def quantity_in_storage(storage_location_id)
197-
line_items.inventory_in_storage(storage_location_id).sum(:quantity)
198-
end
199-
200-
def quantity_out_storage(storage_location_id)
201-
line_items.inventory_out_storage(storage_location_id).sum(:quantity)
202-
end
203-
204-
def quantity_change(storage_location_id)
205-
quantity_in_storage(storage_location_id) - quantity_out_storage(storage_location_id)
206-
end
207-
208196
private
209197

210198
def set_default_distribution_quantity

app/models/line_item.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,6 @@ class LineItem < ApplicationRecord
2525

2626
scope :active, -> { joins(:item).where(items: { active: true }) }
2727

28-
scope :inventory_in_storage, ->(storage_location_id) do
29-
joins("
30-
LEFT OUTER JOIN donations ON donations.id = line_items.itemizable_id AND line_items.itemizable_type = 'Donation'
31-
LEFT OUTER JOIN purchases ON purchases.id = line_items.itemizable_id AND line_items.itemizable_type = 'Purchase'
32-
LEFT OUTER JOIN adjustments ON adjustments.id = line_items.itemizable_id AND line_items.itemizable_type = 'Adjustment'
33-
LEFT OUTER JOIN transfers ON transfers.id = line_items.itemizable_id AND line_items.itemizable_type = 'Transfer'")
34-
.where("donations.storage_location_id = :storage_location_id OR
35-
purchases.storage_location_id = :storage_location_id OR
36-
(adjustments.storage_location_id = :storage_location_id and line_items.quantity < 0) OR
37-
transfers.to_id = :storage_location_id", storage_location_id: storage_location_id)
38-
end
39-
40-
scope :inventory_out_storage, ->(storage_location_id) do
41-
joins("
42-
LEFT OUTER JOIN distributions ON distributions.id = line_items.itemizable_id AND line_items.itemizable_type = 'Distribution'
43-
LEFT OUTER JOIN adjustments ON adjustments.id = line_items.itemizable_id AND line_items.itemizable_type = 'Adjustment'
44-
LEFT OUTER JOIN transfers ON transfers.id = line_items.itemizable_id AND line_items.itemizable_type = 'Transfer'")
45-
.where("distributions.storage_location_id = :storage_location_id OR
46-
(adjustments.storage_location_id = :storage_location_id and line_items.quantity > 0) OR
47-
transfers.from_id = :storage_location_id", storage_location_id: storage_location_id)
48-
end
49-
5028
delegate :name, to: :item
5129

5230
# Used in a distribution that was initialized from a request. The `item_request` will be

spec/models/item_spec.rb

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,6 @@
175175
expect(ai_items).to_not include(child_disposable_item, child_cloth_item, liner_item)
176176
end
177177
end
178-
179-
describe "->created_between" do
180-
let!(:start_date) { 2.days.ago }
181-
let!(:end_date) { 1.day.ago }
182-
let!(:item1) { create(:item, created_at: start_date, organization:) }
183-
let!(:item2) { create(:item, created_at: end_date, organization:) }
184-
let!(:item3) { create(:item, created_at: 3.days.ago, organization:) }
185-
let(:items_in_range) { Item.created_between(start_date, end_date) }
186-
187-
it "returns items created within the specified date range" do
188-
expect(items_in_range.count).to eq(2)
189-
expect(items_in_range).to include(item1, item2)
190-
expect(items_in_range).to_not include(item3)
191-
end
192-
end
193178
end
194179

195180
describe "->period_supplies" do
@@ -402,23 +387,6 @@
402387
end
403388
end
404389
end
405-
406-
describe "#quantity_change" do
407-
let(:storage_location) { create(:storage_location) }
408-
let(:item) { create(:item) }
409-
410-
before do
411-
create(:donation, :with_items, item: item, item_quantity: 5, storage_location: storage_location)
412-
create(:purchase, :with_items, item: item, item_quantity: 10, storage_location: storage_location)
413-
create(:distribution, :with_items, item: item, item_quantity: 5, storage_location: storage_location)
414-
create(:adjustment, :with_items, item: item, item_quantity: -2, storage_location: storage_location)
415-
create(:transfer, :with_items, item: item, item_quantity: 3, from: create(:storage_location), to: storage_location)
416-
end
417-
418-
it "returns the total quantity change for the item" do
419-
expect(item.quantity_change(storage_location.id)).to eq(5 + 10 - 5 - 2 + 3)
420-
end
421-
end
422390
end
423391

424392
describe "default_quantity" do

spec/models/line_item_spec.rb

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,48 +55,6 @@
5555
end.to change { described_class.active.size }.by(1)
5656
end
5757
end
58-
59-
describe "->inventory_in_storage" do
60-
let(:storage_location) { create(:storage_location) }
61-
let(:item) { create(:item) }
62-
63-
before do
64-
create(:donation, :with_items, item: item, item_quantity: 5, storage_location: storage_location)
65-
create(:purchase, :with_items, item: item, item_quantity: 10, storage_location: storage_location)
66-
create(:adjustment, :with_items, item: item, item_quantity: -2, storage_location: storage_location)
67-
create(:transfer, :with_items, item: item, item_quantity: 3, from: create(:storage_location), to: storage_location)
68-
end
69-
70-
it "returns line items that are in the specified storage location" do
71-
expect(described_class.inventory_in_storage(storage_location.id).count).to eq(4)
72-
end
73-
74-
it "returns an empty collection if no line items are in the specified storage location" do
75-
other_storage_location = create(:storage_location)
76-
expect(described_class.inventory_in_storage(other_storage_location.id)).to be_empty
77-
end
78-
end
79-
80-
describe "->inventory_out_storage" do
81-
let(:storage_location) { create(:storage_location) }
82-
let(:item) { create(:item) }
83-
84-
before do
85-
create(:donation, :with_items, item: item, item_quantity: 10, storage_location: storage_location)
86-
create(:distribution, :with_items, item: item, item_quantity: 5, storage_location: storage_location)
87-
create(:adjustment, :with_items, item: item, item_quantity: -2, storage_location: storage_location)
88-
create(:transfer, :with_items, item: item, item_quantity: 3, from: storage_location, to: create(:storage_location))
89-
end
90-
91-
it "returns line items that are out of the specified storage location" do
92-
expect(described_class.inventory_out_storage(storage_location.id).count).to eq(2)
93-
end
94-
95-
it "returns an empty collection if no line items are out of the specified storage location" do
96-
other_storage_location = create(:storage_location)
97-
expect(described_class.inventory_out_storage(other_storage_location.id)).to be_empty
98-
end
99-
end
10058
end
10159

10260
describe 'Methods >' do

spec/system/storage_location_system_spec.rb

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,30 @@
184184
context "when viewing an existing storage location" do
185185
let(:items) { create_list(:item, 2) }
186186
let!(:storage_location) { create(:storage_location, name: "here") }
187+
let(:result) do
188+
[
189+
{
190+
item_id: items[0].id,
191+
item_name: items[0].name,
192+
quantity_in: 10,
193+
quantity_out: 5,
194+
change: 5,
195+
total_quantity_in: 16,
196+
total_quantity_out: 7,
197+
total_change: 9
198+
},
199+
{
200+
item_id: items[1].id,
201+
item_name: items[1].name,
202+
quantity_in: 6,
203+
quantity_out: 2,
204+
change: 4,
205+
total_quantity_in: 16,
206+
total_quantity_out: 7,
207+
total_change: 9
208+
}
209+
].map(&:with_indifferent_access)
210+
end
187211
subject { storage_location_path(storage_location.id) }
188212

189213
context "Inventory Flow Tab" do
@@ -200,27 +224,41 @@
200224

201225
it "shows the inventory flow for the storage location" do
202226
within("#custom-tabs-inventory-flow table tbody") do
203-
items.each do |item|
204-
row = find(:css, "tr[id='#{item.id}']")
205-
change_column_css = item.quantity_change(storage_location).negative? ? "td.modal-body-warning-text" : "td"
206-
expect(row).to have_link(item.name, href: item_path(item.id))
207-
expect(row).to have_css("td", text: item.quantity_in_storage(storage_location.id))
208-
expect(row).to have_css("td", text: item.quantity_out_storage(storage_location.id))
209-
expect(row).to have_css(change_column_css, text: item.quantity_change(storage_location.id))
227+
result.each do |item|
228+
row = find(:css, "tr[id='#{item[:item_id]}']")
229+
change_column_css = item[:change].negative? ? "td.modal-body-warning-text" : "td"
230+
expect(row).to have_link(item[:name], href: item_path(item[:item_id]))
231+
expect(row).to have_css("td", text: item[:quantity_in])
232+
expect(row).to have_css("td", text: item[:quantity_out])
233+
expect(row).to have_css(change_column_css, text: item[:change])
210234
end
211235
end
212236
within("#custom-tabs-inventory-flow table tfoot") do
213237
expect(page).to have_css("td", text: "Total")
214-
expect(page).to have_css("td", text: items.sum { |item| item.quantity_in_storage(storage_location.id) })
215-
expect(page).to have_css("td", text: items.sum { |item| item.quantity_out_storage(storage_location.id) })
216-
expect(page).to have_css("td", text: items.sum { |item| item.quantity_change(storage_location.id) })
238+
expect(page).to have_css("td", text: result.first[:total_quantity_in])
239+
expect(page).to have_css("td", text: result.first[:total_quantity_out])
240+
expect(page).to have_css("td", text: result.first[:total_change])
217241
end
218242
end
219243

220244
context "date range filter" do
221245
let!(:start_date) { 2.days.ago }
222246
let!(:end_date) { 1.day.ago }
223247
let!(:item) { create(:item, name: "Filtered Item", created_at: start_date) }
248+
let(:result) do
249+
[
250+
{
251+
item_id: item.id,
252+
item_name: item.name,
253+
quantity_in: 10,
254+
quantity_out: 0,
255+
change: 10,
256+
total_quantity_in: 10,
257+
total_quantity_out: 0,
258+
total_change: 10
259+
}
260+
].map(&:with_indifferent_access)
261+
end
224262
before do
225263
create(:donation, :with_items, item: item, item_quantity: 10, storage_location: storage_location)
226264
fill_in "filters[date_range]", with: "#{start_date} - #{end_date}"
@@ -231,11 +269,11 @@
231269
it "filters the inventory flow by date range" do
232270
within("#custom-tabs-inventory-flow table tbody") do
233271
expect(page).to have_css("tr", count: 1)
234-
row = find(:css, "tr[id='#{item.id}']")
235-
expect(row).to have_link(item.name, href: item_path(item.id))
236-
expect(row).to have_css("td", text: item.quantity_in_storage(storage_location.id))
237-
expect(row).to have_css("td", text: item.quantity_out_storage(storage_location.id))
238-
expect(row).to have_css("td", text: item.quantity_change(storage_location.id))
272+
row = find(:css, "tr[id='#{result.first[:item_id]}']")
273+
expect(row).to have_link(result.first[:name], href: item_path(result.first[:item_id]))
274+
expect(row).to have_css("td", text: result.first[:quantity_in])
275+
expect(row).to have_css("td", text: result.first[:quantity_out])
276+
expect(row).to have_css("td", text: result.first[:change])
239277
end
240278
end
241279
end

0 commit comments

Comments
 (0)