Skip to content

Commit 89b56e4

Browse files
authored
Merge pull request #4469 from rubyforgood/4433-inventory-history
Show inventory history for pre-event read
2 parents 846dd95 + d54f0cc commit 89b56e4

File tree

5 files changed

+121
-15
lines changed

5 files changed

+121
-15
lines changed

app/controllers/storage_locations_controller.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ def show
7979
@items_in = ItemsInQuery.new(organization: current_organization, storage_location: @storage_location).call
8080
@items_in_total = ItemsInTotalQuery.new(organization: current_organization, storage_location: @storage_location).call
8181
if Event.read_events?(current_organization)
82-
@inventory = View::Inventory.new(current_organization.id, event_time: params[:version_date])
82+
if View::Inventory.within_snapshot?(current_organization.id, params[:version_date])
83+
@inventory = View::Inventory.new(current_organization.id, event_time: params[:version_date])
84+
else
85+
@legacy_inventory = View::Inventory.legacy_inventory_for_storage_location(
86+
current_organization.id,
87+
@storage_location.id,
88+
params[:version_date]
89+
)
90+
end
8391
end
8492

8593
respond_to do |format|

app/models/view/inventory.rb

+25
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ class ViewInventoryItem < EventTypes::EventItem
1313
attr_accessor :inventory, :organization_id
1414
delegate :storage_locations, to: :inventory
1515

16+
# @param event_time [ActiveSupport::TimeWithZone]
17+
# @return [Boolean]
18+
def self.within_snapshot?(organization_id, event_time)
19+
return true if event_time.blank?
20+
21+
event = SnapshotEvent.where(organization_id: organization_id).first
22+
event && event.created_at < event_time
23+
end
24+
25+
# @param organization_id [Integer]
26+
# @param storage_location_id [Integer]
27+
# @param event_time [ActiveSupport::TimeWithZone]
28+
# @return [Array<ViewInventoryItem>]
29+
def self.legacy_inventory_for_storage_location(organization_id, storage_location_id, event_time)
30+
items = Organization.find(organization_id).inventory_items.where(storage_location_id: storage_location_id)
31+
items.map do |item|
32+
ViewInventoryItem.new(
33+
item_id: item.item_id,
34+
quantity: item.paper_trail.version_at(event_time)&.quantity || 0,
35+
storage_location_id: storage_location_id,
36+
db_item: item.item
37+
)
38+
end
39+
end
40+
1641
# @param organization_id [Integer]
1742
# @param event_time [DateTime]
1843
def initialize(organization_id, event_time: nil)

app/views/storage_locations/show.html.erb

+16-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@
102102
<td><%= number_with_delimiter(item.quantity) %></td>
103103
</tr>
104104
<% end %>
105+
<% elsif @legacy_inventory %>
106+
<% @legacy_inventory.each do |item| %>
107+
<tr>
108+
<td><%= link_to item.name, item_path(item.item_id) %></td>
109+
<td><%= number_with_delimiter(item.quantity) %></td>
110+
</tr>
111+
<% end %>
105112
<% else %>
106113
<%= render partial: "inventory_item_row",
107114
collection: @storage_location.inventory_items.joins(:item).where(items: { active: true }),
@@ -111,7 +118,15 @@
111118
<tfoot>
112119
<tr>
113120
<td>Total</td>
114-
<td><%= @inventory ? @inventory.quantity_for(storage_location: @storage_location.id) : @storage_location.size %></td>
121+
<td>
122+
<% if @inventory %>
123+
<%= number_with_delimiter(@inventory.quantity_for(storage_location: @storage_location.id)) %>
124+
<% elsif @legacy_inventory %>
125+
<%= number_with_delimiter(@legacy_inventory.map(&:quantity).sum) %>
126+
<% else %>
127+
<%= number_with_delimiter(@storage_location.size) %>
128+
<% end %>
129+
</td>
115130
</tr>
116131
</tfoot>
117132
</table>

lib/tasks/kill_postgres_connections.rake

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
task :kill_postgres_connections => :environment do
2+
puts "Killing existing Postgres connections - you may be prompted for your computer admin password"
23
db_name = Rails.configuration.database_configuration[Rails.env]['database']
34
sh = <<EOF
45
ps xa \
@@ -12,4 +13,4 @@ EOF
1213
puts "Done killing the connections!"
1314
end
1415

15-
task "db:drop" => :kill_postgres_connections
16+
task "db:drop" => :kill_postgres_connections

spec/requests/storage_locations_requests_spec.rb

+69-12
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,77 @@
235235
let(:inventory_item) { storage_location.inventory_items.first }
236236

237237
context "with a version found" do
238-
it "should show the version specified" do
239-
travel 1.day do
240-
inventory_item.update!(quantity: 100)
238+
context "with events_read on" do
239+
before(:each) { allow(Event).to receive(:read_events?).and_return(true) }
240+
context "before active events" do
241+
it "should show the version specified" do
242+
travel 1.day do
243+
inventory_item.update!(quantity: 100)
244+
end
245+
travel 1.week do
246+
inventory_item.update!(quantity: 300)
247+
end
248+
travel 8.days do
249+
SnapshotEvent.delete_all
250+
SnapshotEvent.publish(organization)
251+
end
252+
travel 2.weeks do
253+
get storage_location_path(storage_location, format: response_format,
254+
version_date: 9.days.ago.to_date.to_fs(:db))
255+
expect(response).to be_successful
256+
expect(response.body).to include("Smithsonian")
257+
expect(response.body).to include("Test Item")
258+
expect(response.body).to include("100")
259+
end
260+
end
241261
end
242-
travel 1.week do
243-
inventory_item.update!(quantity: 300)
262+
263+
context "with active events" do
264+
it 'should show the right version' do
265+
travel 1.day do
266+
TestInventory.create_inventory(organization, {
267+
storage_location.id => {
268+
item.id => 100,
269+
item2.id => 0
270+
}
271+
})
272+
end
273+
travel 1.week do
274+
TestInventory.create_inventory(organization, {
275+
storage_location.id => {
276+
item.id => 300,
277+
item2.id => 0
278+
}
279+
})
280+
end
281+
travel 2.weeks do
282+
get storage_location_path(storage_location, format: response_format,
283+
version_date: 9.days.ago.to_date.to_fs(:db))
284+
expect(response).to be_successful
285+
expect(response.body).to include("Smithsonian")
286+
expect(response.body).to include("Test Item")
287+
expect(response.body).to include("100")
288+
end
289+
end
244290
end
245-
travel 2.weeks do
246-
get storage_location_path(storage_location, format: response_format,
247-
version_date: 9.days.ago.to_date.to_fs(:db))
248-
expect(response).to be_successful
249-
expect(response.body).to include("Smithsonian")
250-
expect(response.body).to include("Test Item")
251-
expect(response.body).to include("100")
291+
end
292+
context "with events_read off" do
293+
before(:each) { allow(Event).to receive(:read_events?).and_return(false) }
294+
it "should show the version specified" do
295+
travel 1.day do
296+
inventory_item.update!(quantity: 100)
297+
end
298+
travel 1.week do
299+
inventory_item.update!(quantity: 300)
300+
end
301+
travel 2.weeks do
302+
get storage_location_path(storage_location, format: response_format,
303+
version_date: 9.days.ago.to_date.to_fs(:db))
304+
expect(response).to be_successful
305+
expect(response.body).to include("Smithsonian")
306+
expect(response.body).to include("Test Item")
307+
expect(response.body).to include("100")
308+
end
252309
end
253310
end
254311
end

0 commit comments

Comments
 (0)