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 %> +
| Date | Type | Added | Level after | Product | Tach | Notes | |
|---|---|---|---|---|---|---|---|
| <%= 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 %> | +
+ 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 @@| Date | Type | Added | Level after | Product | Tach | Notes | |
|---|---|---|---|---|---|---|---|
| <%= 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 %> | -