Files
simple-aircraft-manager-rails/app/models/aircraft.rb
T
rickyandClaude Opus 5.5 d519aa9aae Track oil analysis reports; import AvLab PDFs
New Oil Analysis tab: one record per oil sample with dates, hours, the lab's
verdict and comments, ppm results for 12 wear metals with the lab's averages,
and the report PDF. Shows wear-metal trends, a side-by-side history table,
and flags readings 1.5x or more above the lab average.

AvLab report PDFs can be imported: every sample on the report (current and
previous) is added unless already recorded, the engine is matched by serial
number, and reports for another tail number are refused. The parser reads
positioned text and drops the hidden color-code layer that overlaps results.

Adds the pdf-reader gem. Test fixture is a rebuilt report with a dummy header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 06:12:51 -05:00

77 lines
2.3 KiB
Ruby

class Aircraft < ApplicationRecord
STATUSES = {
"AVAILABLE" => "Available",
"MX" => "In Maintenance",
"GROUND" => "Grounded",
"UNAVAILABLE" => "Unavailable"
}.freeze
has_one_attached :picture
has_many :roles, class_name: "AircraftRole", dependent: :destroy
has_many :users, through: :roles
has_many :notes, class_name: "AircraftNote", dependent: :destroy
has_many :events, class_name: "AircraftEvent", dependent: :delete_all
has_many :share_tokens, dependent: :destroy
has_many :components, dependent: :destroy
has_many :squawks, dependent: :destroy
has_many :logbook_entries, dependent: :destroy
has_many :document_collections, dependent: :destroy
has_many :documents, dependent: :destroy
has_many :inspection_records, dependent: :destroy
has_many :ad_compliances, dependent: :destroy
has_many :major_records, dependent: :destroy
has_many :flight_logs, dependent: :destroy
has_many :consumable_records, dependent: :destroy
has_many :oil_analyses, dependent: :destroy
has_and_belongs_to_many :inspection_types
has_and_belongs_to_many :airworthiness_directives
validates :tail_number, presence: true
validates :status, inclusion: { in: STATUSES.keys }
validates :icao_type, format: { with: /\A[A-Z0-9]{1,4}\z/, message: "must be an ICAO type designator like C150" }, allow_blank: true
normalizes :icao_type, with: ->(t) { t.strip.upcase.presence }
validate :picture_is_image
scope :ordered, -> { order(:tail_number) }
def to_s = tail_number
def status_label = STATUSES[status]
# Hours used for compliance calculations (tach minus offset, as in the original).
def current_hours
tach_time - tach_time_offset
end
def airworthiness
@airworthiness ||= Airworthiness.new(self).call
end
def reload(*)
@airworthiness = nil
super
end
# ADs that apply directly or via any installed component.
def applicable_ads
AirworthinessDirective.applicable_to(self)
end
def applicable_inspection_types
InspectionType.applicable_to(self)
end
def log_event!(category, event_name, user: Current.user, notes: nil)
events.create!(category: category, event_name: event_name, user: user, notes: notes)
end
private
def picture_is_image
if picture.attached? && !picture.content_type.to_s.start_with?("image/")
errors.add(:picture, "must be an image")
end
end
end