mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Record history for ADs and inspection types (applicability links and changes), with restore
Shared records keep applicability in join tables, so linking to the airframe or a component, unlinking, edits to the shared record and a component's deletion are now appended to each affected aircraft's hash chain. Owners can restore a removed link from Activity -> Record history. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d5d06d4169
commit
ba0455b149
10 files changed
+164
-7
No files matched your search
@@ -7,8 +7,7 @@ class AirworthinessDirective < ApplicationRecord
|
||||
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
|
||||
include SharedRecordHistory # has_and_belongs_to_many :aircraft and :components, with history
|
||||
has_many :ad_compliances, dependent: :destroy
|
||||
|
||||
validates :name, :short_description, presence: true
|
||||
|
||||
@@ -19,6 +19,8 @@ class Component < ApplicationRecord
|
||||
has_many :logbook_entry_parts, dependent: :destroy
|
||||
has_and_belongs_to_many :inspection_types
|
||||
has_and_belongs_to_many :airworthiness_directives
|
||||
# Its AD and inspection links go with it; keep that in the record history.
|
||||
before_destroy(prepend: true) { (airworthiness_directives.to_a + inspection_types.to_a).each { _1.history_component_deleted(self) } }
|
||||
|
||||
validates :manufacturer, :model, :date_in_service, presence: true
|
||||
validates :status, inclusion: { in: STATUSES.keys }
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# Record history for records shared between aircraft (ADs and inspection
|
||||
# types). Their applicability lives in plain join tables, so linking one to
|
||||
# an aircraft or a component, unlinking it, and changes to the shared record
|
||||
# are written to each affected aircraft's history chain here (AC 120-78B 3.2.2;
|
||||
# for ADs, the AD status kept under 14 CFR 91.417(a)(2)(v)).
|
||||
module SharedRecordHistory
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
IGNORED = %w[created_at updated_at].freeze
|
||||
|
||||
included do
|
||||
has_and_belongs_to_many :aircraft, after_add: :history_link_aircraft, after_remove: :history_unlink_aircraft
|
||||
has_and_belongs_to_many :components, after_add: :history_link_component, after_remove: :history_unlink_component
|
||||
after_update { (c = saved_changes.except(*IGNORED)).any? && linked_aircraft_ids.each { |id| history!(id, "update", c) } }
|
||||
before_destroy { linked_aircraft_ids.each { |id| history!(id, "destroy", attributes.except(*IGNORED)) } }
|
||||
end
|
||||
|
||||
# Aircraft this record applies to, directly or through a component.
|
||||
def linked_aircraft_ids = (aircraft_ids + components.map(&:aircraft_id)).compact.uniq
|
||||
|
||||
# Logs that a component it applied to is being deleted (the join row goes
|
||||
# with the component, without the callbacks above).
|
||||
def history_component_deleted(component) = history_link(component.aircraft_id, "unlink", component, reason: "component deleted")
|
||||
|
||||
private
|
||||
|
||||
def history_link_aircraft(aircraft) = history_link(aircraft.id, "link", nil)
|
||||
def history_unlink_aircraft(aircraft) = history_link(aircraft.id, "unlink", nil)
|
||||
def history_link_component(component) = history_link(component.aircraft_id, "link", component)
|
||||
def history_unlink_component(component) = history_link(component.aircraft_id, "unlink", component)
|
||||
|
||||
def history_link(aircraft_id, event, component, reason: nil)
|
||||
return unless persisted? && aircraft_id
|
||||
changes = { "record" => to_s, "applies_to" => component ? "component" : "airframe" }
|
||||
changes.merge!("component_id" => component.id, "component" => "#{component} #{component.model}".strip) if component
|
||||
changes["reason"] = reason if reason
|
||||
history!(aircraft_id, event, changes)
|
||||
end
|
||||
|
||||
def history!(aircraft_id, event, changes)
|
||||
RecordVersion.append!(item: self, aircraft_id: aircraft_id, event: event, changes: changes.as_json)
|
||||
end
|
||||
end
|
||||
@@ -12,8 +12,7 @@ class InspectionType < ApplicationRecord
|
||||
# Short form for the airworthiness status ("VFR only").
|
||||
LIMITATIONS = { "no_ifr" => "VFR only", "no_transponder" => "No transponder" }.freeze
|
||||
|
||||
has_and_belongs_to_many :aircraft
|
||||
has_and_belongs_to_many :components
|
||||
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
|
||||
has_many :satisfied_by, class_name: "InspectionType", foreign_key: :satisfies_id, inverse_of: :satisfies, dependent: :nullify
|
||||
|
||||
Reference in new issue
Block a user