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:
rickyandClaude Opus 5.5 committed 2026-11-04 17:59:08 -05:00
commit 38df19c331
264 files changed
+8365

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :headless_chrome, screen_size: [ 1400, 1400 ]
end
View File
Whitespace-only changes.
View File
Whitespace-only changes.
View File
Whitespace-only changes.
View File
Whitespace-only changes.
+104
View File
@@ -0,0 +1,104 @@
require "test_helper"
# Renders every page with realistic data to catch view/route errors.
class PagesTest < ActionDispatch::IntegrationTest
setup do
@owner = create_user("owner@example.com", admin: false)
@a = create_aircraft(owner: @owner, tail_number: "N100AB")
@engine = create_component(@a)
@oil = create_component(@a, component_type: component_type("Oil"), parent_component: @engine,
replacement_critical: true, replacement_hours: 50, hours_since_overhaul: 45, overhaul_date: Date.current)
@squawk = @a.squawks.create!(priority: 1, issue_reported: "Nav light out", reported_by: @owner)
@a.squawks.create!(priority: 3, issue_reported: "Old one", resolved: true, resolved_at: Time.current)
@note = @a.notes.create!(text: "Hangar 4", user: @owner, public: true)
@entry = @a.logbook_entries.create!(date: Date.current, text: "Oil changed", aircraft_hours_at_entry: 1000)
@collection = @a.document_collections.create!(name: "Logbooks", visibility: "status")
@doc = @a.documents.create!(name: "W&B", document_collection: @collection)
@doc.pages.attach(io: StringIO.new("hello"), filename: "wb.txt", content_type: "text/plain")
@itype = InspectionType.create!(name: "Annual", recurring: true, recurring_months: 12)
@itype.aircraft << @a
@irec = @a.inspection_records.create!(inspection_type: @itype, date: Date.current, aircraft_hours: 1000)
@ad = AirworthinessDirective.create!(name: "2020-01-01", short_description: "Check", recurring: true, recurring_hours: 100)
@ad.aircraft << @a
@adc = @ad.ad_compliances.create!(aircraft: @a, date_complied: Date.current, compliance_notes: "Done", next_due_at_time: 1100)
@major = @a.major_records.create!(record_type: "alteration", title: "LED lights", date_performed: Date.current)
@flight = @a.flight_logs.create!(date: Date.current, tach_time: 1.2)
@cr = @a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: 1000)
@token = @a.share_tokens.create!(privilege: "maintenance", label: "Mechanic")
@a.log_event!("aircraft", "Created", user: @owner)
sign_in @owner
end
test "owner can render every page" do
a = @a
paths = [
root_path, aircraft_path(a), new_aircraft_path, edit_aircraft_path(a),
aircraft_components_path(a), aircraft_components_path(a, disposed: 1), new_aircraft_component_path(a),
aircraft_component_path(a, @engine), edit_aircraft_component_path(a, @oil),
aircraft_squawks_path(a), new_aircraft_squawk_path(a), edit_aircraft_squawk_path(a, @squawk),
aircraft_notes_path(a), new_aircraft_note_path(a), edit_aircraft_note_path(a, @note),
aircraft_flight_logs_path(a), new_aircraft_flight_log_path(a), aircraft_flight_log_path(a, @flight),
aircraft_consumable_records_path(a), new_aircraft_consumable_record_path(a, type: "fuel"), edit_aircraft_consumable_record_path(a, @cr),
aircraft_logbook_entries_path(a), aircraft_logbook_entries_path(a, q: "oil", log_type: "AC"),
new_aircraft_logbook_entry_path(a), aircraft_logbook_entry_path(a, @entry), edit_aircraft_logbook_entry_path(a, @entry),
aircraft_documents_path(a), new_aircraft_document_path(a), aircraft_document_path(a, @doc), edit_aircraft_document_path(a, @doc),
new_aircraft_document_collection_path(a), edit_aircraft_document_collection_path(a, @collection),
aircraft_inspections_path(a), new_aircraft_inspection_type_path(a), edit_aircraft_inspection_type_path(a, @itype),
new_aircraft_inspection_record_path(a, inspection_type_id: @itype.id), edit_aircraft_inspection_record_path(a, @irec),
aircraft_airworthiness_directives_path(a), new_aircraft_airworthiness_directive_path(a),
edit_aircraft_airworthiness_directive_path(a, @ad), new_aircraft_ad_compliance_path(a, ad_id: @ad.id),
edit_aircraft_ad_compliance_path(a, @adc),
aircraft_major_records_path(a), new_aircraft_major_record_path(a), aircraft_major_record_path(a, @major),
edit_aircraft_major_record_path(a, @major),
aircraft_aircraft_roles_path(a), aircraft_events_path(a), aircraft_events_path(a, category: "aircraft"),
edit_profile_path
]
paths.each do |path|
get path
assert_response :success, "GET #{path} → #{response.status}"
end
end
test "maintenance share link renders without login and shows history" do
delete session_path
get shared_path(@token.token)
assert_response :success
assert_includes response.body, "N100AB"
assert_includes response.body, "Oil changed" # logbook
assert_includes response.body, "LED lights" # major records
assert_includes response.body, "Hangar 4" # public note
assert_includes response.body, "W&amp;B" # status-visible doc
end
test "status share link hides maintenance history" do
t = @a.share_tokens.create!(privilege: "status")
@a.notes.create!(text: "Secret combo", public: false)
delete session_path
get shared_path(t.token)
assert_response :success
assert_not_includes response.body, "Oil changed"
assert_not_includes response.body, "LED lights"
assert_not_includes response.body, "Secret combo"
assert_includes response.body, "Nav light out"
end
test "expired or unknown share link is 404" do
t = @a.share_tokens.create!(privilege: "status", expires_at: 1.minute.ago)
get shared_path(t.token)
assert_response :not_found
get shared_path("nope")
assert_response :not_found
end
test "admin pages" do
admin = create_user("boss@example.com", admin: true)
sign_in admin
[ admin_users_path, new_admin_user_path, edit_admin_user_path(@owner),
admin_component_types_path, new_admin_component_type_path ].each do |path|
get path
assert_response :success, path
end
get aircraft_path(@a) # admins see every aircraft
assert_response :success
end
end
+126
View File
@@ -0,0 +1,126 @@
require "test_helper"
class PermissionsTest < ActionDispatch::IntegrationTest
setup do
@owner = create_user
@pilot = create_user
@outsider = create_user
@a = create_aircraft(owner: @owner, pilot: @pilot, tach_time: 1000)
@engine = create_component(@a, hours_in_service: 100, hours_since_overhaul: 100)
end
test "outsider gets 404 for aircraft and nested resources" do
sign_in @outsider
get aircraft_path(@a)
assert_response :not_found
get aircraft_components_path(@a)
assert_response :not_found
patch hours_aircraft_path(@a), params: { tach_time: 2000 }
assert_response :not_found
assert_equal 1000, @a.reload.tach_time
end
test "fleet only lists accessible aircraft" do
other = create_aircraft(owner: @outsider, tail_number: "N999ZZ")
sign_in @pilot
get root_path
assert_includes response.body, @a.tail_number
assert_not_includes response.body, other.tail_number
end
test "pilot can update hours, log flights, report squawks, add notes and oil" do
sign_in @pilot
patch hours_aircraft_path(@a), params: { tach_time: "1010" }
assert_equal 1010, @a.reload.tach_time
assert_equal 110, @engine.reload.hours_in_service
post aircraft_flight_logs_path(@a), params: { flight_log: { date: Date.current, tach_out: 1010, tach_in: 1011.5, oil_added: 1 } }
assert_redirected_to aircraft_flight_logs_path(@a)
assert_equal 1011.5, @a.reload.tach_time
assert_equal 1, @a.consumable_records.oil.count
post aircraft_squawks_path(@a), params: { squawk: { priority: 0, issue_reported: "Mag drop" } }
assert_equal @pilot, @a.squawks.last.reported_by
assert_equal "RED", @a.reload.airworthiness.status
post aircraft_notes_path(@a), params: { aircraft_note: { text: "Low on fuel" } }
assert_equal 1, @a.notes.count
post aircraft_consumable_records_path(@a), params: { consumable_record: { record_type: "fuel", date: Date.current, quantity_added: 20, flight_hours: 1011.5 } }
assert_equal 1, @a.consumable_records.fuel.count
end
test "pilot cannot edit owner-level data" do
squawk = @a.squawks.create!(priority: 1, issue_reported: "x")
sign_in @pilot
patch resolve_aircraft_squawk_path(@a, squawk)
assert_not squawk.reload.resolved?
post aircraft_components_path(@a), params: { component: { component_type_id: component_type.id, manufacturer: "x", model: "y", date_in_service: Date.current } }
assert_equal 1, @a.components.count
delete aircraft_component_path(@a, @engine)
assert Component.exists?(@engine.id)
patch aircraft_path(@a), params: { aircraft: { tail_number: "HACKED" } }
assert_not_equal "HACKED", @a.reload.tail_number
post aircraft_share_tokens_path(@a), params: { share_token: { privilege: "maintenance" } }
assert_equal 0, @a.share_tokens.count
get aircraft_aircraft_roles_path(@a)
assert_response :redirect
post aircraft_logbook_entries_path(@a), params: { logbook_entry: { date: Date.current, text: "x" } }
assert_equal 0, @a.logbook_entries.count
end
test "owner manages roles with last-owner protection" do
sign_in @owner
owner_role = @a.roles.find_by(user: @owner)
patch aircraft_aircraft_role_path(@a, owner_role), params: { role: "pilot" }
assert_equal "owner", owner_role.reload.role
newbie = create_user("newbie@example.com")
post aircraft_aircraft_roles_path(@a), params: { email_address: "NewBie@example.com ", role: "owner" }
assert_equal "owner", @a.roles.find_by(user: newbie).role
delete aircraft_aircraft_role_path(@a, owner_role)
assert AircraftRole.exists?(owner_role.id), "can't remove self"
end
test "owner full workflow: component service, AD compliance, inspection" do
sign_in @owner
post reset_service_aircraft_component_path(@a, @engine), params: { reset_in_service: "1" }
assert_equal 0, @engine.reload.hours_in_service
post aircraft_airworthiness_directives_path(@a), params: {
airworthiness_directive: { name: "2024-05-05", short_description: "Muffler", recurring: "1", recurring_hours: 100 },
applies_to_airframe: "0", component_ids: [ @engine.id ]
}
ad = AirworthinessDirective.find_by!(name: "2024-05-05")
assert_equal [ @engine ], ad.components.to_a
assert_equal "RED", @a.reload.airworthiness.status
post aircraft_ad_compliances_path(@a), params: { ad_compliance: { airworthiness_directive_id: ad.id, date_complied: Date.current, compliance_notes: "Inspected", next_due_at_time: 1100 } }
assert_equal "GREEN", @a.reload.airworthiness.status
post aircraft_inspection_types_path(@a), params: { inspection_type: { name: "Annual", recurring: "1", recurring_months: 12, required: "1" }, applies_to_airframe: "1" }
assert_equal "RED", @a.reload.airworthiness.status
it = InspectionType.find_by!(name: "Annual")
post aircraft_inspection_records_path(@a), params: { inspection_record: { inspection_type_id: it.id, date: Date.current, aircraft_hours: 1000 } }
assert_equal "GREEN", @a.reload.airworthiness.status
delete aircraft_airworthiness_directive_path(@a, ad)
assert_not @a.reload.applicable_ads.exists?(ad.id)
end
test "owner cannot reach another aircraft's records through their own aircraft" do
other = create_aircraft(owner: @outsider)
other_engine = create_component(other)
sign_in @owner
get aircraft_component_path(@a, other_engine)
assert_response :not_found
end
end
+22
View File
@@ -0,0 +1,22 @@
require "test_helper"
class SetupTest < ActionDispatch::IntegrationTest
test "first run creates admin, then setup is closed" do
get root_path
assert_redirected_to new_setup_path
post setup_path, params: { user: { name: "Ricky", email_address: "a@b.com", password: "password123", password_confirmation: "password123" } }
assert_redirected_to root_path
assert User.last.admin?
get new_setup_path
assert_redirected_to root_path
end
test "create aircraft makes creator the owner" do
user = create_user
sign_in user
post aircraft_index_path, params: { aircraft: { tail_number: "N1", tach_time: 50 } }
a = Aircraft.find_by!(tail_number: "N1")
assert_redirected_to aircraft_path(a)
assert_equal "owner", user.role_for(a)
end
end
View File
Whitespace-only changes.
@@ -0,0 +1,7 @@
# Preview all emails at http://localhost:3000/rails/mailers/passwords_mailer
class PasswordsMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/passwords_mailer/reset
def reset
PasswordsMailer.reset(User.take)
end
end
View File
Whitespace-only changes.
+92
View File
@@ -0,0 +1,92 @@
require "test_helper"
class AirworthinessTest < ActiveSupport::TestCase
setup do
@aircraft = create_aircraft(tach_time: 1000)
end
test "clean aircraft is green" do
assert_equal "GREEN", @aircraft.airworthiness.status
end
test "unresolved priority-0 squawk grounds aircraft" do
@aircraft.squawks.create!(priority: 0, issue_reported: "Flat tire")
assert_equal "RED", Airworthiness.new(@aircraft).call.status
end
test "lower-priority squawk does not affect status" do
@aircraft.squawks.create!(priority: 1, issue_reported: "Light out")
assert_equal "GREEN", Airworthiness.new(@aircraft).call.status
end
test "required inspection never completed is red" do
InspectionType.create!(name: "Annual", recurring: true, recurring_months: 12).aircraft << @aircraft
result = Airworthiness.new(@aircraft).call
assert_equal "RED", result.status
assert_match "Never Completed", result.issues.first.title
end
test "annual due at end of month, orange within 30 days" do
type = InspectionType.create!(name: "Annual", recurring: true, recurring_months: 12)
type.aircraft << @aircraft
@aircraft.inspection_records.create!(inspection_type: type, date: Date.new(2025, 6, 10), aircraft_hours: 900)
assert_equal "GREEN", Airworthiness.new(@aircraft, today: Date.new(2026, 5, 30)).call.status
assert_equal "ORANGE", Airworthiness.new(@aircraft, today: Date.new(2026, 6, 15)).call.status
assert_equal "ORANGE", Airworthiness.new(@aircraft, today: Date.new(2026, 6, 30)).call.status
assert_equal "RED", Airworthiness.new(@aircraft, today: Date.new(2026, 7, 1)).call.status
end
test "hour-based inspection uses current hours minus offset" do
type = InspectionType.create!(name: "100 hr", recurring: true, recurring_hours: 100)
type.aircraft << @aircraft
@aircraft.inspection_records.create!(inspection_type: type, date: Date.current, aircraft_hours: 950)
assert_equal "GREEN", Airworthiness.new(@aircraft).call.status # 50 hrs since
@aircraft.update!(tach_time: 1045)
assert_equal "ORANGE", Airworthiness.new(@aircraft).call.status # 95 since, within 10
@aircraft.update!(tach_time: 1050)
assert_equal "RED", Airworthiness.new(@aircraft).call.status # 100 since
@aircraft.update!(tach_time_offset: 10)
assert_equal "ORANGE", Airworthiness.new(@aircraft).call.status # offset → 90 since
end
test "AD without compliance is red; hour-based due soon is orange; permanent is green" do
ad = AirworthinessDirective.create!(name: "2020-01-01", short_description: "Check thing")
ad.aircraft << @aircraft
assert_equal "RED", Airworthiness.new(@aircraft).call.status
c = ad.ad_compliances.create!(aircraft: @aircraft, date_complied: Date.current, compliance_notes: "Done",
next_due_at_time: 1005)
assert_equal "ORANGE", Airworthiness.new(@aircraft).call.status
c.update!(permanent: true)
assert_equal "GREEN", Airworthiness.new(@aircraft).call.status
end
test "conditional and non-mandatory ADs never affect status" do
AirworthinessDirective.create!(name: "C", short_description: "x", compliance_type: "conditional").aircraft << @aircraft
AirworthinessDirective.create!(name: "S", short_description: "x", bulletin_type: "sb", mandatory: false).aircraft << @aircraft
assert_equal "GREEN", Airworthiness.new(@aircraft).call.status
end
test "AD applied via component counts for the aircraft" do
engine = create_component(@aircraft)
AirworthinessDirective.create!(name: "ENG", short_description: "x").components << engine
assert_equal "RED", Airworthiness.new(@aircraft).call.status
end
test "replacement-critical component overdue by hours or days" do
oil = create_component(@aircraft, component_type: component_type("Oil"), replacement_critical: true,
replacement_hours: 50, hours_since_overhaul: 45, overhaul_date: Date.current)
assert_equal "ORANGE", Airworthiness.new(@aircraft).call.status
oil.update!(hours_since_overhaul: 50)
assert_equal "RED", Airworthiness.new(@aircraft).call.status
oil.update!(hours_since_overhaul: 0, replacement_days: 120, overhaul_date: 121.days.ago.to_date)
assert_equal "RED", Airworthiness.new(@aircraft).call.status
end
test "spare components are ignored" do
create_component(@aircraft, status: "SPARE", replacement_critical: true, replacement_hours: 10, hours_since_overhaul: 50)
assert_equal "GREEN", Airworthiness.new(@aircraft).call.status
end
end
+33
View File
@@ -0,0 +1,33 @@
require "test_helper"
class ConsumptionStatsTest < ActiveSupport::TestCase
test "oil hrs/qt average excludes IQR outliers and excluded records" do
a = create_aircraft
a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: 100)
hours = 100.0
# interval values (hrs/qt): 10, 11, 9, 10, 1 (outlier), 12, 10 (manually excluded)
[ [ 10, false ], [ 11, false ], [ 9, false ], [ 10, false ], [ 1, false ], [ 12, false ], [ 10, true ] ].each do |dh, excl|
hours += dh
a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: hours,
excluded_from_averages: excl)
end
stats = ConsumptionStats.new(a, "oil")
assert_equal 7, stats.points.size
assert_equal 10.4, stats.average # (10+11+9+10+12)/5
end
test "zero spread disables outlier filtering (matches original)" do
a = create_aircraft
[ 100, 110, 120, 130, 131, 141 ].each do |h|
a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: h)
end
assert_equal 8.2, ConsumptionStats.new(a, "oil").average
end
test "fuel gal/hr" do
a = create_aircraft
a.consumable_records.create!(record_type: "fuel", date: Date.current, quantity_added: 40, flight_hours: 100)
a.consumable_records.create!(record_type: "fuel", date: Date.current, quantity_added: 17, flight_hours: 102)
assert_equal 8.5, ConsumptionStats.new(a, "fuel").average
end
end
+58
View File
@@ -0,0 +1,58 @@
require "test_helper"
class HoursServiceTest < ActiveSupport::TestCase
setup do
@user = create_user
@aircraft = create_aircraft(owner: @user, tach_time: 1000, hobbs_time: 1200)
@engine = create_component(@aircraft, hours_in_service: 500, hours_since_overhaul: 300)
@spare = create_component(@aircraft, status: "SPARE", hours_in_service: 10, hours_since_overhaul: 10)
end
test "hours update cascades delta to in-use components only and logs event" do
HoursService.new(@aircraft, user: @user).update_hours!(tach_time: "1012.5", hobbs_time: "1214")
assert_equal 1012.5, @aircraft.reload.tach_time
assert_equal 1214, @aircraft.hobbs_time
assert_equal 512.5, @engine.reload.hours_in_service
assert_equal 312.5, @engine.hours_since_overhaul
assert_equal 10, @spare.reload.hours_in_service
assert_equal "hours", @aircraft.events.first.category
end
test "downward correction clamps component hours at zero" do
@engine.update!(hours_since_overhaul: 2)
HoursService.new(@aircraft, user: @user).update_hours!(tach_time: "995")
assert_equal 0, @engine.reload.hours_since_overhaul
assert_equal 495, @engine.hours_in_service
end
test "flight log increments hours and creates consumable records" do
flight = @aircraft.flight_logs.new(date: Date.current, tach_out: 1000, tach_in: 1001.8, hobbs_time: 2.1,
oil_added: 1, fuel_added: 20, departure_location: "KPVD", destination_location: "KBID")
HoursService.new(@aircraft, user: @user).log_flight!(flight)
assert_equal 1.8, flight.tach_time
assert_equal 1001.8, @aircraft.reload.tach_time
assert_equal 1202.1, @aircraft.hobbs_time
assert_equal 501.8, @engine.reload.hours_in_service
assert_equal %w[fuel oil], @aircraft.consumable_records.pluck(:record_type).sort
assert_equal 1001.8, @aircraft.consumable_records.first.flight_hours
assert_includes @aircraft.events.pluck(:event_name), "Flight logged: 1.8 hrs (KPVD → KBID)"
end
test "enforcement blocks flights on red aircraft only when enabled" do
@aircraft.squawks.create!(priority: 0, issue_reported: "Engine rough")
HoursService.new(@aircraft).update_hours!(tach_time: 1001) # allowed: enforcement off
@aircraft.update!(airworthiness_enforcement: true)
assert_raises(AircraftGroundedError) { HoursService.new(@aircraft).update_hours!(tach_time: 1002) }
assert_equal 1001, @aircraft.reload.tach_time
end
test "component service reset" do
@engine.reset_service!
assert_equal 0, @engine.hours_since_overhaul
assert_equal 500, @engine.hours_in_service
@engine.reset_service!(reset_in_service: true)
assert_equal 0, @engine.hours_in_service
assert_equal Date.current, @engine.date_in_service
end
end
View File
Whitespace-only changes.
+36
View File
@@ -0,0 +1,36 @@
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
module ActiveSupport
class TestCase
parallelize(workers: :number_of_processors)
def create_user(email = "user#{SecureRandom.hex(3)}@example.com", admin: false)
User.create!(email_address: email, password: "password123", name: email.split("@").first, admin: admin)
end
def create_aircraft(owner: nil, pilot: nil, **attrs)
Aircraft.create!({ tail_number: "N#{rand(10000..99999)}", tach_time: 1000 }.merge(attrs)).tap do |a|
a.roles.create!(user: owner, role: "owner") if owner
a.roles.create!(user: pilot, role: "pilot") if pilot
end
end
def component_type(name = "Engine")
ComponentType.find_or_create_by!(name: name)
end
def create_component(aircraft, **attrs)
aircraft.components.create!({ component_type: component_type, manufacturer: "Lycoming", model: "O-320",
status: "IN-USE", date_in_service: 1.year.ago.to_date }.merge(attrs))
end
end
end
class ActionDispatch::IntegrationTest
def sign_in(user)
post session_path, params: { email_address: user.email_address, password: "password123" }
follow_redirect!
end
end