From 455a26a3462ada3428b512a1d715124e0238790a Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Wed, 11 Nov 2026 20:22:02 -0500 Subject: [PATCH] Flights tab: load the flight log 30 days at a time, the rest as you scroll (lazy Turbo frames) Co-Authored-By: Claude Opus 5.5 --- app/controllers/flight_logs_controller.rb | 15 ++++++- app/views/flight_logs/_days.html.erb | 54 +++++++++++++++++++++++ app/views/flight_logs/index.html.erb | 47 ++------------------ docs/guide/flights.md | 2 +- test/integration/flight_days_test.rb | 17 +++++++ 5 files changed, 88 insertions(+), 47 deletions(-) create mode 100644 app/views/flight_logs/_days.html.erb diff --git a/app/controllers/flight_logs_controller.rb b/app/controllers/flight_logs_controller.rb index a2f7000..9386dea 100644 --- a/app/controllers/flight_logs_controller.rb +++ b/app/controllers/flight_logs_controller.rb @@ -5,9 +5,20 @@ class FlightLogsController < ApplicationController before_action :require_engine_data_access!, only: %i[attach_engine_data remove_engine_data] helper_method :manage_engine_data? + DAYS_PER_PAGE = 30 + + # The flight log a page of days at a time; the rest load as you scroll + # (each page ends in a lazy frame that fetches the next). def index - @flight_logs = @aircraft.flight_logs.recent_first.includes(:user).with_attached_track_log.with_attached_engine_data - @days = FlightDay.group(@flight_logs) + @page = [ params[:page].to_i, 1 ].max + dates = @aircraft.flight_logs.distinct.order(date: :desc) + .offset((@page - 1) * DAYS_PER_PAGE).limit(DAYS_PER_PAGE + 1).pluck(:date) + @more = dates.size > DAYS_PER_PAGE + logs = @aircraft.flight_logs.where(date: dates.first(DAYS_PER_PAGE)) + .includes(:user).with_attached_track_log.with_attached_engine_data + @days = FlightDay.group(logs) + return render(partial: "flight_logs/days", locals: { days: @days, page: @page, more: @more }) if turbo_frame_request? + @stats = FlightStats.new(@aircraft) end diff --git a/app/views/flight_logs/_days.html.erb b/app/views/flight_logs/_days.html.erb new file mode 100644 index 0000000..c7eb5bd --- /dev/null +++ b/app/views/flight_logs/_days.html.erb @@ -0,0 +1,54 @@ +<%# One page of days. Rendered inside its own frame; the last day is followed + by a lazy frame that loads the next page when it scrolls into view. + Links in it open in the whole page (target _top). %> +<% blank = tag.span("β€”", class: "text-slate-300") %> +<% qty = ->(v) { v ? number_with_precision(v, precision: 2, strip_insignificant_zeros: true) : blank } %> +<%= turbo_frame_tag "flight-days-#{page}", target: "_top", class: "block" do %> + <% days.each do |day| %> +
+ + <%= day.date.strftime("%a %b %-d, %Y") %><%= pluralize(day.flights.size, "flight") %> + + <%= day.route.presence || blank %> + <% if day.track_log? %>πŸ“<% end %> + <% if day.engine_data? %>πŸ“ˆ<% end %> + <% if day.pilots.any? %><%= day.pilots.to_sentence %><% end %> + + Tach <%= day.tach_time ? hours(day.tach_time) : blank %> + Hobbs <%= day.hobbs_time ? hours(day.hobbs_time) : blank %> + Fuel <%= qty.(day.fuel_added) %> + Oil <%= qty.(day.oil_added) %> + + +
+ + + + <% day.flights.each_with_index do |fl, i| %> + + + + + + + + + + <% end %> + +
LegRouteTachHobbsFuel (gal)Oil (qt)Pilot
<%= link_to "Leg #{i + 1}", aircraft_flight_log_path(@aircraft, fl), class: "link" %> + <%= fl.route_summary.presence || blank %> + <% if fl.track_log.attached? %>πŸ“<% end %> + <% if fl.engine_data.attached? %>πŸ“ˆ<% end %> + <%= fl.tach_time ? hours(fl.tach_time) : blank %><%= fl.hobbs_time ? hours(fl.hobbs_time) : blank %><%= qty.(fl.fuel_added) %><%= qty.(fl.oil_added) %><%= fl.user&.display_name || blank %>
+
+
+ <% end %> + <% if more %> + <%= turbo_frame_tag "flight-days-#{page + 1}", src: aircraft_flight_logs_path(@aircraft, page: page + 1), loading: :lazy, target: "_top", class: "block" do %> +

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

+ <% end %> + <% end %> +<% end %> diff --git a/app/views/flight_logs/index.html.erb b/app/views/flight_logs/index.html.erb index ff2f444..bcbc9c2 100644 --- a/app/views/flight_logs/index.html.erb +++ b/app/views/flight_logs/index.html.erb @@ -3,53 +3,12 @@ <% if @stats.any? %><%= render "flight_logs/stats", stats: @stats %><% end %>

Flight Log

<%= link_to "Export for AirTrail", new_aircraft_air_trail_export_path(@aircraft), class: "btn-secondary" %><% if owner? %><%= link_to "Standardize airport codes", aircraft_airport_codes_path(@aircraft), class: "btn-secondary" %><%= link_to "Import from spreadsheet", new_aircraft_flight_import_path(@aircraft), class: "btn-secondary" %><% end %><%= link_to "+ Log Flight", new_aircraft_flight_log_path(@aircraft), class: "btn-primary" %>
- <% if @flight_logs.any? %> - <% blank = tag.span("β€”", class: "text-slate-300") %> - <% qty = ->(v) { v ? number_with_precision(v, precision: 2, strip_insignificant_zeros: true) : blank } %> + <% if @days.any? %> -
- <% @days.each do |day| %> -
- - <%= day.date.strftime("%a %b %-d, %Y") %><%= pluralize(day.flights.size, "flight") %> - - <%= day.route.presence || blank %> - <% if day.track_log? %>πŸ“<% end %> - <% if day.engine_data? %>πŸ“ˆ<% end %> - <% if day.pilots.any? %><%= day.pilots.to_sentence %><% end %> - - Tach <%= day.tach_time ? hours(day.tach_time) : blank %> - Hobbs <%= day.hobbs_time ? hours(day.hobbs_time) : blank %> - Fuel <%= qty.(day.fuel_added) %> - Oil <%= qty.(day.oil_added) %> - - -
- - - - <% day.flights.each_with_index do |fl, i| %> - - - - - - - - - - <% end %> - -
LegRouteTachHobbsFuel (gal)Oil (qt)Pilot
<%= link_to "Leg #{i + 1}", aircraft_flight_log_path(@aircraft, fl), class: "link" %> - <%= fl.route_summary.presence || blank %> - <% if fl.track_log.attached? %>πŸ“<% end %> - <% if fl.engine_data.attached? %>πŸ“ˆ<% end %> - <%= fl.tach_time ? hours(fl.tach_time) : blank %><%= fl.hobbs_time ? hours(fl.hobbs_time) : blank %><%= qty.(fl.fuel_added) %><%= qty.(fl.oil_added) %><%= fl.user&.display_name || blank %>
-
-
- <% end %> +
+ <%= render "flight_logs/days", days: @days, page: @page, more: @more %>
<% else %> <%= empty_state "No flights logged yet." %> diff --git a/docs/guide/flights.md b/docs/guide/flights.md index 51ac093..15b78cc 100644 --- a/docs/guide/flights.md +++ b/docs/guide/flights.md @@ -19,7 +19,7 @@ If **Enforce airworthiness** is on, neither works while the aircraft is grounded ## The Flights tab -The **Flights** tab shows totals and hours by month, then the flight log grouped by day: each day shows the whole route flown (KOQU β†’ KBID β†’ KOQU; a "Β·" marks where the aircraft moved without a logged leg) and that day's tach and Hobbs time, fuel and oil. Click a day to see its legs, and a leg to open the flight. From there: +The **Flights** tab shows totals and hours by month, then the flight log grouped by day: each day shows the whole route flown (KOQU β†’ KBID β†’ KOQU; a "Β·" marks where the aircraft moved without a logged leg) and that day's tach and Hobbs time, fuel and oil. Click a day to see its legs, and a leg to open the flight. The most recent 30 days load with the page; earlier ones load as you scroll down (or with **Load more**). From there: - **Import from spreadsheet** (owners): historical flights from `.xlsx` or `.csv` (Date, From, To, Tach, Hobbs, Oil Added, Fuel Added). A preview flags problems such as a tach going backwards and skips rows already imported. It only adds history: component hours don't change unless you choose to advance the meters. - **Export for AirTrail**: a file to import into AirTrail. For automatic syncing, see [Your profile](profile.md). diff --git a/test/integration/flight_days_test.rb b/test/integration/flight_days_test.rb index 92caf23..889db2d 100644 --- a/test/integration/flight_days_test.rb +++ b/test/integration/flight_days_test.rb @@ -41,4 +41,21 @@ class FlightDaysTest < ActionDispatch::IntegrationTest leg(Date.new(2026, 11, 8), nil, nil, 2, 2.5) assert_equal "KOQU", FlightDay.group(@a.flight_logs.to_a).first.route end + + test "the log loads a page of days at a time; the next page comes in a lazy frame" do + 35.times { |i| leg(Date.new(2026, 1, 1) + i, "KOQU", "KBID", 100 + i, 100.5 + i) } + sign_in @owner + get aircraft_flight_logs_path(@a) + assert_select "details.flight-day", FlightLogsController::DAYS_PER_PAGE + assert_select "turbo-frame#flight-days-1[target=_top]" + assert_select "turbo-frame#flight-days-2[loading=lazy][src=?]", aircraft_flight_logs_path(@a, page: 2) + assert_select "h2", text: "Flight hours by month" # stats only on the full page + + get aircraft_flight_logs_path(@a, page: 2), headers: { "Turbo-Frame" => "flight-days-2" } + assert_response :success + assert_select "turbo-frame#flight-days-2 details.flight-day", 5 + assert_select "turbo-frame#flight-days-3", 0 # last page + assert_select "h2", text: "Flight hours by month", count: 0 + assert_includes response.body, "Thu Jan 1, 2026" + end end