Oil & Fuel: records load 50 at a time, the rest as you scroll (all records reachable; was capped at 200)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-11 20:27:48 -05:00
1 parent 455a26a346
commit 85326d494c
5 files changed
+77 -21

No files matched your search

@@ -3,10 +3,19 @@ class ConsumableRecordsController < ApplicationController
before_action :require_owner!, only: %i[edit update destroy] before_action :require_owner!, only: %i[edit update destroy]
before_action :set_record, only: %i[edit update destroy] before_action :set_record, only: %i[edit update destroy]
RECORDS_PER_PAGE = 50
# Records a page at a time; the rest load as you scroll (each page ends in
# a lazy frame that fetches the next). The charts cover everything.
def index def index
@page = [ params[:page].to_i, 1 ].max
rows = @aircraft.consumable_records.recent_first.offset((@page - 1) * RECORDS_PER_PAGE).limit(RECORDS_PER_PAGE + 1).to_a
@more = rows.size > RECORDS_PER_PAGE
@records = rows.first(RECORDS_PER_PAGE)
return render(partial: "consumable_records/records", locals: { records: @records, page: @page, more: @more }) if turbo_frame_request?
@oil = ConsumptionStats.new(@aircraft, "oil") @oil = ConsumptionStats.new(@aircraft, "oil")
@fuel = ConsumptionStats.new(@aircraft, "fuel") @fuel = ConsumptionStats.new(@aircraft, "fuel")
@records = @aircraft.consumable_records.recent_first.limit(200)
end end
def new def new
@@ -0,0 +1,36 @@
<%# One page of records, in its own frame; a lazy frame after it loads the
next page when it scrolls into view. Each page is its own table, with the
same fixed column widths so the columns line up. Links open in the whole page. %>
<%= turbo_frame_tag "consumable-records-#{page}", target: "_top", class: "block" do %>
<div class="overflow-x-auto">
<table class="table w-full table-fixed">
<colgroup>
<col class="w-28"><col class="w-24"><col class="w-24"><col class="w-24"><col><col class="w-24"><col><col class="w-20">
</colgroup>
<% if page == 1 %>
<thead><tr><th>Date</th><th>Type</th><th class="text-right">Added</th><th class="text-right">Level after</th><th>Product</th><th class="text-right">Tach</th><th>Notes</th><th></th></tr></thead>
<% end %>
<tbody>
<% records.each do |r| %>
<tr class="consumable-record">
<td class="whitespace-nowrap"><%= r.date %></td>
<td><%= tag.span ConsumableRecord::TYPES[r.record_type], class: r.record_type == "oil" ? "badge-orange" : "badge-blue" %></td>
<td class="text-right tabular-nums" data-label="Added"><%= r.quantity_added %> <%= r.unit %></td>
<td class="text-right tabular-nums" data-label="Level after"><%= r.level_after ? "#{r.level_after} #{r.unit}" : "" %></td>
<td class="truncate" data-label="Product"><%= r.consumable_type %></td>
<td class="text-right tabular-nums" data-label="Tach"><%= hours(r.flight_hours) %></td>
<td class="text-xs text-slate-500 break-words"><%= r.notes %><%= " (excluded from average)" if r.excluded_from_averages? %></td>
<td class="text-right"><% if owner? %><%= link_to "Edit", edit_aircraft_consumable_record_path(@aircraft, r), class: "btn-secondary btn-sm" %><% end %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% if more %>
<%= turbo_frame_tag "consumable-records-#{page + 1}", src: aircraft_consumable_records_path(@aircraft, page: page + 1), loading: :lazy, target: "_top", class: "block" do %>
<p class="px-4 py-4 text-center text-sm text-slate-500">
Loading earlier records… <%= link_to "Load more", aircraft_consumable_records_path(@aircraft, page: page + 1), class: "link", data: { turbo_frame: "consumable-records-#{page + 1}" } %>
</p>
<% end %>
<% end %>
<% end %>
+1 -19
View File
@@ -27,25 +27,7 @@
<section class="card mt-5"> <section class="card mt-5">
<div class="card-header"><h2 class="card-title">Records</h2></div> <div class="card-header"><h2 class="card-title">Records</h2></div>
<% if @records.any? %> <% if @records.any? %>
<div class="overflow-x-auto"> <%= render "consumable_records/records", records: @records, page: @page, more: @more %>
<table class="table">
<thead><tr><th>Date</th><th>Type</th><th class="text-right">Added</th><th class="text-right">Level after</th><th>Product</th><th class="text-right">Tach</th><th>Notes</th><th></th></tr></thead>
<tbody>
<% @records.each do |r| %>
<tr>
<td class="whitespace-nowrap"><%= r.date %></td>
<td><%= tag.span ConsumableRecord::TYPES[r.record_type], class: r.record_type == "oil" ? "badge-orange" : "badge-blue" %></td>
<td class="text-right tabular-nums"><%= r.quantity_added %> <%= r.unit %></td>
<td class="text-right tabular-nums"><%= r.level_after ? "#{r.level_after} #{r.unit}" : "" %></td>
<td><%= r.consumable_type %></td>
<td class="text-right tabular-nums"><%= hours(r.flight_hours) %></td>
<td class="text-xs text-slate-500"><%= r.notes %><%= " (excluded from average)" if r.excluded_from_averages? %></td>
<td class="text-right"><% if owner? %><%= link_to "Edit", edit_aircraft_consumable_record_path(@aircraft, r), class: "btn-secondary btn-sm" %><% end %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %> <% else %>
<%= empty_state "No oil or fuel records yet." %> <%= empty_state "No oil or fuel records yet." %>
<% end %> <% end %>
+1 -1
View File
@@ -4,7 +4,7 @@
Oil and fuel added when logging a flight are recorded automatically. The **Oil & Fuel** tab also takes records on their own (**+ Add Oil** / **+ Add Fuel**): quantity added, level after, product and tach time. Oil and fuel added when logging a flight are recorded automatically. The **Oil & Fuel** tab also takes records on their own (**+ Add Oil** / **+ Add Fuel**): quantity added, level after, product and tach time.
It charts consumption and shows averages over the last 20 intervals, ignoring outliers. Tick **Exclude from averages** for an oil change, so the oil added isn't counted as consumption. It charts consumption and shows averages over the last 20 intervals, ignoring outliers. The records list shows the latest 50; older ones load as you scroll down (or with **Load more**). Tick **Exclude from averages** for an oil change, so the oil added isn't counted as consumption.
## Oil analysis ## Oil analysis
@@ -0,0 +1,29 @@
require "test_helper"
# Oil & Fuel records load a page at a time (no more hidden records past 200).
class ConsumableRecordsPagingTest < ActionDispatch::IntegrationTest
setup do
@owner = create_user("owner@example.com")
@a = create_aircraft(owner: @owner, tail_number: "N12345")
per = ConsumableRecordsController::RECORDS_PER_PAGE
(per + 7).times do |i|
@a.consumable_records.create!(record_type: i.even? ? "fuel" : "oil", date: Date.new(2026, 1, 1) + i, quantity_added: 1 + i, flight_hours: 1000 + i)
end
end
test "the first page has the charts and a page of records; the next page comes in a lazy frame" do
sign_in @owner
get aircraft_consumable_records_path(@a)
assert_select "tr.consumable-record", ConsumableRecordsController::RECORDS_PER_PAGE
assert_select "turbo-frame#consumable-records-1[target=_top]"
assert_select "turbo-frame#consumable-records-2[loading=lazy][src=?]", aircraft_consumable_records_path(@a, page: 2)
assert_select "h2", text: "Fuel Burn"
get aircraft_consumable_records_path(@a, page: 2), headers: { "Turbo-Frame" => "consumable-records-2" }
assert_select "turbo-frame#consumable-records-2 tr.consumable-record", 7
assert_select "turbo-frame#consumable-records-3", 0
assert_select "thead", 0 # header only on the first page
assert_select "h2", text: "Fuel Burn", count: 0
assert_includes response.body, "2026-01-01" # the oldest record is reachable
end
end