Files
simple-aircraft-manager-rails/app/controllers/air_trail_connections_controller.rb
rickyandClaude Opus 5.5 89ed0a76f9 Move all user-facing text into config/locales/en.yml for localization
- Views use lazy t(".key") lookups; sentences with values or links stay
  whole (interpolation, _html keys); counts use one/other plurals
- Stored-value labels (statuses, types, priorities...) are LabelSets read
  from labels: in the current locale; field names and validation messages
  under activerecord:; flash and activity text under flash:
- Services, models, helpers, mailer subjects, the logbook PDF, procedures
  templates, records package README and transcription warnings localized;
  the PDF reuses LogbookEntry#certification_statement instead of a copy
- Scripts read js: strings from a JSON tag via app/javascript/i18n.js
- Dates through I18n.l (app, weekday, month_year, long formats); html lang
  follows the locale
- APP_LOCALE picks the language (falls back to English); missing
  translations raise in development and tests
- README: Translating section and APP_LOCALE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:08:20 -05:00

95 lines
3.6 KiB
Ruby

# The signed-in user's AirTrail sync settings (Profile → AirTrail).
class AirTrailConnectionsController < ApplicationController
before_action :set_connection, except: %i[show create]
def show
@connection = current_user.air_trail_connection || current_user.build_air_trail_connection
return unless @connection.persisted?
@counts = @connection.counts
@failed = @connection.syncs.failed.includes(flight_log: :aircraft).order(updated_at: :desc).limit(20)
@unresolved = @connection.unresolved_airport_codes
@flight_count = FlightLog.where(user: current_user).count
end
def create
@connection = current_user.build_air_trail_connection(connection_params)
save_and_verify
end
def update
attrs = connection_params
attrs.delete(:api_key) if attrs[:api_key].blank? # keep the stored key
url_changed = attrs[:base_url].present? && attrs[:base_url].chomp("/") != @connection.base_url
@connection.assign_attributes(attrs)
@connection.remote_user_id = nil if url_changed || attrs.key?(:api_key) # re-identify
save_and_verify
end
def test
verify
redirect_to air_trail_connection_path
end
# Link existing AirTrail flights and push everything else, in the background.
def sync
AirTrailBackfillJob.perform_later(@connection.id)
redirect_to air_trail_connection_path, notice: t("flash.air_trail_connections.sync_started_refresh_this")
end
def map_airport
code = params[:code].to_s.strip.upcase
target = params[:airtrail_code].to_s.strip.upcase
match = target.present? && AirTrailSyncer.new(@connection).map_airport(code, target)
if match
AirTrailBackfillJob.perform_later(@connection.id) # retry flights that needed it
redirect_to air_trail_connection_path, notice: t("flash.air_trail_connections.mapped_to_retrying_affected", code: code, icao: match["icao"], name: match["name"])
else
redirect_to air_trail_connection_path, alert: t("flash.air_trail_connections.airtrail_has_no_airport", target: target)
end
rescue AirTrailClient::Error => e
redirect_to air_trail_connection_path, alert: e.message
end
# Stops syncing. Flights already in AirTrail are left there.
def destroy
@connection.destroy!
redirect_to air_trail_connection_path, notice: t("flash.air_trail_connections.airtrail_disconnected_flights_already")
end
private
def set_connection
@connection = current_user.air_trail_connection or redirect_to(air_trail_connection_path)
end
def connection_params
params.require(:air_trail_connection).permit(:base_url, :api_key, :enabled)
end
def save_and_verify
if @connection.save
verify
redirect_to air_trail_connection_path
else
render :show, status: :unprocessable_entity
end
end
def verify
me = @connection.client.me
scopes = Array(me.dig("credential", "scopes"))
missing = me.dig("credential", "kind") == "apiKey" ? AirTrailConnection::REQUIRED_SCOPES - scopes : []
@connection.update!(remote_user_id: me["id"], remote_username: me["username"], checked_at: Time.current,
status: missing.empty? ? "ok" : "error",
last_error: missing.empty? ? nil : t("flash.air_trail_connections.missing_scopes", scopes: missing.join(", ")))
if missing.empty?
flash[:notice] = t("flash.air_trail_connections.connected_to_airtrail_as", username: me["username"])
else
flash[:alert] = @connection.last_error
end
rescue AirTrailClient::Error => e
@connection.update!(status: "error", last_error: e.message, checked_at: Time.current)
flash[:alert] = e.message
end
end