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

@@ -0,0 +1,63 @@
# Admin → Airports: OurAirports updates and custom airports.
class Admin::AirportsController < ApplicationController
before_action :require_admin!
before_action :set_airport, only: %i[edit update destroy]
def index
@total = Airport.count
@custom = Airport.custom.order(:ident)
@updates = AirportUpdate.recent_first.includes(:requested_by).limit(5)
@running = AirportUpdate.in_progress.exists?
@last_success = AirportUpdate.last_success
@query = params[:q].to_s.strip
@results = @query.present? ? Airport.search(@query, limit: 20) : []
end
# Queue a download + sync from OurAirports.
def refresh
if AirportUpdate.in_progress.exists?
redirect_to admin_airports_path, alert: "An update is already running."
else
update = AirportUpdate.create!(source_url: AirportData::SOURCE_URL, requested_by: current_user)
AirportUpdateJob.perform_later(update.id)
redirect_to admin_airports_path, notice: "Airport update started. Refresh this page in a minute or two."
end
end
def new
@airport = Airport.new(custom: true, kind: "small_airport", ident: params[:ident])
end
def create
@airport = Airport.new(airport_params.merge(custom: true))
if @airport.save
redirect_to admin_airports_path, notice: "Custom airport #{@airport.ident} added."
else
render :new, status: :unprocessable_entity
end
end
def edit; end
def update
if @airport.update(airport_params)
redirect_to admin_airports_path, notice: "#{@airport.ident} updated."
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@airport.destroy!
redirect_to admin_airports_path, notice: "#{@airport.ident} removed."
end
private
# Only custom airports can be edited; OurAirports data comes from updates.
def set_airport = @airport = Airport.custom.find(params[:id])
def airport_params
params.require(:airport).permit(:ident, :name, :kind, :municipality, :region, :country, :latitude, :longitude, :elevation_ft, :gps_code, :local_code, :iata)
end
end