diff --git a/app/controllers/consumable_records_controller.rb b/app/controllers/consumable_records_controller.rb index 94f1e1d..adafe25 100644 --- a/app/controllers/consumable_records_controller.rb +++ b/app/controllers/consumable_records_controller.rb @@ -3,10 +3,19 @@ class ConsumableRecordsController < ApplicationController before_action :require_owner!, 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 + @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") @fuel = ConsumptionStats.new(@aircraft, "fuel") - @records = @aircraft.consumable_records.recent_first.limit(200) end def new diff --git a/app/views/consumable_records/_records.html.erb b/app/views/consumable_records/_records.html.erb new file mode 100644 index 0000000..11221f3 --- /dev/null +++ b/app/views/consumable_records/_records.html.erb @@ -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 %> +
+ + + + + <% if page == 1 %> + + <% end %> + + <% records.each do |r| %> + + + + + + + + + + + <% end %> + +
DateTypeAddedLevel afterProductTachNotes
<%= r.date %><%= tag.span ConsumableRecord::TYPES[r.record_type], class: r.record_type == "oil" ? "badge-orange" : "badge-blue" %><%= r.quantity_added %> <%= r.unit %><%= r.level_after ? "#{r.level_after} #{r.unit}" : "" %><%= r.consumable_type %><%= hours(r.flight_hours) %><%= r.notes %><%= " (excluded from average)" if r.excluded_from_averages? %><% if owner? %><%= link_to "Edit", edit_aircraft_consumable_record_path(@aircraft, r), class: "btn-secondary btn-sm" %><% end %>
+
+ <% 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 %> +

+ Loading earlier records… <%= link_to "Load more", aircraft_consumable_records_path(@aircraft, page: page + 1), class: "link", data: { turbo_frame: "consumable-records-#{page + 1}" } %> +

+ <% end %> + <% end %> +<% end %> diff --git a/app/views/consumable_records/index.html.erb b/app/views/consumable_records/index.html.erb index bb1bd42..03e7425 100644 --- a/app/views/consumable_records/index.html.erb +++ b/app/views/consumable_records/index.html.erb @@ -27,25 +27,7 @@

Records

<% if @records.any? %> -
- - - - <% @records.each do |r| %> - - - - - - - - - - - <% end %> - -
DateTypeAddedLevel afterProductTachNotes
<%= r.date %><%= tag.span ConsumableRecord::TYPES[r.record_type], class: r.record_type == "oil" ? "badge-orange" : "badge-blue" %><%= r.quantity_added %> <%= r.unit %><%= r.level_after ? "#{r.level_after} #{r.unit}" : "" %><%= r.consumable_type %><%= hours(r.flight_hours) %><%= r.notes %><%= " (excluded from average)" if r.excluded_from_averages? %><% if owner? %><%= link_to "Edit", edit_aircraft_consumable_record_path(@aircraft, r), class: "btn-secondary btn-sm" %><% end %>
-
+ <%= render "consumable_records/records", records: @records, page: @page, more: @more %> <% else %> <%= empty_state "No oil or fuel records yet." %> <% end %> diff --git a/docs/guide/oil-and-fuel.md b/docs/guide/oil-and-fuel.md index 4ef8c32..15bab45 100644 --- a/docs/guide/oil-and-fuel.md +++ b/docs/guide/oil-and-fuel.md @@ -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. -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 diff --git a/test/integration/consumable_records_paging_test.rb b/test/integration/consumable_records_paging_test.rb new file mode 100644 index 0000000..c8b81e4 --- /dev/null +++ b/test/integration/consumable_records_paging_test.rb @@ -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