Files
simple-aircraft-manager-rails/app/models/user_certificate.rb
T
rickyandClaude Opus 5.5 aa262065b2 Certificates on user profiles, with "Sign as"
- Users add their FAA certificates (kind, number, optional expiry) on
  their profile; admins see them on the Users list
- "Sign as" on new/edited logbook entries and the squawk Resolve page
  fills the signer's name, kind of certificate and number (expired
  certificates aren't offered); deferrals default to mechanic for users
  with a mechanic certificate
- Weekly reminder lists certificates expiring within 60 days (or lapsed
  in the last 30), even when nothing else is due
- Preventive maintenance templates keep a certificate already chosen

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

27 lines
1.3 KiB
Ruby

# A user's FAA certificate (A&P, IA, pilot…), used to sign logbook entries.
# Some expire (an Inspection Authorization lapses on March 31 of each
# odd-numbered year unless renewed, 14 CFR 65.92).
class UserCertificate < ApplicationRecord
KINDS = LogbookEntry::CERTIFICATE_TYPES
MECHANIC_KINDS = %w[AP IA A P REPAIR_STATION REPAIRMAN].freeze
SHORT_LABELS = { "AP" => "A&P", "IA" => "IA", "A" => "Airframe", "P" => "Powerplant", "REPAIR_STATION" => "Repair station",
"REPAIRMAN" => "Repairman", "PILOT" => "Pilot", "OTHER" => "Other" }.freeze
EXPIRY_WARNING_DAYS = 60
belongs_to :user
normalizes :number, with: ->(n) { n.strip }
validates :kind, inclusion: { in: KINDS.keys }
validates :number, presence: true, length: { maximum: 40 }
scope :ordered, -> { order(Arel.sql("CASE kind WHEN 'IA' THEN 0 WHEN 'AP' THEN 1 ELSE 2 END"), :created_at) }
# Expiring soon, or lapsed in the last month (so it isn't mentioned forever).
scope :expiring, ->(within = EXPIRY_WARNING_DAYS.days) { where(expires_on: (Date.current - 30)..(Date.current + within)) }
def kind_label = KINDS[kind]
def short_label = SHORT_LABELS[kind]
def mechanic? = MECHANIC_KINDS.include?(kind)
def expired?(today = Date.current) = expires_on.present? && today > expires_on
def to_s = "#{kind_label} #{number}"
end