Files
simple-aircraft-manager-rails/app/controllers/airworthiness_directives_controller.rb
T
rickyandClaude Opus 5.5 b75fd7cf62 Import ADs from FAA DRS exports; N/A determinations and DRS links
AD import (owners, ADs tab → Import from FAA DRS):
- Upload a DRS export (.xlsx recommended, .csv works), preview, confirm.
- Per AD: track, not applicable, or skip, with suggestions and flags for
  superseded/non-current ADs, model mismatches, appliance and STC-only
  ADs, repetitive actions, and older ADs this one supersedes.
- ADs are matched by number; re-importing updates existing ADs from the
  FAA data (preview lists what will change) without touching compliance
  records, recurrence or applicability. Superseded tracked ADs are
  updated and flagged rather than skipped.
- DRS's Excel export hyperlinks each AD number to its DRS page; those
  links are kept (http(s) only). DRS ids aren't derivable from the AD
  number, so no links are guessed.

Also:
- Per-aircraft "not applicable" AD determinations (N/A badge, quick
  action, excluded from airworthiness).
- AD numbers link to their DRS document in the AD list, compliance
  history (now with description, logbook/inspection links) and share page.
- Saved DRS search link per aircraft with an "Open in DRS" button.
- Upload storage extracted to a StoredUpload concern shared with the
  flight import.
- document_url / drs_search_url added in their own guarded migration
  (they were briefly in an already-applied migration).

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

76 lines
2.9 KiB
Ruby

class AirworthinessDirectivesController < ApplicationController
include AircraftScoped
include Applicability
before_action :require_owner!, except: :index
before_action :set_ad, only: %i[edit update destroy]
def index
@rows = Compliance.ad_rows(@aircraft)
@history = AdCompliance.for_aircraft(@aircraft).recent_first
.includes(:component, :logbook_entry, airworthiness_directive: :document, inspection_record: :inspection_type)
@attachable = AirworthinessDirective.where.not(id: @aircraft.applicable_ads.select(:id)).ordered if owner?
end
def new
@ad = AirworthinessDirective.new
@applies_to_airframe = true
end
def create
@ad = AirworthinessDirective.new(ad_params)
AirworthinessDirective.transaction do
@ad.save!
apply_applicability(@ad, to_airframe: applies_to_airframe?, component_ids: applicable_component_ids)
end
log_event("ad", "#{@ad.bulletin_label} added: #{@ad.name}")
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "#{@ad.bulletin_label} added."
rescue ActiveRecord::RecordInvalid
@applies_to_airframe = applies_to_airframe?
render :new, status: :unprocessable_entity
end
# Link an AD that already exists (e.g. tracked on another aircraft).
def attach
ad = AirworthinessDirective.find(params[:airworthiness_directive_id])
ad.aircraft << @aircraft unless ad.aircraft.include?(@aircraft)
log_event("ad", "#{ad.bulletin_label} linked: #{ad.name}")
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "#{ad.name} linked."
end
def edit
@applies_to_airframe = @ad.aircraft.include?(@aircraft)
end
def update
AirworthinessDirective.transaction do
@ad.update!(ad_params)
apply_applicability(@ad, to_airframe: applies_to_airframe?, component_ids: applicable_component_ids)
end
log_event("ad", "#{@ad.bulletin_label} updated: #{@ad.name}")
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "#{@ad.bulletin_label} updated."
rescue ActiveRecord::RecordInvalid
@applies_to_airframe = applies_to_airframe?
render :edit, status: :unprocessable_entity
end
# Removes the AD from this aircraft (the shared AD record survives if other aircraft use it).
def destroy
detach_from_aircraft(@ad)
log_event("ad", "#{@ad.bulletin_label} removed: #{@ad.name}")
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "#{@ad.name} removed from #{@aircraft.tail_number}."
end
private
def set_ad = @ad = @aircraft.applicable_ads.find(params[:id])
def ad_params
p = params.require(:airworthiness_directive).permit(
:name, :short_description, :required_action, :compliance_type, :trigger_condition, :recurring,
:recurring_hours, :recurring_months, :recurring_days, :bulletin_type, :mandatory, :document_id, :document_url
)
p[:document_id] = scoped_id(@aircraft.documents, p[:document_id])
p
end
end