mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
7dbf180f76
commit
455a26a346
5 files changed
+88
-47
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -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| %>
|
||||
<details class="group flight-day border-b border-slate-100">
|
||||
<summary class="flex cursor-pointer list-none flex-wrap items-baseline gap-x-3 gap-y-1 px-4 py-3 hover:bg-slate-50 sm:grid sm:grid-cols-[9.5rem_1fr_repeat(4,5rem)_1.25rem] [&::-webkit-details-marker]:hidden">
|
||||
<span class="font-medium whitespace-nowrap"><%= day.date.strftime("%a %b %-d, %Y") %><span class="block text-xs font-normal text-slate-500"><%= pluralize(day.flights.size, "flight") %></span></span>
|
||||
<span class="min-w-0 basis-full sm:basis-auto">
|
||||
<%= day.route.presence || blank %>
|
||||
<% if day.track_log? %><span class="ml-1" title="GPS track attached" aria-label="GPS track attached">📍</span><% end %>
|
||||
<% if day.engine_data? %><span class="ml-1" title="Engine data attached" aria-label="Engine data attached">📈</span><% end %>
|
||||
<% if day.pilots.any? %><span class="block text-xs text-slate-500"><%= day.pilots.to_sentence %></span><% end %>
|
||||
</span>
|
||||
<span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Tach </span><%= day.tach_time ? hours(day.tach_time) : blank %></span>
|
||||
<span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Hobbs </span><%= day.hobbs_time ? hours(day.hobbs_time) : blank %></span>
|
||||
<span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Fuel </span><%= qty.(day.fuel_added) %></span>
|
||||
<span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Oil </span><%= qty.(day.oil_added) %></span>
|
||||
<span aria-hidden="true" class="ml-auto text-slate-400 transition group-open:rotate-90">›</span>
|
||||
</summary>
|
||||
<div class="overflow-x-auto bg-slate-50/60 px-2 pb-3 sm:px-4">
|
||||
<table class="table">
|
||||
<thead><tr><th>Leg</th><th>Route</th><th class="text-right">Tach</th><th class="text-right">Hobbs</th><th class="text-right">Fuel (gal)</th><th class="text-right">Oil (qt)</th><th class="pl-6">Pilot</th></tr></thead>
|
||||
<tbody>
|
||||
<% day.flights.each_with_index do |fl, i| %>
|
||||
<tr>
|
||||
<td class="whitespace-nowrap" data-label=""><%= link_to "Leg #{i + 1}", aircraft_flight_log_path(@aircraft, fl), class: "link" %></td>
|
||||
<td class="whitespace-nowrap" data-label="Route">
|
||||
<%= fl.route_summary.presence || blank %>
|
||||
<% if fl.track_log.attached? %><span class="ml-1" title="GPS track attached" aria-label="GPS track attached">📍</span><% end %>
|
||||
<% if fl.engine_data.attached? %><span class="ml-1" title="Engine data attached" aria-label="Engine data attached">📈</span><% end %>
|
||||
</td>
|
||||
<td class="text-right tabular-nums" data-label="Tach"><%= fl.tach_time ? hours(fl.tach_time) : blank %></td>
|
||||
<td class="text-right tabular-nums" data-label="Hobbs"><%= fl.hobbs_time ? hours(fl.hobbs_time) : blank %></td>
|
||||
<td class="text-right tabular-nums" data-label="Fuel"><%= qty.(fl.fuel_added) %></td>
|
||||
<td class="text-right tabular-nums" data-label="Oil"><%= qty.(fl.oil_added) %></td>
|
||||
<td class="pl-6 text-slate-600" data-label="Pilot"><%= fl.user&.display_name || blank %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</details>
|
||||
<% 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 %>
|
||||
<p class="px-4 py-4 text-center text-sm text-slate-500">
|
||||
Loading earlier flights… <%= link_to "Load more", aircraft_flight_logs_path(@aircraft, page: page + 1), class: "link", data: { turbo_frame: "flight-days-#{page + 1}" } %>
|
||||
</p>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
@@ -3,53 +3,12 @@
|
||||
<% if @stats.any? %><%= render "flight_logs/stats", stats: @stats %><% end %>
|
||||
<section class="card">
|
||||
<div class="card-header"><h2 class="card-title">Flight Log</h2><div class="flex gap-2"><%= 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" %></div></div>
|
||||
<% 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? %>
|
||||
<div class="hidden grid-cols-[9.5rem_1fr_repeat(4,5rem)_1.25rem] gap-3 border-b border-slate-200 bg-slate-50 px-4 py-2 text-xs font-semibold uppercase tracking-wide text-slate-500 sm:grid">
|
||||
<span>Date</span><span>Route</span><span class="text-right">Tach</span><span class="text-right">Hobbs</span><span class="text-right">Fuel (gal)</span><span class="text-right">Oil (qt)</span><span></span>
|
||||
</div>
|
||||
<div class="divide-y divide-slate-100">
|
||||
<% @days.each do |day| %>
|
||||
<details class="group flight-day">
|
||||
<summary class="flex cursor-pointer list-none flex-wrap items-baseline gap-x-3 gap-y-1 px-4 py-3 hover:bg-slate-50 sm:grid sm:grid-cols-[9.5rem_1fr_repeat(4,5rem)_1.25rem] [&::-webkit-details-marker]:hidden">
|
||||
<span class="font-medium whitespace-nowrap"><%= day.date.strftime("%a %b %-d, %Y") %><span class="block text-xs font-normal text-slate-500"><%= pluralize(day.flights.size, "flight") %></span></span>
|
||||
<span class="min-w-0 basis-full sm:basis-auto">
|
||||
<%= day.route.presence || blank %>
|
||||
<% if day.track_log? %><span class="ml-1" title="GPS track attached" aria-label="GPS track attached">📍</span><% end %>
|
||||
<% if day.engine_data? %><span class="ml-1" title="Engine data attached" aria-label="Engine data attached">📈</span><% end %>
|
||||
<% if day.pilots.any? %><span class="block text-xs text-slate-500"><%= day.pilots.to_sentence %></span><% end %>
|
||||
</span>
|
||||
<span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Tach </span><%= day.tach_time ? hours(day.tach_time) : blank %></span>
|
||||
<span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Hobbs </span><%= day.hobbs_time ? hours(day.hobbs_time) : blank %></span>
|
||||
<span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Fuel </span><%= qty.(day.fuel_added) %></span>
|
||||
<span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Oil </span><%= qty.(day.oil_added) %></span>
|
||||
<span aria-hidden="true" class="ml-auto text-slate-400 transition group-open:rotate-90">›</span>
|
||||
</summary>
|
||||
<div class="overflow-x-auto bg-slate-50/60 px-2 pb-3 sm:px-4">
|
||||
<table class="table">
|
||||
<thead><tr><th>Leg</th><th>Route</th><th class="text-right">Tach</th><th class="text-right">Hobbs</th><th class="text-right">Fuel (gal)</th><th class="text-right">Oil (qt)</th><th class="pl-6">Pilot</th></tr></thead>
|
||||
<tbody>
|
||||
<% day.flights.each_with_index do |fl, i| %>
|
||||
<tr>
|
||||
<td class="whitespace-nowrap" data-label=""><%= link_to "Leg #{i + 1}", aircraft_flight_log_path(@aircraft, fl), class: "link" %></td>
|
||||
<td class="whitespace-nowrap" data-label="Route">
|
||||
<%= fl.route_summary.presence || blank %>
|
||||
<% if fl.track_log.attached? %><span class="ml-1" title="GPS track attached" aria-label="GPS track attached">📍</span><% end %>
|
||||
<% if fl.engine_data.attached? %><span class="ml-1" title="Engine data attached" aria-label="Engine data attached">📈</span><% end %>
|
||||
</td>
|
||||
<td class="text-right tabular-nums" data-label="Tach"><%= fl.tach_time ? hours(fl.tach_time) : blank %></td>
|
||||
<td class="text-right tabular-nums" data-label="Hobbs"><%= fl.hobbs_time ? hours(fl.hobbs_time) : blank %></td>
|
||||
<td class="text-right tabular-nums" data-label="Fuel"><%= qty.(fl.fuel_added) %></td>
|
||||
<td class="text-right tabular-nums" data-label="Oil"><%= qty.(fl.oil_added) %></td>
|
||||
<td class="pl-6 text-slate-600" data-label="Pilot"><%= fl.user&.display_name || blank %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</details>
|
||||
<% end %>
|
||||
<div>
|
||||
<%= render "flight_logs/days", days: @days, page: @page, more: @more %>
|
||||
</div>
|
||||
<% else %>
|
||||
<%= empty_state "No flights logged yet." %>
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user