mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
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>
35 lines
1.3 KiB
Ruby
35 lines
1.3 KiB
Ruby
# A user's AirTrail account (https://github.com/johanohly/AirTrail). Flights
|
|
# the user logs are pushed there through AirTrail's REST API.
|
|
#
|
|
# The API key needs these scopes: profile.read, flight.read.own,
|
|
# flight.create.own, flight.update.own, flight.delete.own,
|
|
# reference_data.read and tracks.write.
|
|
class AirTrailConnection < ApplicationRecord
|
|
REQUIRED_SCOPES = %w[profile.read flight.read.own flight.create.own flight.update.own
|
|
flight.delete.own reference_data.read tracks.write].freeze
|
|
|
|
belongs_to :user
|
|
has_many :syncs, class_name: "AirTrailSync", dependent: :delete_all
|
|
has_many :airports, class_name: "AirTrailAirport", dependent: :delete_all
|
|
|
|
encrypts :api_key
|
|
|
|
normalizes :base_url, with: ->(url) { url.strip.chomp("/") }
|
|
validates :base_url, presence: true, format: { with: %r{\Ahttps?://[^\s/]+(:\d+)?(/\S*)?\z}i, message: "must be a http(s) URL" }
|
|
validates :api_key, presence: true
|
|
|
|
scope :active, -> { where(enabled: true) }
|
|
|
|
def client = AirTrailClient.new(base_url: base_url, api_key: api_key)
|
|
|
|
def ok? = status == "ok"
|
|
|
|
def unresolved_airport_codes
|
|
airports.where(remote_id: nil).order(:code).pluck(:code)
|
|
end
|
|
|
|
def counts
|
|
syncs.group(:status).count.reverse_merge("synced" => 0, "pending" => 0, "error" => 0)
|
|
end
|
|
end
|