mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Initial commit: Rails port of Simple Aircraft Manager
Rails 8.1 + SQLite rewrite of marbindrakon/simple-aircraft-manager, designed to self-host as a single container (Solid Queue/Cache/Cable, Active Storage on disk, Hotwire + importmap, Tailwind). - Airworthiness engine ported from health/services.py (ADs, grounding squawks, inspection recurrency, component replacement) - Hours updates and flight logs cascading to in-service components, optional airworthiness enforcement - Components, squawks, ADs, inspections, logbook, documents, major records, oil/fuel consumption stats and charts, activity log - Admin/owner/pilot RBAC and public share links (status/maintenance) - Docker Compose and Kamal deployment, env-driven production config - Service and integration tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
38df19c331
264 files changed
+8365
No files matched your search
@@ -0,0 +1,16 @@
|
||||
class AdCompliance < ApplicationRecord
|
||||
belongs_to :airworthiness_directive
|
||||
belongs_to :aircraft, optional: true
|
||||
belongs_to :component, optional: true
|
||||
belongs_to :logbook_entry, optional: true
|
||||
belongs_to :inspection_record, optional: true
|
||||
|
||||
validates :date_complied, :compliance_notes, presence: true
|
||||
validate { errors.add(:base, "Must belong to an aircraft or component") unless aircraft_id || component_id }
|
||||
|
||||
scope :recent_first, -> { order(date_complied: :desc, created_at: :desc) }
|
||||
|
||||
def self.for_aircraft(aircraft)
|
||||
where(aircraft_id: aircraft.id).or(where(component_id: aircraft.components.select(:id)))
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,73 @@
|
||||
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_and_belongs_to_many :inspection_types
|
||||
has_and_belongs_to_many :airworthiness_directives
|
||||
|
||||
validates :tail_number, presence: true
|
||||
validates :status, inclusion: { in: STATUSES.keys }
|
||||
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
|
||||
@@ -0,0 +1,15 @@
|
||||
class AircraftEvent < ApplicationRecord
|
||||
CATEGORIES = {
|
||||
"hours" => "Hours Update", "flight" => "Flight", "component" => "Component",
|
||||
"squawk" => "Squawk", "note" => "Note", "oil" => "Oil", "fuel" => "Fuel",
|
||||
"logbook" => "Logbook", "ad" => "Airworthiness Directive", "inspection" => "Inspection",
|
||||
"document" => "Document", "aircraft" => "Aircraft", "role" => "Role",
|
||||
"major_record" => "Major Repair/Alteration"
|
||||
}.freeze
|
||||
|
||||
belongs_to :aircraft
|
||||
belongs_to :user, optional: true
|
||||
|
||||
validates :category, inclusion: { in: CATEGORIES.keys }
|
||||
default_scope { order(created_at: :desc) }
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
class AircraftNote < ApplicationRecord
|
||||
belongs_to :aircraft
|
||||
belongs_to :user, optional: true
|
||||
|
||||
validates :text, presence: true
|
||||
scope :visible_publicly, -> { where(public: true) }
|
||||
scope :recent_first, -> { order(created_at: :desc) }
|
||||
end
|
||||
@@ -0,0 +1,11 @@
|
||||
class AircraftRole < ApplicationRecord
|
||||
ROLES = { "owner" => "Owner", "pilot" => "Pilot" }.freeze
|
||||
|
||||
belongs_to :aircraft
|
||||
belongs_to :user
|
||||
|
||||
validates :role, inclusion: { in: ROLES.keys }
|
||||
validates :user_id, uniqueness: { scope: :aircraft_id, message: "already has a role on this aircraft" }
|
||||
|
||||
scope :owners, -> { where(role: "owner") }
|
||||
end
|
||||
@@ -0,0 +1,33 @@
|
||||
class AirworthinessDirective < ApplicationRecord
|
||||
COMPLIANCE_TYPES = { "standard" => "Standard", "conditional" => "Conditional" }.freeze
|
||||
BULLETIN_TYPES = {
|
||||
"ad" => "Airworthiness Directive", "saib" => "Special Airworthiness Information Bulletin",
|
||||
"sb" => "Service Bulletin", "alert" => "Airworthiness Alert", "other" => "Other"
|
||||
}.freeze
|
||||
BULLETIN_LABELS = { "ad" => "AD", "saib" => "SAIB", "sb" => "SB", "alert" => "Alert", "other" => "Bulletin" }.freeze
|
||||
|
||||
belongs_to :document, optional: true
|
||||
has_and_belongs_to_many :aircraft
|
||||
has_and_belongs_to_many :components
|
||||
has_many :ad_compliances, dependent: :destroy
|
||||
|
||||
validates :name, :short_description, presence: true
|
||||
validates :compliance_type, inclusion: { in: COMPLIANCE_TYPES.keys }
|
||||
validates :bulletin_type, inclusion: { in: BULLETIN_TYPES.keys }
|
||||
|
||||
scope :ordered, -> { order(:name) }
|
||||
|
||||
def self.applicable_to(aircraft)
|
||||
direct = joins(:aircraft).where(aircraft: { id: aircraft.id }).select(:id)
|
||||
via_component = joins(:components).where(components: { aircraft_id: aircraft.id }).select(:id)
|
||||
where(id: direct).or(where(id: via_component))
|
||||
end
|
||||
|
||||
def to_s = name
|
||||
def conditional? = compliance_type == "conditional"
|
||||
def bulletin_label = BULLETIN_LABELS[bulletin_type]
|
||||
|
||||
def latest_compliance_for(aircraft)
|
||||
ad_compliances.for_aircraft(aircraft).order(date_complied: :desc, created_at: :desc).first
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
primary_abstract_class
|
||||
end
|
||||
@@ -0,0 +1,73 @@
|
||||
class Component < ApplicationRecord
|
||||
STATUSES = { "SPARE" => "Spare Part", "IN-USE" => "In Service", "DISPOSED" => "Disposed" }.freeze
|
||||
|
||||
belongs_to :aircraft, optional: true
|
||||
belongs_to :parent_component, class_name: "Component", optional: true
|
||||
belongs_to :component_type
|
||||
has_many :child_components, class_name: "Component", foreign_key: :parent_component_id, dependent: :destroy
|
||||
has_many :squawks, dependent: :nullify
|
||||
has_and_belongs_to_many :inspection_types
|
||||
has_and_belongs_to_many :airworthiness_directives
|
||||
|
||||
validates :manufacturer, :model, :date_in_service, presence: true
|
||||
validates :status, inclusion: { in: STATUSES.keys }
|
||||
validate :parent_on_same_aircraft
|
||||
|
||||
scope :in_use, -> { where(status: "IN-USE") }
|
||||
|
||||
def to_s
|
||||
[ component_type&.name, install_location.presence && "(#{install_location})" ].compact.join(" ")
|
||||
end
|
||||
|
||||
def status_label = STATUSES[status]
|
||||
|
||||
def hours_to_tbo
|
||||
tbo_hours - hours_since_overhaul if tbo_hours
|
||||
end
|
||||
|
||||
def hours_to_replacement
|
||||
replacement_hours - hours_since_overhaul if replacement_hours
|
||||
end
|
||||
|
||||
def next_replacement_date
|
||||
overhaul_date + replacement_days.days if replacement_days && overhaul_date
|
||||
end
|
||||
|
||||
# :overdue / :due_soon / :ok / nil (not tracked) for display on the components tab.
|
||||
def life_status
|
||||
statuses = []
|
||||
if replacement_critical?
|
||||
if replacement_hours
|
||||
statuses << rank(hours_to_replacement <= 0, hours_to_replacement <= Airworthiness::HOURS_WARNING)
|
||||
end
|
||||
if (d = next_replacement_date)
|
||||
statuses << rank(Date.current >= d, Date.current + Airworthiness::DAYS_WARNING >= d)
|
||||
end
|
||||
end
|
||||
if tbo_critical? && tbo_hours && !on_condition?
|
||||
statuses << rank(hours_to_tbo <= 0, hours_to_tbo <= Airworthiness::HOURS_WARNING * 5)
|
||||
end
|
||||
return nil if statuses.empty?
|
||||
%i[overdue due_soon ok].find { |s| statuses.include?(s) }
|
||||
end
|
||||
|
||||
# Service reset: always zeroes time since overhaul; optionally the whole
|
||||
# in-service clock when the part was physically replaced.
|
||||
def reset_service!(reset_in_service: false)
|
||||
attrs = { hours_since_overhaul: 0, overhaul_date: Date.current }
|
||||
attrs.merge!(hours_in_service: 0, date_in_service: Date.current) if reset_in_service
|
||||
update!(attrs)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def rank(overdue, soon)
|
||||
overdue ? :overdue : (soon ? :due_soon : :ok)
|
||||
end
|
||||
|
||||
def parent_on_same_aircraft
|
||||
return unless parent_component
|
||||
errors.add(:parent_component, "can't be itself") if parent_component_id == id && id
|
||||
errors.add(:parent_component, "must be on the same aircraft") if parent_component.aircraft_id != aircraft_id
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
class ComponentType < ApplicationRecord
|
||||
has_many :components, dependent: :restrict_with_error
|
||||
validates :name, presence: true, uniqueness: { case_sensitive: false }
|
||||
scope :ordered, -> { order(:name) }
|
||||
def to_s = name
|
||||
end
|
||||
Whitespace-only changes.
@@ -0,0 +1,27 @@
|
||||
# Mirrors the original's ALLOWED_UPLOAD_EXTENSIONS / content-type checks.
|
||||
module UploadValidation
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
ALLOWED_CONTENT_TYPES = %w[
|
||||
image/jpeg image/png image/gif image/webp image/bmp image/tiff
|
||||
application/pdf text/plain application/gpx+xml application/xml text/xml
|
||||
text/csv application/vnd.google-earth.kml+xml application/octet-stream
|
||||
].freeze
|
||||
MAX_SIZE = 50.megabytes
|
||||
|
||||
class_methods do
|
||||
def validates_uploads(*names)
|
||||
validate do
|
||||
names.each do |name|
|
||||
Array(public_send(name).attachments).each do |att|
|
||||
next unless att.blob
|
||||
unless ALLOWED_CONTENT_TYPES.include?(att.blob.content_type)
|
||||
errors.add(name, "#{att.filename} is not an allowed file type")
|
||||
end
|
||||
errors.add(name, "#{att.filename} is too large") if att.blob.byte_size > MAX_SIZE
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
class ConsumableRecord < ApplicationRecord
|
||||
TYPES = { "oil" => "Oil", "fuel" => "Fuel" }.freeze
|
||||
UNITS = { "oil" => "qt", "fuel" => "gal" }.freeze
|
||||
|
||||
belongs_to :aircraft
|
||||
belongs_to :flight_log, optional: true
|
||||
|
||||
validates :record_type, inclusion: { in: TYPES.keys }
|
||||
validates :date, :flight_hours, presence: true
|
||||
validates :quantity_added, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
||||
|
||||
scope :oil, -> { where(record_type: "oil") }
|
||||
scope :fuel, -> { where(record_type: "fuel") }
|
||||
scope :recent_first, -> { order(date: :desc, created_at: :desc) }
|
||||
|
||||
def unit = UNITS[record_type]
|
||||
end
|
||||
@@ -0,0 +1,4 @@
|
||||
class Current < ActiveSupport::CurrentAttributes
|
||||
attribute :session
|
||||
delegate :user, to: :session, allow_nil: true
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
class Document < ApplicationRecord
|
||||
include UploadValidation
|
||||
|
||||
TYPES = {
|
||||
"LOG" => "Logbook", "ALTER" => "Alteration Record", "REPORT" => "Report",
|
||||
"ESTIMATE" => "Estimate", "DISC" => "Discrepancy List", "INVOICE" => "Receipt / Invoice",
|
||||
"AIRCRAFT" => "Aircraft Record", "OTHER" => "Other"
|
||||
}.freeze
|
||||
|
||||
belongs_to :aircraft
|
||||
belongs_to :document_collection, optional: true
|
||||
has_many_attached :pages
|
||||
has_many :logbook_entries, dependent: :nullify
|
||||
|
||||
validates :name, presence: true
|
||||
validates :doc_type, inclusion: { in: TYPES.keys }
|
||||
validates :visibility, inclusion: { in: DocumentCollection::VISIBILITIES.keys }, allow_nil: true
|
||||
validates_uploads :pages
|
||||
normalizes :visibility, with: ->(v) { v.presence }
|
||||
|
||||
scope :ordered, -> { order(:name) }
|
||||
|
||||
def to_s = name
|
||||
def type_label = TYPES[doc_type]
|
||||
|
||||
# nil visibility inherits from the collection; uncollected docs default to private.
|
||||
def effective_visibility
|
||||
visibility || document_collection&.visibility || "private"
|
||||
end
|
||||
|
||||
def visible_to_share?(share)
|
||||
case effective_visibility
|
||||
when "status" then true
|
||||
when "maintenance" then share.maintenance?
|
||||
else false
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
class DocumentCollection < ApplicationRecord
|
||||
VISIBILITIES = { "private" => "Private", "status" => "All share links", "maintenance" => "Maintenance only" }.freeze
|
||||
|
||||
belongs_to :aircraft
|
||||
has_many :documents, dependent: :destroy
|
||||
|
||||
validates :name, presence: true
|
||||
validates :visibility, inclusion: { in: VISIBILITIES.keys }
|
||||
|
||||
scope :ordered, -> { order(starred: :desc, name: :asc) }
|
||||
def to_s = name
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
class FlightLog < ApplicationRecord
|
||||
include UploadValidation
|
||||
|
||||
belongs_to :aircraft
|
||||
belongs_to :user, optional: true
|
||||
has_many :consumable_records, dependent: :nullify
|
||||
has_one_attached :track_log
|
||||
|
||||
validates :date, presence: true
|
||||
validates :tach_time, presence: true, numericality: { greater_than: 0 }
|
||||
validates :hobbs_time, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
|
||||
validates_uploads :track_log
|
||||
before_validation :derive_times
|
||||
|
||||
scope :recent_first, -> { order(date: :desc, created_at: :desc) }
|
||||
|
||||
def route_summary
|
||||
[ departure_location, destination_location ].compact_blank.join(" → ")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Let the pilot enter out/in readings instead of the delta.
|
||||
def derive_times
|
||||
self.tach_time = tach_in - tach_out if tach_time.blank? && tach_in && tach_out
|
||||
self.hobbs_time = hobbs_in - hobbs_out if hobbs_time.blank? && hobbs_in && hobbs_out
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
class InspectionRecord < ApplicationRecord
|
||||
belongs_to :inspection_type
|
||||
belongs_to :aircraft, optional: true
|
||||
belongs_to :component, optional: true
|
||||
belongs_to :logbook_entry, optional: true
|
||||
has_many :ad_compliances, dependent: :nullify
|
||||
|
||||
validates :date, presence: true
|
||||
validate { errors.add(:base, "Must belong to an aircraft or component") unless aircraft_id || component_id }
|
||||
|
||||
scope :recent_first, -> { order(date: :desc, created_at: :desc) }
|
||||
|
||||
# Records belonging to the aircraft, or to any of its components.
|
||||
def self.for_aircraft(aircraft)
|
||||
where(aircraft_id: aircraft.id).or(where(component_id: aircraft.components.select(:id)))
|
||||
end
|
||||
|
||||
# Hours at inspection, falling back to the linked logbook entry.
|
||||
def hours_at_inspection
|
||||
aircraft_hours || logbook_entry&.aircraft_hours_at_entry
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
class InspectionType < ApplicationRecord
|
||||
has_and_belongs_to_many :aircraft
|
||||
has_and_belongs_to_many :components
|
||||
has_many :inspection_records, dependent: :destroy
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
scope :ordered, -> { order(:name) }
|
||||
|
||||
# Applies to the aircraft directly, or to one of its components.
|
||||
def self.applicable_to(aircraft)
|
||||
direct = joins(:aircraft).where(aircraft: { id: aircraft.id }).select(:id)
|
||||
via_component = joins(:components).where(components: { aircraft_id: aircraft.id }).select(:id)
|
||||
where(id: direct).or(where(id: via_component))
|
||||
end
|
||||
|
||||
def to_s = name
|
||||
|
||||
def interval_description
|
||||
return "One-time" unless recurring?
|
||||
parts = []
|
||||
parts << "#{recurring_hours.to_s.sub(/\.0\z/, '')} hrs" if recurring_hours.to_f > 0
|
||||
parts << "#{recurring_months} months" if recurring_months > 0
|
||||
parts << "#{recurring_days} days" if recurring_days > 0
|
||||
parts.any? ? "Every #{parts.join(' / ')}" : "Recurring"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,26 @@
|
||||
class LogbookEntry < ApplicationRecord
|
||||
LOG_TYPES = { "AC" => "Aircraft", "ENG" => "Engine", "PROP" => "Propeller", "OTHER" => "Other" }.freeze
|
||||
ENTRY_TYPES = {
|
||||
"MAINTENANCE" => "Maintenance", "INSPECTION" => "Inspection", "FLIGHT" => "Flight",
|
||||
"HOURS_UPDATE" => "Hours Update", "OTHER" => "Other"
|
||||
}.freeze
|
||||
|
||||
belongs_to :aircraft
|
||||
belongs_to :document, optional: true
|
||||
has_many :inspection_records, dependent: :nullify
|
||||
has_many :ad_compliances, dependent: :nullify
|
||||
|
||||
validates :date, :text, presence: true
|
||||
validates :log_type, inclusion: { in: LOG_TYPES.keys }
|
||||
validates :entry_type, inclusion: { in: ENTRY_TYPES.keys }
|
||||
|
||||
scope :recent_first, -> { order(date: :desc, created_at: :desc) }
|
||||
|
||||
def self.search(q)
|
||||
return all if q.blank?
|
||||
term = "%#{sanitize_sql_like(q)}%"
|
||||
where("text LIKE :t OR signoff_person LIKE :t OR signoff_location LIKE :t", t: term)
|
||||
end
|
||||
|
||||
def to_s = "#{date} — #{LOG_TYPES[log_type]}"
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
class MajorRecord < ApplicationRecord
|
||||
RECORD_TYPES = { "repair" => "Major Repair", "alteration" => "Major Alteration" }.freeze
|
||||
|
||||
belongs_to :aircraft
|
||||
belongs_to :component, optional: true
|
||||
belongs_to :form_337_document, class_name: "Document", optional: true
|
||||
belongs_to :stc_document, class_name: "Document", optional: true
|
||||
belongs_to :logbook_entry, optional: true
|
||||
|
||||
validates :title, :date_performed, presence: true
|
||||
validates :record_type, inclusion: { in: RECORD_TYPES.keys }
|
||||
|
||||
scope :recent_first, -> { order(date_performed: :desc, created_at: :desc) }
|
||||
|
||||
def type_label = RECORD_TYPES[record_type]
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
class Session < ApplicationRecord
|
||||
belongs_to :user
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
class ShareToken < ApplicationRecord
|
||||
PRIVILEGES = { "status" => "Current Status", "maintenance" => "Maintenance Detail" }.freeze
|
||||
MAX_PER_AIRCRAFT = 10
|
||||
|
||||
belongs_to :aircraft
|
||||
belongs_to :created_by, class_name: "User", optional: true
|
||||
has_secure_token :token, length: 32
|
||||
|
||||
validates :privilege, inclusion: { in: PRIVILEGES.keys }
|
||||
validate :within_limit, on: :create
|
||||
|
||||
scope :active, -> { where(expires_at: nil).or(where(expires_at: Time.current..)) }
|
||||
|
||||
def self.find_active(token)
|
||||
active.find_by(token: token.to_s)
|
||||
end
|
||||
|
||||
def maintenance? = privilege == "maintenance"
|
||||
def expired? = expires_at.present? && expires_at < Time.current
|
||||
|
||||
private
|
||||
|
||||
def within_limit
|
||||
if aircraft && aircraft.share_tokens.count >= MAX_PER_AIRCRAFT
|
||||
errors.add(:base, "An aircraft can have at most #{MAX_PER_AIRCRAFT} share links")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
class Squawk < ApplicationRecord
|
||||
include UploadValidation
|
||||
|
||||
PRIORITIES = { 0 => "Ground Aircraft", 1 => "Fix Soon", 2 => "Fix at Next Inspection", 3 => "Fix Eventually" }.freeze
|
||||
PRIORITY_COLORS = { 0 => "red", 1 => "orange", 2 => "blue", 3 => "gray" }.freeze
|
||||
|
||||
belongs_to :aircraft
|
||||
belongs_to :component, optional: true
|
||||
belongs_to :reported_by, class_name: "User", optional: true
|
||||
has_one_attached :attachment
|
||||
|
||||
validates :priority, inclusion: { in: PRIORITIES.keys }
|
||||
validates :issue_reported, presence: true
|
||||
validates_uploads :attachment
|
||||
|
||||
scope :active, -> { where(resolved: false) }
|
||||
scope :resolved, -> { where(resolved: true) }
|
||||
scope :by_priority, -> { order(:priority, created_at: :desc) }
|
||||
|
||||
def priority_label = PRIORITIES[priority]
|
||||
def priority_color = PRIORITY_COLORS[priority]
|
||||
def grounding? = priority.zero? && !resolved?
|
||||
|
||||
def resolve!(notes: nil)
|
||||
update!(resolved: true, resolved_at: Time.current, notes: [ self.notes, notes ].compact_blank.join("\n\n"))
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
class User < ApplicationRecord
|
||||
has_secure_password
|
||||
has_many :sessions, dependent: :destroy
|
||||
has_many :aircraft_roles, dependent: :destroy
|
||||
has_many :aircraft, through: :aircraft_roles
|
||||
|
||||
normalizes :email_address, with: ->(e) { e.strip.downcase }
|
||||
|
||||
validates :email_address, presence: true, uniqueness: true,
|
||||
format: { with: URI::MailTo::EMAIL_REGEXP }
|
||||
validates :password, length: { minimum: 8 }, allow_nil: true
|
||||
|
||||
# Aircraft visible to this user: admins see the whole fleet.
|
||||
def accessible_aircraft
|
||||
admin? ? Aircraft.all : aircraft
|
||||
end
|
||||
|
||||
# 'admin' | 'owner' | 'pilot' | nil
|
||||
def role_for(aircraft)
|
||||
return "admin" if admin?
|
||||
aircraft_roles.find_by(aircraft: aircraft)&.role
|
||||
end
|
||||
|
||||
def display_name
|
||||
name.presence || email_address
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user