Skip to content

Commit dbb1d78

Browse files
awwaiidclaude
andcommitted
Inventory Flow: fix item links in the lazy frame, address review findings
- Item links in the flow table navigated the turbo frame instead of the page, replacing the table with "Content missing". Target _top on the frame, with a system spec that clicks a link. - A malformed filters param (?filters=x) raised a 500 on the storage location page and anywhere else the date range picker is used. DateRangeHelper now ignores a non-hash filters param, and the show page's tab and date range filters are computed in the controller. - SnapshotEvent#editable? is false, matching the doc comment. - Look up item names through the organization's items, and render a missing item's name without a broken link. - Compare event classes with is_a? instead of type strings. - System spec compares whole rows and totals (the old substring matches passed with the wrong numbers), and checks the filter keeps the tab. - Query specs for a transfer seen from the receiving location, kit deallocation, legacy UpdateExistingEvent edits, distribution/transfer/ purchase destroys, and a multi-month history that must add up per row, chain month to month, and end at the current inventory. - editable? spec covers every event type. - User guide describes the Inventory Flow tab instead of the old Coming In / Going Out tabs; remove their screenshots. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014FSHhMV2oCu9RE8hpg29ey
1 parent 9683659 commit dbb1d78

15 files changed

Lines changed: 245 additions & 99 deletions

‎app/controllers/storage_locations_controller.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def show
5858
setup_date_range_picker
5959
@storage_location = current_organization.storage_locations.find(params[:id])
6060
version_date = params[:version_date].presence&.to_date
61+
@show_flow_tab = params[:tab] == "flow"
62+
@date_range_filters = date_range_filters
6163
if View::Inventory.within_snapshot?(current_organization.id, version_date)
6264
@inventory = View::Inventory.new(current_organization.id, event_time: version_date)
6365
else
@@ -158,6 +160,14 @@ def include_omitted_items(existing_item_ids = [])
158160
end
159161
end
160162

163+
# @return [Hash] the Inventory Flow tab's date range filters, if any
164+
def date_range_filters
165+
filters = params[:filters]
166+
return {} unless filters.is_a?(ActionController::Parameters)
167+
168+
filters.permit(:date_range, :date_range_label).to_h.compact_blank
169+
end
170+
161171
helper_method \
162172
def filter_params
163173
return {} unless params.key?(:filters)

‎app/events/snapshot_event.rb‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ def self.publish(organization)
1919
data: inventory
2020
)
2121
end
22+
23+
# A snapshot replaces the whole inventory rather than versioning a record.
24+
def editable?
25+
false
26+
end
2227
end

‎app/helpers/date_range_helper.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Encapsulates methods used on the Dashboard that need some business logic
22
module DateRangeHelper
33
def date_range_params
4-
params.dig(:filters, :date_range).presence || default_date
4+
date_range_filter(:date_range).presence || default_date
55
end
66

77
def date_range_label
8-
case (params.dig(:filters, :date_range_label).presence || "this year").downcase
8+
case (date_range_filter(:date_range_label).presence || "this year").downcase
99
when "today"
1010
"today"
1111
when "yesterday"
@@ -46,6 +46,13 @@ def selected_range
4646
(start_date.beginning_of_day)..(end_date.end_of_day)
4747
end
4848

49+
# @param key [Symbol]
50+
# @return [String, nil] the filter's value, or nil if params[:filters] isn't a hash
51+
def date_range_filter(key)
52+
filters = params[:filters]
53+
filters[key] if filters.respond_to?(:key?)
54+
end
55+
4956
def selected_range_described
5057
start_date, end_date = selected_interval
5158
if start_date == Time.zone.today

‎app/queries/items_flow_query.rb‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
# the record was first created, so a donation of 100 that was later edited to
88
# 150 shows 150 on its original date, and a deleted donation shows nothing.
99
# The exception is records from before event tracking began, whose edits are
10-
# stored as UpdateExistingEvents; those edits are dated when they were made.
10+
# stored as UpdateExistingEvents; those edits are dated when they were made,
11+
# and count as in or out by the sign of the change (so reducing such a
12+
# distribution shows as in).
1113
#
1214
# To do this we replay the inventory once (the same replay used everywhere
1315
# else, so audits, kits and edits are handled exactly as InventoryAggregate
@@ -61,7 +63,7 @@ def empty?
6163
end
6264
end
6365

64-
ADJUSTMENT_TYPES = %w[AdjustmentEvent AuditEvent].freeze
66+
ADJUSTMENT_TYPES = [AdjustmentEvent, AuditEvent].freeze
6567

6668
# @param organization [Organization]
6769
# @param storage_location [StorageLocation]
@@ -83,7 +85,7 @@ def call
8385
changes_by_item = records.flat_map(&:item_changes).group_by(&:item_id)
8486

8587
item_ids = (snapshot_quantities.keys + changes_by_item.keys).uniq
86-
names = Item.where(id: item_ids).pluck(:id, :name).to_h
88+
names = @organization.items.where(id: item_ids).pluck(:id, :name).to_h
8789

8890
rows = item_ids.filter_map do |item_id|
8991
row = build_row(item_id, names[item_id], snapshot_quantities.fetch(item_id, 0), changes_by_item.fetch(item_id, []),
@@ -159,7 +161,7 @@ def replay
159161
end
160162

161163
key = event.editable? ? [event.eventable_type, event.eventable_id] : [:event, event.id]
162-
record = records[key] ||= RecordChanges.new(time: event.event_time, adjustment: ADJUSTMENT_TYPES.include?(event.type))
164+
record = records[key] ||= RecordChanges.new(time: event.event_time, adjustment: ADJUSTMENT_TYPES.any? { |type| event.is_a?(type) })
163165
record.touch(item_ids_here(event)).each do |item_id|
164166
quantity = items[item_id]&.quantity.to_i
165167
record.add(item_id, quantity - quantities[item_id])

‎app/views/storage_locations/_item_row.html.erb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<tr data-item-id="<%= item_row.item_id %>">
2-
<td><%= link_to item_row.item_name, item_path(item_row.item_id) %></td>
2+
<td><%= item_row.item_name ? link_to(item_row.item_name, item_path(item_row.item_id)) : "Unknown item ##{item_row.item_id}" %></td>
33
<td><%= item_row.quantity_start %></td>
44
<td><%= item_row.quantity_in %></td>
55
<td><%= item_row.quantity_out %></td>

‎app/views/storage_locations/inventory_flow.html.erb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%= turbo_frame_tag "inventory_flow" do %>
1+
<%= turbo_frame_tag "inventory_flow", target: "_top" do %>
22
<table class="table">
33
<thead>
44
<tr>

‎app/views/storage_locations/show.html.erb‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,21 @@
6767
<!-- Default box -->
6868
<div class="card card-primary card-outline card-outline-tabs">
6969
<div class="card-header p-0 border-bottom-0">
70-
<% show_flow_tab = params[:tab] == "flow" %>
71-
<% date_range_filters = params.fetch(:filters, {}).permit(:date_range, :date_range_label).to_h.compact_blank %>
7270
<ul class="nav nav-tabs" id="custom-tabs-three-tab" role="tablist">
7371
<li class="nav-item">
74-
<a class="nav-link <%= 'active' unless show_flow_tab %>" id="custom-tabs-inventory-tab" data-bs-toggle="pill" href="#custom-tabs-inventory" role="tab" aria-controls="custom-tabs-inventory" aria-selected="<%= !show_flow_tab %>">Inventory</a>
72+
<a class="nav-link <%= 'active' unless @show_flow_tab %>" id="custom-tabs-inventory-tab" data-bs-toggle="pill" href="#custom-tabs-inventory" role="tab" aria-controls="custom-tabs-inventory" aria-selected="<%= !@show_flow_tab %>">Inventory</a>
7573
</li>
7674
<li class="nav-item">
77-
<a class="nav-link <%= 'active' if show_flow_tab %>" id="custom-tabs-inventory-flow-tab" data-bs-toggle="pill" href="#custom-tabs-inventory-flow" role="tab" aria-controls="custom-tabs-inventory-flow" aria-selected="<%= show_flow_tab %>">Inventory Flow</a>
75+
<a class="nav-link <%= 'active' if @show_flow_tab %>" id="custom-tabs-inventory-flow-tab" data-bs-toggle="pill" href="#custom-tabs-inventory-flow" role="tab" aria-controls="custom-tabs-inventory-flow" aria-selected="<%= @show_flow_tab %>">Inventory Flow</a>
7876
</li>
7977
</ul>
8078
</div>
8179
<div class="card-body">
8280
<div class="tab-content" id="custom-tabs-three-tabContent">
8381

84-
<div class="tab-pane fade <%= 'show active' unless show_flow_tab %>" id="custom-tabs-inventory" role="tabpanel" aria-labelledby="custom-tabs-inventory-tab">
82+
<div class="tab-pane fade <%= 'show active' unless @show_flow_tab %>" id="custom-tabs-inventory" role="tabpanel" aria-labelledby="custom-tabs-inventory-tab">
8583
<%= form_for @storage_location, method: :get do %>
86-
<% date_range_filters.each do |name, value| %>
84+
<% @date_range_filters.each do |name, value| %>
8785
<%= hidden_field_tag "filters[#{name}]", value, id: nil %>
8886
<% end %>
8987
Show Inventory at Date: <%= date_field_tag 'version_date', params[:version_date], min: InventoryItem::EARLIEST_VERSION, autocomplete: "on" %>
@@ -132,7 +130,7 @@
132130
</table>
133131
</div><!-- /.box-body.table-responsive -->
134132

135-
<div class="tab-pane fade <%= 'show active' if show_flow_tab %>" id="custom-tabs-inventory-flow" role="tabpanel" aria-labelledby="custom-tabs-inventory-flow-tab">
133+
<div class="tab-pane fade <%= 'show active' if @show_flow_tab %>" id="custom-tabs-inventory-flow" role="tabpanel" aria-labelledby="custom-tabs-inventory-flow-tab">
136134
<%= form_for @storage_location, method: :get do %>
137135
<%= hidden_field_tag :tab, "flow", id: nil %>
138136
<%= hidden_field_tag :version_date, params[:version_date], id: nil if params[:version_date].present? %>
@@ -148,8 +146,8 @@
148146
</div>
149147
</div>
150148
<% end %>
151-
<%= turbo_frame_tag "inventory_flow", loading: :lazy,
152-
src: inventory_flow_storage_location_path(@storage_location, filters: date_range_filters.presence) do %>
149+
<%= turbo_frame_tag "inventory_flow", loading: :lazy, target: "_top",
150+
src: inventory_flow_storage_location_path(@storage_location, filters: @date_range_filters.presence) do %>
153151
<p class="text-muted">Loading inventory flow&hellip;</p>
154152
<% end %>
155153
</div><!-- /.box-body.table-responsive -->
Binary file not shown.
Binary file not shown.

‎docs/user_guide/bank/inventory_storage_locations.md‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,26 @@ This brings up the New Storage Location page
2323

2424
## Storage location view
2525
To view a Storage Location, click the "View" button beside it in the Storage Location summary page.
26-
The view has three tabs: Inventory, Inventory Coming In, and Inventory Going Out
26+
The view has two tabs: Inventory and Inventory Flow
2727

2828
### Inventory
2929

3030
Here you see the current levels of inventory for the chosen Storage Location. You can see the inventory levels for past dates by choosing the date in "Show Inventory at Date" and clicking "View"
3131
That shows the inventory at the beginning of the day.
3232
![Storage Location - Inventory tab](images/inventory/inventory_storage_location_view_inventory.png)
3333

34-
### Inventory Coming In
35-
This shows the sum of all the inventory coming in ([Purchases](essentials_purchases.md), [Donations](essentials_donations.md), [Adjustments](inventory_adjustments.md), [Transfers](inventory_transfers.md), and [Kit](inventory_kits.md) allocations and de-allocations that add inventory) for the given Storage Location for all time
34+
### Inventory Flow
35+
This shows how the quantity of each item at the Storage Location changed over a date range. Choose the range in "Date Range" and click "Filter". For each item you see:
36+
- Quantity at Start: how much was there at the start of the range
37+
- Quantity In: what came in during the range ([Purchases](essentials_purchases.md), [Donations](essentials_donations.md), [Transfers](inventory_transfers.md) from other Storage Locations, and [Kit](inventory_kits.md) allocations and de-allocations that add inventory)
38+
- Quantity Out: what went out during the range ([Distributions](essentials_distributions.md), [Transfers](inventory_transfers.md) to other Storage Locations, and [Kit](inventory_kits.md) allocations and de-allocations that remove inventory)
39+
- Quantity Adjustment/Audit: the net change from [Adjustments](inventory_adjustments.md) and [Audits](inventory_audits.md) during the range
40+
- Quantity Change: In, minus Out, plus Adjustment/Audit
41+
- Quantity at End: how much was there at the end of the range
3642

37-
![Inventory coming in](images/inventory/inventory_storage_locations_coming_in.png)
43+
Each record is counted on the date it was entered into Human Essentials, not the date on the record itself (for example, a donation's issued date). If a record is changed or deleted later, the change is counted on the date the record was first entered, so the numbers always reflect your records as they are now. This means the Quantity at End for a past date can differ from what the Inventory tab shows for that date.
3844

39-
40-
### Inventory Going Out
41-
This shows the sum of all the inventory going out ([Distributions](essentials_distributions.md), [Adjustments](inventory_adjustments.md), [Transfers](inventory_transfers.md), and [Kit](inventory_kits.md) allocations and de-allocations that remove inventory) for the given Storage Location for all time
42-
43-
![Inventory going out](images/inventory/inventory_storage_locations_going_out.png)
45+
Human Essentials only keeps inventory history from a certain date (shown under the table). If your range starts before then, Quantity at Start and Quantity Change are left blank, and In, Out and Adjustment/Audit only count from that date.
4446

4547
## Editing a Storage Location
4648
You can edit your Storage Location (though it seems a very rare event), by clicking "Edit" beside the Storage Location in the Storage Location Summary Page.

0 commit comments

Comments
 (0)