AirTrail sync and export, GPS tracks, airport database with autocomplete

AirTrail (https://github.com/johanohly/AirTrail):
- File export (Flights → Export for AirTrail) in AirTrail's import
  format, validated against its schema; same-day repeat legs get leg
  numbers so AirTrail's de-duplication doesn't merge them.
- Automatic sync through AirTrail's REST API (Profile → AirTrail sync):
  per-user connection with an encrypted API key and scope check; flights
  are created/updated/deleted in background jobs; a full sync first
  links flights already in AirTrail (e.g. from a file import) instead of
  duplicating them; airports are resolved and cached, with manual
  mapping for unknown codes.
- GPS tracks: GPX, KML and CSV track logs are converted to AirTrail's
  track format for both sync and export; the flight page shows a summary.
- Aircraft ICAO type (e.g. C150) for AirTrail's aircraft type.

Airport database (OurAirports, the source AirTrail uses):
- Managed like AirTrail: downloaded in the background on first start,
  updated in place from Admin → Airports (adds, changes, removes; ids
  stable), with admin-managed custom airports that updates never touch.
- Each airport carries AirTrail's code, so logged codes translate to
  AirTrail's exactly (1B9 → K1B9, 4MB → K5M3).
- From/To autocomplete on new flights (airports this aircraft has used
  rank first); From is pre-filled with the last flight's destination;
  codes are normalized to uppercase.
- Flights → Standardize airport codes: rewrite logged codes to the
  standard code, and fix codes not in the database (typos such as 0R8 →
  08R are suggested).

Also: admin pages grouped under an Admin dropdown menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-05 19:40:20 -05:00
1 parent 0c4803d717
commit 5bf682502b
68 files changed
+2667 -10

No files matched your search

+18
View File
@@ -4,6 +4,9 @@ class FlightLog < ApplicationRecord
belongs_to :aircraft
belongs_to :user, optional: true
has_many :consumable_records, dependent: :nullify
# Capture AirTrail ids before the sync rows are deleted, so the remote flights can be removed too.
before_destroy :remember_air_trail_flights, prepend: true
has_many :air_trail_syncs, dependent: :delete_all
has_one_attached :track_log
validates :date, presence: true
@@ -11,6 +14,9 @@ class FlightLog < ApplicationRecord
validates :hobbs_time, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
validates_uploads :track_log
before_validation :derive_times
normalizes :departure_location, :destination_location, with: ->(code) { code.strip.upcase.presence }
after_commit :push_to_air_trail, on: %i[create update]
after_commit :delete_from_air_trail, on: :destroy
scope :recent_first, -> { order(date: :desc, created_at: :desc) }
@@ -20,6 +26,18 @@ class FlightLog < ApplicationRecord
private
def push_to_air_trail
AirTrailPushJob.perform_later(id) if user_id && AirTrailConnection.active.exists?(user_id: user_id)
end
def remember_air_trail_flights
@air_trail_remote = air_trail_syncs.where.not(remote_id: nil).pluck(:air_trail_connection_id, :remote_id)
end
def delete_from_air_trail
Array(@air_trail_remote).each { |connection_id, remote_id| AirTrailDeleteJob.perform_later(connection_id, remote_id) }
end
# Let the pilot enter out/in readings instead of the delta.
def derive_times
self.tach_time = tach_in - tach_out if tach_time.blank? && tach_in && tach_out