mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
What people can do on an aircraft now comes from their role there, and administrators set what each role allows (Admin → Roles). - Roles are installation-wide sets of permissions (31, grouped as flying, squawks and notes, maintenance records, imports and exports, the aircraft, notifications). Seeing an aircraft's records needs none. - Built in: Owner (always every permission, so an aircraft can always be managed), Mechanic (shop maintenance: logbook, signing, components, inspections, ADs, squawks, reminders), Pilot (what pilots could do before) and Viewer (view only). Administrators can change all but Owner's, and add their own; a role can be deleted once nobody has it. - Existing owners and pilots keep exactly what they could do. - Admin → Users → a user lists their role on every aircraft, to change, remove or add. On the aircraft's Access page, anyone who manages people hands out roles, but only an Owner can give or take away Owner, nobody changes their own (except administrators), and an aircraft always keeps an Owner (AircraftAccess). - Controllers declare the permission each action needs; a request that changes something with none declared is refused (and raises in development and tests), and a test walks every route to check. - Weekly reminders and overdue alerts go to roles that get reminders; the DRS reminder also needs Manage ADs. The API follows the person's role; its owners_only error is now not_permitted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
2.9 KiB
Ruby
72 lines
2.9 KiB
Ruby
class FlightLog < ApplicationRecord
|
|
include UploadValidation
|
|
|
|
belongs_to :aircraft
|
|
belongs_to :user, optional: true
|
|
has_many :consumable_records, dependent: :nullify
|
|
# Capture AirTrail ids before the sync rows are deleted, so the remote flights can be removed too.
|
|
before_destroy :remember_air_trail_flights, prepend: true
|
|
has_many :air_trail_syncs, dependent: :delete_all
|
|
has_one_attached :track_log
|
|
has_one_attached :engine_data # engine monitor recording (CGR-30P CSV)
|
|
|
|
validates :date, presence: true
|
|
validates :tach_time, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
|
validates :hobbs_time, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
|
|
validates_uploads :track_log, :engine_data
|
|
validate :engine_data_readable
|
|
before_validation :derive_times
|
|
normalizes :departure_location, :destination_location, with: ->(code) { code.strip.upcase.presence }
|
|
after_commit :push_to_air_trail, on: %i[create update]
|
|
after_commit :delete_from_air_trail, on: :destroy
|
|
|
|
scope :recent_first, -> { order(date: :desc, created_at: :desc) }
|
|
|
|
def route_summary
|
|
[ departure_location, destination_location ].compact_blank.join(" → ")
|
|
end
|
|
|
|
# The meter readings that move the aircraft's and components' hours.
|
|
METERS = %w[tach_out tach_in tach_time hobbs_out hobbs_in hobbs_time].freeze
|
|
|
|
# Its readings can be corrected only while it's the last flight logged and
|
|
# nothing has changed the hours since (an hours update, an import): only
|
|
# then is the aircraft's time just this flight's on top of the one before.
|
|
def meters_editable?
|
|
aircraft.flight_logs.maximum(:id) == id && (tach_in.nil? || tach_in == aircraft.tach_time)
|
|
end
|
|
|
|
private
|
|
|
|
# Reject files the engine data parser can't read, before they're stored.
|
|
def engine_data_readable
|
|
change = attachment_changes["engine_data"]
|
|
io = change.attachable if change.is_a?(ActiveStorage::Attached::Changes::CreateOne)
|
|
io = io[:io] if io.is_a?(Hash)
|
|
return unless io.respond_to?(:read)
|
|
content = io.read
|
|
io.rewind
|
|
EngineDataParser.new(content, filename: engine_data.filename.to_s).parse
|
|
rescue EngineDataParser::Error => e
|
|
errors.add(:engine_data, e.message)
|
|
end
|
|
|
|
def push_to_air_trail
|
|
AirTrailPushJob.perform_later(id) if user_id && AirTrailConnection.active.exists?(user_id: user_id)
|
|
end
|
|
|
|
def remember_air_trail_flights
|
|
@air_trail_remote = air_trail_syncs.where.not(remote_id: nil).pluck(:air_trail_connection_id, :remote_id)
|
|
end
|
|
|
|
def delete_from_air_trail
|
|
Array(@air_trail_remote).each { |connection_id, remote_id| AirTrailDeleteJob.perform_later(connection_id, remote_id) }
|
|
end
|
|
|
|
# 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
|