Files
simple-aircraft-manager-rails/app/controllers/flight_logs_controller.rb
T
rickyandClaude Opus 5.5 d8cd462194 Configurable roles: Owner, Mechanic, Pilot, Viewer and custom roles
What people can do on an aircraft now comes from their role there, and
administrators set what each role allows (Admin → Roles).

- Roles are installation-wide sets of permissions (31, grouped as flying,
  squawks and notes, maintenance records, imports and exports, the
  aircraft, notifications). Seeing an aircraft's records needs none.
- Built in: Owner (always every permission, so an aircraft can always be
  managed), Mechanic (shop maintenance: logbook, signing, components,
  inspections, ADs, squawks, reminders), Pilot (what pilots could do
  before) and Viewer (view only). Administrators can change all but
  Owner's, and add their own; a role can be deleted once nobody has it.
- Existing owners and pilots keep exactly what they could do.
- Admin → Users → a user lists their role on every aircraft, to change,
  remove or add. On the aircraft's Access page, anyone who manages people
  hands out roles, but only an Owner can give or take away Owner, nobody
  changes their own (except administrators), and an aircraft always
  keeps an Owner (AircraftAccess).
- Controllers declare the permission each action needs; a request that
  changes something with none declared is refused (and raises in
  development and tests), and a test walks every route to check.
- Weekly reminders and overdue alerts go to roles that get reminders;
  the DRS reminder also needs Manage ADs. The API follows the person's
  role; its owners_only error is now not_permitted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 17:43:38 -05:00

161 lines
7.1 KiB
Ruby

class FlightLogsController < ApplicationController
include AircraftScoped
requires_permission :log_flights, only: %i[new create]
requires_permission :delete_flights, only: :destroy
checks_permission_itself only: %i[edit update 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?
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
@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
def show
begin
@track = TrackParser.parse_attachment(@flight_log.track_log)
rescue TrackParser::Error => e
@track_error = t("flash.flight_logs.track_unreadable", message: e.message)
end
begin
@engine_data = EngineDataParser.parse_attachment(@flight_log.engine_data)
rescue EngineDataParser::Error, ActiveStorage::FileNotFoundError => e
@engine_data_error = t("flash.flight_logs.engine_data_unreadable", message: e.message)
end
@air_trail_connection = @flight_log.user&.air_trail_connection
@air_trail_sync = @flight_log.air_trail_syncs.first
end
# JSON for the interactive engine chart viewer (loaded when a chart is clicked).
def engine_data_series
recording = EngineDataParser.parse_attachment(@flight_log.engine_data) or return head(:not_found)
return unless stale?(etag: [ @flight_log.engine_data.blob.key, EngineDataParser::CACHE_VERSION ], public: false)
render json: recording.detail.merge(groups: helpers.engine_chart_groups(recording))
rescue EngineDataParser::Error, ActiveStorage::FileNotFoundError => e
render json: { error: e.message }, status: :unprocessable_entity
end
def new
# Aircraft almost always depart from where they last landed.
last = @aircraft.flight_logs.where.not(destination_location: [ nil, "" ]).recent_first.first
# "Save and log next leg" comes back here with that flight's date.
date = begin
Date.iso8601(params[:date].to_s)
rescue Date::Error
Date.current
end
@flight_log = @aircraft.flight_logs.new(date: date, tach_out: @aircraft.tach_time,
hobbs_out: @aircraft.hobbs_time,
departure_location: last&.destination_location)
end
def create
# Already received (sent again from the offline queue): don't log it twice.
if (key = params.dig(:flight_log, :client_key).to_s.presence) && @aircraft.flight_logs.exists?(client_key: key.first(64))
return redirect_to(aircraft_flight_logs_path(@aircraft), notice: t("flash.flight_logs.flight_already_logged"))
end
@flight_log = @aircraft.flight_logs.new(flight_log_params)
if @flight_log.valid?
HoursService.new(@aircraft).log_flight!(@flight_log)
notice = t("flash.flight_logs.flight_logged_hrs", tach_time: @flight_log.tach_time)
if params[:next_leg].present?
redirect_to new_aircraft_flight_log_path(@aircraft, date: @flight_log.date.iso8601), notice: "#{notice} #{t('flash.flight_logs.next_leg')}"
else
redirect_to aircraft_flight_logs_path(@aircraft), notice: notice
end
else
render :new, status: :unprocessable_entity
end
rescue AircraftGroundedError => e
flash.now[:alert] = e.message
render :new, status: :conflict
end
def attach_engine_data
file = params.dig(:flight_log, :engine_data)
return redirect_to(aircraft_flight_log_path(@aircraft, @flight_log), alert: t("flash.flight_logs.choose_an_engine_data")) if file.blank?
@flight_log.engine_data = file
if @flight_log.save
redirect_to aircraft_flight_log_path(@aircraft, @flight_log), notice: t("flash.flight_logs.engine_data_attached")
else
redirect_to aircraft_flight_log_path(@aircraft, @flight_log), alert: @flight_log.errors.full_messages.to_sentence
end
end
def remove_engine_data
@flight_log.engine_data.purge_later
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!
log_event("flight", t("flash.flight_logs.flight_log_deleted"))
redirect_to aircraft_flight_logs_path(@aircraft), notice: t("flash.flight_logs.flight_log_deleted_aircraft")
end
private
# Any flight with "correct any flight"; the ones they logged with "correct own flights".
def can_edit_flight?(flight)
can?(:edit_any_flight) || (can?(:edit_own_flights) && flight.user_id.present? && flight.user_id == current_user.id)
end
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? = can_edit_flight?(@flight_log)
def require_engine_data_access!
return if manage_engine_data?
redirect_to aircraft_flight_log_path(@aircraft, @flight_log), alert: t("flash.flight_logs.only_owners_or_the")
end
def flight_log_params
params.require(:flight_log).permit(
:date, :tach_time, :tach_out, :tach_in, :hobbs_time, :hobbs_out, :hobbs_in,
:departure_location, :destination_location, :route, :oil_added, :oil_added_type,
:oil_level_after, :fuel_added, :fuel_added_type, :fuel_level_after, :notes, :track_log, :engine_data, :client_key
).tap { _1[:client_key] = _1[:client_key].to_s.first(64).presence if _1.key?(:client_key) }
end
end