mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Overview shows coming due and the last flight; intervals in days, months or years
- Overview: "Coming due" (next 30 days or 10 hours) and "Last flight" (the most recent day's legs, meters at the end, fuel and oil left). - Component replace / TBO / inspect intervals, inspection types and ADs take a number and a days / months / years choice (CalendarInterval). Components gain month columns (years stored as 12 months; months land on the same date); inspection types and ADs keep end-of-month. ADs now honour days. - Due list: hours already run out count as overdue; a component covered by an inspection type no longer gets its own "Inspect every" item. - Loading calculator moved to a partial; no limit warnings until a load is entered. Header buttons don't wrap on phones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
215efe6814
commit
31ac991bab
37 files changed
+385
-75
No files matched your search
@@ -6,6 +6,9 @@ class AirworthinessDirective < ApplicationRecord
|
||||
}.freeze
|
||||
BULLETIN_LABELS = { "ad" => "AD", "saib" => "SAIB", "sb" => "SB", "alert" => "Alert", "other" => "Bulletin" }.freeze
|
||||
|
||||
include CalendarInterval
|
||||
calendar_interval :recurring, days: :recurring_days, months: :recurring_months, blank: 0
|
||||
|
||||
belongs_to :document, optional: true
|
||||
include SharedRecordHistory # has_and_belongs_to_many :aircraft and :components, with history
|
||||
has_many :ad_compliances, dependent: :destroy
|
||||
@@ -45,9 +48,9 @@ class AirworthinessDirective < ApplicationRecord
|
||||
def conditional? = compliance_type == "conditional"
|
||||
def bulletin_label = BULLETIN_LABELS[bulletin_type]
|
||||
|
||||
# Recurrence rule for next-due math (AD status uses hours and months).
|
||||
# Recurrence rule for next-due math.
|
||||
def due_rule
|
||||
{ hours: recurring_hours.to_f, months: recurring_months, days: 0 } if recurring?
|
||||
{ hours: recurring_hours.to_f, months: recurring_months, days: recurring_days } if recurring?
|
||||
end
|
||||
|
||||
def latest_compliance_for(aircraft)
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
class Component < ApplicationRecord
|
||||
include Versioned
|
||||
include CalendarInterval
|
||||
STATUSES = { "SPARE" => "Spare Part", "IN-USE" => "In Service", "DISPOSED" => "Disposed" }.freeze
|
||||
# What "since overhaul" counts from, and how logbooks abbreviate it.
|
||||
OVERHAUL_KINDS = { "major" => "Major overhaul", "factory_overhaul" => "Factory overhaul", "factory_new" => "Factory new or rebuilt (zero time)" }.freeze
|
||||
OVERHAUL_ABBREVIATIONS = { "major" => "SMOH", "factory_overhaul" => "SFOH", "factory_new" => "SNEW" }.freeze
|
||||
|
||||
calendar_interval :replacement, days: :replacement_days, months: :replacement_months
|
||||
calendar_interval :tbo, days: :tbo_days, months: :tbo_months
|
||||
calendar_interval :inspection, days: :inspection_days, months: :inspection_months
|
||||
|
||||
belongs_to :aircraft, optional: true
|
||||
belongs_to :parent_component, class_name: "Component", optional: true
|
||||
belongs_to :component_type
|
||||
@@ -66,7 +71,7 @@ class Component < ApplicationRecord
|
||||
def expired?(today = Date.current) = expires_on.present? && today > expires_on
|
||||
|
||||
def next_replacement_date
|
||||
overhaul_date + replacement_days.days if replacement_days && overhaul_date
|
||||
replacement_after(overhaul_date)
|
||||
end
|
||||
|
||||
# :overdue / :due_soon / :ok / nil (not tracked) for display on the components tab.
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
# A calendar interval entered as a number and a unit (days, months or years)
|
||||
# and stored in a days column and a months column (years as 12 months); one
|
||||
# of the two is used.
|
||||
#
|
||||
# calendar_interval :replacement, days: :replacement_days, months: :replacement_months
|
||||
#
|
||||
# gives replacement_every / replacement_unit (for the form), and
|
||||
# replacement_interval_label ("1 year", "6 months", "90 days").
|
||||
module CalendarInterval
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
UNITS = { "days" => "days", "months" => "months", "years" => "years" }.freeze
|
||||
|
||||
# [count, unit] for display, from the stored columns.
|
||||
def self.split(days, months)
|
||||
months = months.to_i
|
||||
days = days.to_i
|
||||
if months.positive? then months % 12 == 0 ? [ months / 12, "years" ] : [ months, "months" ]
|
||||
elsif days.positive? then [ days, "days" ]
|
||||
end
|
||||
end
|
||||
|
||||
def self.label(days, months)
|
||||
count, unit = split(days, months)
|
||||
count && "#{count} #{count == 1 ? unit.singularize : unit}"
|
||||
end
|
||||
|
||||
class_methods do
|
||||
# blank: what an empty interval is stored as (nil, or 0 for NOT NULL columns).
|
||||
def calendar_interval(name, days:, months:, blank: nil)
|
||||
every_ivar = :"@#{name}_every"
|
||||
unit_ivar = :"@#{name}_unit"
|
||||
|
||||
define_method(:"#{name}_every") do
|
||||
instance_variable_defined?(every_ivar) ? instance_variable_get(every_ivar) : CalendarInterval.split(self[days], self[months])&.first
|
||||
end
|
||||
define_method(:"#{name}_unit") do
|
||||
(instance_variable_get(unit_ivar) if instance_variable_defined?(unit_ivar)) ||
|
||||
CalendarInterval.split(self[days], self[months])&.last || "months"
|
||||
end
|
||||
define_method(:"#{name}_every=") { |v| instance_variable_set(every_ivar, v) }
|
||||
define_method(:"#{name}_unit=") { |v| instance_variable_set(unit_ivar, v) }
|
||||
define_method(:"#{name}_interval_label") { CalendarInterval.label(self[days], self[months]) }
|
||||
define_method(:"#{name}_calendar?") { self[months].to_i.positive? || self[days].to_i.positive? }
|
||||
# The date one interval after `date`: months to the same day (or the
|
||||
# month's last day), days added.
|
||||
define_method(:"#{name}_after") do |date|
|
||||
if date.nil? then nil
|
||||
elsif self[months].to_i.positive? then date >> self[months].to_i
|
||||
elsif self[days].to_i.positive? then date + self[days].to_i
|
||||
end
|
||||
end
|
||||
|
||||
# Store the entered interval; a bad number leaves the columns alone and
|
||||
# is reported by the validation below.
|
||||
before_validation do
|
||||
next unless instance_variable_defined?(every_ivar)
|
||||
raw = instance_variable_get(every_ivar).to_s.strip
|
||||
count = Integer(raw, 10, exception: false)
|
||||
unit = public_send(:"#{name}_unit")
|
||||
if raw.empty?
|
||||
self[days] = blank
|
||||
self[months] = blank
|
||||
elsif count&.positive? && UNITS.key?(unit)
|
||||
self[days] = unit == "days" ? count : blank
|
||||
self[months] = { "months" => count, "years" => count * 12 }.fetch(unit, blank)
|
||||
end
|
||||
end
|
||||
validate do
|
||||
next unless instance_variable_defined?(every_ivar)
|
||||
raw = instance_variable_get(every_ivar).to_s.strip
|
||||
next if raw.empty? || Integer(raw, 10, exception: false)&.positive?
|
||||
errors.add(:"#{name}_every", "must be a whole number more than 0")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -12,6 +12,8 @@ class InspectionType < ApplicationRecord
|
||||
# Short form for the airworthiness status ("VFR only").
|
||||
LIMITATIONS = { "no_ifr" => "VFR only", "no_transponder" => "No transponder" }.freeze
|
||||
|
||||
include CalendarInterval
|
||||
calendar_interval :recurring, days: :recurring_days, months: :recurring_months, blank: 0
|
||||
include SharedRecordHistory # has_and_belongs_to_many :aircraft and :components, with history
|
||||
has_many :inspection_records, dependent: :destroy
|
||||
belongs_to :satisfies, class_name: "InspectionType", optional: true
|
||||
@@ -54,7 +56,8 @@ class InspectionType < ApplicationRecord
|
||||
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_interval_label if recurring_months > 0 && recurring_days.zero?
|
||||
parts << "#{recurring_months} months" if recurring_months > 0 && recurring_days > 0
|
||||
parts << "#{recurring_days} days" if recurring_days > 0
|
||||
parts.any? ? "Every #{parts.join(' / ')}" : "Recurring"
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user