Calendar feed: due items (inspections, ADs, component limits, documents) as a private iCalendar link, hour-based dates projected from the past year's flying

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-11 21:00:30 -05:00
1 parent 87e30946fe
commit 98e943cf0a
14 files changed
+266 -7

No files matched your search

@@ -0,0 +1,23 @@
# The calendar feed: what's coming due on every aircraft the user can see,
# as all-day events (dates for hour-based items are projected from the past
# year's flying). Calendar apps fetch it with the secret link from Profile.
class CalendarFeedsController < ApplicationController
allow_unauthenticated_access
skip_before_action :redirect_to_setup
rate_limit to: 60, within: 1.hour, with: -> { head :too_many_requests }
def show
user = User.active.find_by(calendar_token: params[:token].to_s) or return head(:not_found)
cal = Ical.new("Aircraft maintenance")
host = Sso.app_url
user.accessible_aircraft.ordered.each do |aircraft|
DueList.new(aircraft).items.each do |i|
when_text = [ ("projected from recent flying" if i.projected), ("at #{i.due_hours} hrs" if i.due_hours) ].compact.join(", ")
cal.add(uid: "#{aircraft.id}-#{i.key}@simple-aircraft-manager", date: i.date,
summary: "#{aircraft.tail_number}: #{i.title}#{' (overdue)' if i.overdue?}",
description: [ "#{aircraft.tail_number}: #{i.title}", when_text.presence ].compact.join("\n"), url: "#{host}#{i.path}")
end
end
send_data cal.to_s, type: "text/calendar; charset=utf-8", disposition: "inline", filename: "aircraft-maintenance.ics"
end
end
@@ -0,0 +1,7 @@
# Profile: create (or replace) the secret calendar feed link.
class CalendarTokensController < ApplicationController
def create
current_user.regenerate_calendar_token
redirect_to edit_profile_path(anchor: "calendar"), notice: "Calendar link ready. Any earlier link no longer works."
end
end
@@ -3,7 +3,7 @@ class RecordVersionsController < ApplicationController
include AircraftScoped
before_action :require_owner!, only: :restore
SHARED = %w[AirworthinessDirective InspectionType].freeze
SHARED = { "AirworthinessDirective" => AirworthinessDirective, "InspectionType" => InspectionType }.freeze
def index
@type = params[:type].presence_in(RecordVersion.where(aircraft_id: @aircraft.id).distinct.pluck(:item_type))
@@ -16,8 +16,8 @@ class RecordVersionsController < ApplicationController
# Puts back an AD or inspection type link that was removed (the removal
# stays in the history, and the restore is recorded as a new link).
def restore
version = RecordVersion.where(aircraft_id: @aircraft.id, event: "unlink", item_type: SHARED).find(params[:id])
record = version.item_type.constantize.find_by(id: version.item_id)
version = RecordVersion.where(aircraft_id: @aircraft.id, event: "unlink", item_type: SHARED.keys).find(params[:id])
record = SHARED.fetch(version.item_type).find_by(id: version.item_id)
return redirect_to(aircraft_record_versions_path(@aircraft), alert: "That record no longer exists.") unless record
c = version.changes_hash
if c["applies_to"] == "component"
@@ -33,8 +33,8 @@ class RecordVersionsController < ApplicationController
# Whether an unlink can still be undone (the record exists and isn't linked that way now).
def self.restorable?(version, aircraft)
return false unless version.event == "unlink" && SHARED.include?(version.item_type)
record = version.item_type.constantize.find_by(id: version.item_id) or return false
return false unless version.event == "unlink" && SHARED.key?(version.item_type)
record = SHARED[version.item_type].find_by(id: version.item_id) or return false
c = version.changes_hash
if c["applies_to"] == "component"
record.component_ids.exclude?(c["component_id"]) && aircraft.components.exists?(id: c["component_id"])