Files
simple-aircraft-manager-rails/app/models/logbook_template.rb
T
rickyandClaude Opus 5.5 72e1d7760f Editable logbook templates; component times follow the airframe time
- Templates move to the database (Admin -> Logbook templates: add, edit,
  delete, restore the built-in ones). The built-in preventive maintenance
  set is added once on first use; deletions stick. A template can set the
  entry type, logbook (picking the matching component) and certificate
- Template list shows "Name — Part 43 App. A (c)(6), (c)(23)" instead of
  the doubled parentheses
- Component total time and time since overhaul are worked out from the
  entry's airframe total time (current times shifted by the difference);
  blank when the entry predates the overhaul; typing them stops the
  automatic updates

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

38 lines
1.6 KiB
Ruby

# Wording to start a logbook entry from (Admin → Logbook templates). Picking
# one on the entry form fills the text and, when set, the entry type, the
# logbook and the kind of certificate.
class LogbookTemplate < ApplicationRecord
DEFAULTS_FILE = Rails.root.join("config/logbook_templates.yml")
validates :name, :text, presence: true
validates :entry_type, inclusion: { in: LogbookEntry::ENTRY_TYPES.keys }
validates :log_type, inclusion: { in: LogbookEntry::LOG_TYPES.keys }, allow_blank: true
validates :certificate_type, inclusion: { in: LogbookEntry::CERTIFICATE_TYPES.keys }, allow_blank: true
normalizes :log_type, :certificate_type, :reference, with: ->(v) { v.strip.presence }
scope :ordered, -> { order(:position, :name) }
def label = reference.present? ? "#{name} — #{reference}" : name
def self.defaults = YAML.safe_load_file(DEFAULTS_FILE)
# Adds the built-in templates that aren't here (matched by name). Returns
# how many were added.
def self.restore_defaults!
existing = pluck(:name).to_set
next_position = (maximum(:position) || 0) + 1
added = defaults.reject { existing.include?(_1["name"]) }
added.each_with_index { |d, i| create!(d.slice("name", "reference", "text", "log_type", "certificate_type").merge("position" => next_position + i)) }
added.size
end
# The built-in set, once: after that, admins' deletions stick.
def self.ensure_defaults!
return if Setting.integer("logbook_templates_seeded") == 1
transaction do
restore_defaults!
Setting.update_integers("logbook_templates_seeded" => "1")
end
end
end