Flights can be corrected after they're saved

- Edit on a flight: owners can correct any flight; a pilot, the flights
  they logged (the same rule as its engine data).
- Date, airports, route, notes, oil and fuel (its oil and fuel records
  follow) and the attached files can always be changed.
- The meter readings only on the latest flight, while its tach in is
  still the aircraft's tach: the aircraft and installed components move
  by the difference, and with both readings the duration is worked out
  from them. On earlier flights they're locked (later flights started
  from them), pointing to Update Hours; the server ignores them too.
- Each correction is in the activity log with what changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-15 17:12:05 -05:00
1 parent 34879e7310
commit d2cd60c6a7
11 files changed
+274 -38

No files matched your search

+27 -4
View File
@@ -1,7 +1,9 @@
class FlightLogsController < ApplicationController
include AircraftScoped
before_action :require_owner!, only: :destroy
before_action :set_flight_log, only: %i[show engine_data_series attach_engine_data remove_engine_data]
before_action :set_flight_log, only: %i[show edit update engine_data_series attach_engine_data remove_engine_data]
before_action :require_flight_editor!, only: %i[edit update]
helper_method :can_edit_flight?
before_action :require_engine_data_access!, only: %i[attach_engine_data remove_engine_data]
helper_method :manage_engine_data?
@@ -99,6 +101,22 @@ class FlightLogsController < ApplicationController
redirect_to aircraft_flight_log_path(@aircraft, @flight_log), notice: t("flash.flight_logs.engine_data_removed")
end
def edit
@meters_editable = @flight_log.meters_editable?
end
# A correction. The readings only change on the latest flight (see
# FlightLog#meters_editable?); on earlier ones they're left as they are.
def update
@meters_editable = @flight_log.meters_editable?
attributes = flight_log_params.except(:client_key)
attributes = attributes.except(*FlightLog::METERS) unless @meters_editable
HoursService.new(@aircraft).update_flight!(@flight_log, attributes)
redirect_to aircraft_flight_log_path(@aircraft, @flight_log), notice: t("flash.flight_logs.flight_corrected")
rescue ActiveRecord::RecordInvalid
render :edit, status: :unprocessable_entity
end
# Deleting a flight does not roll back hours; correct those with an hours update.
def destroy
@aircraft.flight_logs.find(params[:id]).destroy!
@@ -108,14 +126,19 @@ class FlightLogsController < ApplicationController
private
def can_edit_flight?(flight) = flight.editable_by?(current_user, role: current_role)
def require_flight_editor!
return if can_edit_flight?(@flight_log)
redirect_to aircraft_flight_log_path(@aircraft, @flight_log), alert: t("flash.flight_logs.only_owners_or_the")
end
def set_flight_log
@flight_log = @aircraft.flight_logs.find(params[:id])
end
# Owners, or the pilot who logged the flight.
def manage_engine_data?
owner? || (@flight_log.user_id.present? && @flight_log.user_id == current_user.id)
end
def manage_engine_data? = can_edit_flight?(@flight_log)
def require_engine_data_access!
return if manage_engine_data?
+15
View File
@@ -26,6 +26,21 @@ class FlightLog < ApplicationRecord
[ departure_location, destination_location ].compact_blank.join(" → ")
end
# The meter readings that move the aircraft's and components' hours.
METERS = %w[tach_out tach_in tach_time hobbs_out hobbs_in hobbs_time].freeze
# Its readings can be corrected only while it's the last flight logged and
# nothing has changed the hours since (an hours update, an import): only
# then is the aircraft's time just this flight's on top of the one before.
def meters_editable?
aircraft.flight_logs.maximum(:id) == id && (tach_in.nil? || tach_in == aircraft.tach_time)
end
# Owners can correct any flight; a pilot, the flights they logged.
def editable_by?(user, role:)
role.in?(%w[admin owner]) || (user_id.present? && user_id == user.id)
end
private
# Reject files the engine data parser can't read, before they're stored.
+45
View File
@@ -67,8 +67,53 @@ class HoursService
flight
end
# Correct a saved flight. Its readings change only when it's the latest
# (FlightLog#meters_editable?); the aircraft and its components then move by
# the difference. Its oil and fuel records follow. With both readings given,
# the duration is worked out from them.
def update_flight!(flight, attributes)
Aircraft.transaction do
before = flight.attributes.slice(*FlightLog::METERS, "date", "departure_location", "destination_location",
"oil_added", "fuel_added", "route", "notes")
flight.assign_attributes(attributes)
flight.tach_time = flight.tach_in - flight.tach_out if flight.tach_in && flight.tach_out
flight.hobbs_time = flight.hobbs_in - flight.hobbs_out if flight.hobbs_in && flight.hobbs_out
flight.save!
tach_delta = flight.tach_time - before["tach_time"].to_d
hobbs_delta = flight.hobbs_time.to_d - before["hobbs_time"].to_d
if tach_delta.nonzero? || hobbs_delta.nonzero?
@aircraft.tach_time += tach_delta
@aircraft.hobbs_time += hobbs_delta if @aircraft.hobbs_time
@aircraft.save!
cascade_to_components(@aircraft.hobbs? && flight.hobbs_time ? hobbs_delta : tach_delta, clamp: true)
end
sync_consumables(flight)
changed = before.keys.reject { |key| before[key] == flight[key] }
@aircraft.log_event!("flight", I18n.t("#{H}.flight_corrected", date: I18n.l(flight.date, format: :app)), user: @user,
notes: changed.map { |key| FlightLog.human_attribute_name(key) }.join(", ").presence)
end
flight
end
private
# The flight's oil and fuel records match what it says was added.
def sync_consumables(flight)
{ "oil" => [ flight.oil_added, flight.oil_level_after, flight.oil_added_type ],
"fuel" => [ flight.fuel_added, flight.fuel_level_after, flight.fuel_added_type ] }.each do |type, (qty, level, kind)|
record = flight.consumable_records.find_by(record_type: type)
if qty.to_f.positive?
record ||= @aircraft.consumable_records.new(record_type: type, flight_log: flight, flight_hours: @aircraft.tach_time,
notes: I18n.t("#{H}.auto_created_from_flight_log"))
record.update!(date: flight.date, quantity_added: qty, level_after: level, consumable_type: kind)
else
record&.destroy!
end
end
end
def cascade_to_components(delta, clamp: false)
@aircraft.components.in_use.find_each do |c|
his = c.hours_in_service + delta
+38
View File
@@ -0,0 +1,38 @@
<%# The flight's fields, for Log Flight and for correcting one. Its meter
readings are locked (meters_locked) on all but the latest flight. %>
<% meters_locked = local_assigns.fetch(:meters_locked, false) %>
<div class="grid gap-4 sm:grid-cols-3">
<%= f.field :date, as: :date, required: true %>
<%= render "shared/airport_field", f: f, attribute: :departure_location, label: t("flight_logs.new.from"), aircraft: @aircraft, placeholder: t("flight_logs.new.koqu") %>
<%= render "shared/airport_field", f: f, attribute: :destination_location, label: t("flight_logs.new.to"), aircraft: @aircraft, placeholder: t("flight_logs.new.code_or_name") %>
</div>
<% if meters_locked %>
<p class="hint"><%= t("flight_logs.fields.meters_locked_html", link: link_to(t("flight_logs.fields.update_hours"), aircraft_path(@aircraft), class: "link")) %></p>
<% end %>
<%= tag.fieldset class: "grid gap-4 sm:grid-cols-3", disabled: meters_locked do %>
<legend class="mb-2 text-sm font-semibold"><%= t("flight_logs.new.tach_required") %></legend>
<%= f.field :tach_out, as: :number, step: 0.1, label: t("flight_logs.new.tach_out") %>
<%= f.field :tach_in, as: :number, step: 0.1, label: t("flight_logs.new.tach_in") %>
<%= f.field :tach_time, as: :number, step: 0.1, label: t("flight_logs.new.or_duration"), hint: t("flight_logs.new.calculated_from_out_in_if") %>
<% end %>
<%= tag.fieldset class: "grid gap-4 sm:grid-cols-3", disabled: meters_locked do %>
<legend class="mb-2 text-sm font-semibold"><%= t("flight_logs.new.hobbs_optional") %></legend>
<%= f.field :hobbs_out, as: :number, step: 0.1, label: t("flight_logs.new.hobbs_out") %>
<%= f.field :hobbs_in, as: :number, step: 0.1, label: t("flight_logs.new.hobbs_in") %>
<%= f.field :hobbs_time, as: :number, step: 0.1, label: t("flight_logs.new.or_duration") %>
<% end %>
<fieldset class="grid gap-4 sm:grid-cols-3">
<legend class="mb-2 text-sm font-semibold"><%= t("flight_logs.new.oil_fuel_added") %></legend>
<%= f.field :oil_added, as: :number, step: 0.25, label: t("flight_logs.new.oil_added_qt") %>
<%= f.field :oil_level_after, as: :number, step: 0.25, label: t("flight_logs.new.oil_level_after_qt") %>
<%= f.field :oil_added_type, label: t("flight_logs.new.oil_type"), placeholder: t("flight_logs.new.aeroshell_w100") %>
<%= f.field :fuel_added, as: :number, step: 0.1, label: t("flight_logs.new.fuel_added_gal") %>
<%= f.field :fuel_level_after, as: :number, step: 0.1, label: t("flight_logs.new.fuel_level_after_gal") %>
<%= f.field :fuel_added_type, label: t("flight_logs.new.fuel_type"), placeholder: t("flight_logs.new.n100ll") %>
</fieldset>
<%= f.field :route, as: :textarea, rows: 2 %>
<%= f.field :notes, as: :textarea, rows: 2 %>
<div class="grid gap-4 sm:grid-cols-2">
<%= f.field :track_log, as: :file, label: t("flight_logs.new.track_log_gpx_kml_csv") %>
<%= f.field :engine_data, as: :file, label: t("flight_logs.new.engine_monitor_data"), hint: t("flight_logs.new.cgr_30p_cgr_30c_flight"), accept: ".csv,text/csv" %>
</div>
+12
View File
@@ -0,0 +1,12 @@
<% content_for :title, t(".correct_flight") %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<%= form_with model: [@aircraft, @flight_log], class: "card card-body space-y-5 max-w-3xl" do |f| %>
<h2 class="card-title"><%= t(".correct_the_flight_on", date: fmt_date(@flight_log.date_was)) %></h2>
<%= f.errors_summary %>
<%= render "flight_logs/fields", f: f, meters_locked: !@meters_editable %>
<% if @meters_editable %><p class="hint"><%= t(".changing_the_readings") %></p><% end %>
<div class="flex flex-wrap gap-2">
<%= f.submit t(".save"), data: { turbo_submits_with: t("flight_logs.new.sending") } %>
<%= link_to t(".cancel"), aircraft_flight_log_path(@aircraft, @flight_log), class: "btn-secondary" %>
</div>
<% end %>
+1 -32
View File
@@ -6,38 +6,7 @@
<%= f.hidden_field :client_key, data: { offline_form_target: "key" } %>
<div class="hidden rounded-md bg-sky-50 px-3 py-2 text-sm text-sky-900 ring-1 ring-sky-200" data-offline-form-target="notice" role="status"></div>
<%= f.errors_summary %>
<div class="grid gap-4 sm:grid-cols-3">
<%= f.field :date, as: :date, required: true %>
<%= render "shared/airport_field", f: f, attribute: :departure_location, label: t(".from"), aircraft: @aircraft, placeholder: t(".koqu") %>
<%= render "shared/airport_field", f: f, attribute: :destination_location, label: t(".to"), aircraft: @aircraft, placeholder: t(".code_or_name") %>
</div>
<fieldset class="grid gap-4 sm:grid-cols-3">
<legend class="mb-2 text-sm font-semibold"><%= t(".tach_required") %></legend>
<%= f.field :tach_out, as: :number, step: 0.1, label: t(".tach_out") %>
<%= f.field :tach_in, as: :number, step: 0.1, label: t(".tach_in") %>
<%= f.field :tach_time, as: :number, step: 0.1, label: t(".or_duration"), hint: t(".calculated_from_out_in_if") %>
</fieldset>
<fieldset class="grid gap-4 sm:grid-cols-3">
<legend class="mb-2 text-sm font-semibold"><%= t(".hobbs_optional") %></legend>
<%= f.field :hobbs_out, as: :number, step: 0.1, label: t(".hobbs_out") %>
<%= f.field :hobbs_in, as: :number, step: 0.1, label: t(".hobbs_in") %>
<%= f.field :hobbs_time, as: :number, step: 0.1, label: t(".or_duration") %>
</fieldset>
<fieldset class="grid gap-4 sm:grid-cols-3">
<legend class="mb-2 text-sm font-semibold"><%= t(".oil_fuel_added") %></legend>
<%= f.field :oil_added, as: :number, step: 0.25, label: t(".oil_added_qt") %>
<%= f.field :oil_level_after, as: :number, step: 0.25, label: t(".oil_level_after_qt") %>
<%= f.field :oil_added_type, label: t(".oil_type"), placeholder: t(".aeroshell_w100") %>
<%= f.field :fuel_added, as: :number, step: 0.1, label: t(".fuel_added_gal") %>
<%= f.field :fuel_level_after, as: :number, step: 0.1, label: t(".fuel_level_after_gal") %>
<%= f.field :fuel_added_type, label: t(".fuel_type"), placeholder: t(".n100ll") %>
</fieldset>
<%= f.field :route, as: :textarea, rows: 2 %>
<%= f.field :notes, as: :textarea, rows: 2 %>
<div class="grid gap-4 sm:grid-cols-2">
<%= f.field :track_log, as: :file, label: t(".track_log_gpx_kml_csv") %>
<%= f.field :engine_data, as: :file, label: t(".engine_monitor_data"), hint: t(".cgr_30p_cgr_30c_flight"), accept: ".csv,text/csv" %>
</div>
<%= render "flight_logs/fields", f: f %>
<p class="hint"><%= t(".saving_adds_the_tach_time") %></p>
<div class="flex flex-wrap gap-2">
<%= f.submit t(".log_flight"), data: { turbo_submits_with: t(".sending") } %>
+4 -1
View File
@@ -2,7 +2,10 @@
<div class="space-y-6">
<section class="card max-w-3xl">
<div class="card-header"><h2 class="card-title"><%= t(".flight_on_html", date: (fmt_date(@flight_log.date))) %></h2>
<% if owner? %><%= delete_button t(".delete"), aircraft_flight_log_path(@aircraft, @flight_log), confirm: t(".delete_this_flight_log_aircraft") %><% end %></div>
<div class="flex flex-wrap gap-2">
<% if can_edit_flight?(@flight_log) %><%= link_to t(".edit"), edit_aircraft_flight_log_path(@aircraft, @flight_log), class: "btn-secondary btn-sm" %><% end %>
<% if owner? %><%= delete_button t(".delete"), aircraft_flight_log_path(@aircraft, @flight_log), confirm: t(".delete_this_flight_log_aircraft") %><% end %>
</div></div>
<%# One item per row on a phone (label, then value); two columns on wider screens. %>
<dl class="card-body grid gap-x-8 gap-y-3 text-sm sm:grid-cols-2">
<div class="detail-row"><dt><%= t(".tach") %></dt><dd><%= hours(@flight_log.tach_time) %> <span class="text-xs text-slate-500"><%= "(#{@flight_log.tach_out} → #{@flight_log.tach_in})" if @flight_log.tach_out && @flight_log.tach_in %></span></dd></div>