Files
simple-aircraft-manager-rails/app/controllers/documents_controller.rb
T
rickyandClaude Opus 5.5 d8cd462194 Configurable roles: Owner, Mechanic, Pilot, Viewer and custom roles
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>
2026-11-15 17:43:38 -05:00

137 lines
5.8 KiB
Ruby

class DocumentsController < ApplicationController
include AircraftScoped
requires_permission :manage_documents, except: %i[index show]
before_action :set_document, only: %i[show edit update destroy]
def index
@collections = @aircraft.document_collections.ordered.includes(documents: { pages_attachments: :blob })
@uncollected = @aircraft.documents.where(document_collection_id: nil).ordered.includes(pages_attachments: :blob)
end
def show
@page = (params[:page] || 1).to_i.clamp(1, [ @document.pages.size, 1 ].max)
end
def new
@document = @aircraft.documents.new(document_collection_id: params[:collection_id])
@document.doc_type = params[:doc_type] if Document::TYPES.key?(params[:doc_type])
end
def create
@document = @aircraft.documents.new(document_params)
if @document.save
log_event("document", t("flash.documents.document_added_2", name: @document.name))
redirect_to aircraft_document_path(@aircraft, @document), notice: t("flash.documents.document_added")
else
render :new, status: :unprocessable_entity
end
end
def bulk_new
@bulk = bulk_defaults.merge(document_collection_id: params[:collection_id])
end
# Each file becomes a document named after it; files given the same name are
# combined, in the order sent, as the pages of one document. Files arrive as
# direct-upload signed ids (or as plain uploads when JavaScript is off).
def bulk_create
@bulk = bulk_defaults.merge(params.slice(:doc_type, :visibility, :document_collection_id, :new_collection).permit!.to_h.symbolize_keys)
files = Array(params[:files]).compact_blank
return bulk_error(t("flash.documents.choose_files")) if files.empty?
return bulk_error(t("flash.documents.too_many_files", max: MAX_BULK_FILES)) if files.size > MAX_BULK_FILES
names = Array(params[:names])
rejected = []
groups = {}
files.each_with_index do |file, i|
upload = file.is_a?(String) ? unattached_blob(file) : file
next rejected << t("flash.documents.upload_expired", number: i + 1) unless upload
name = names[i].to_s.squish.presence || File.basename(upload_filename(upload), ".*").tr("_", " ").squish.presence || "Untitled"
(groups[name.first(255)] ||= []) << upload
end
collection_id, error = bulk_collection
return bulk_error(error) if error
created = []
groups.each do |name, pages|
doc = @aircraft.documents.new(name: name, doc_type: @bulk[:doc_type], visibility: @bulk[:visibility].presence,
document_collection_id: collection_id, pages: pages)
if doc.save
created << doc
else
rejected << t("flash.documents.rejected_file", name: name, errors: doc.errors.full_messages.to_sentence)
pages.each { _1.purge_later if _1.is_a?(ActiveStorage::Blob) }
end
end
if created.empty?
@aircraft.document_collections.find_by(id: collection_id)&.destroy if @bulk[:new_collection].present?
return bulk_error(t("flash.documents.nothing_uploaded", items: rejected.to_sentence))
end
log_event("document", t("flash.documents.bulk_uploaded_event", count: created.size), notes: created.map(&:name).join(", ").truncate(1000))
flash[:alert] = t("flash.documents.not_uploaded", items: rejected.to_sentence) if rejected.any?
redirect_to aircraft_documents_path(@aircraft), notice: t("flash.documents.bulk_uploaded", count: created.size), status: :see_other
end
def edit; end
def update
attrs = document_params
new_pages = attrs.delete(:pages)
@document.assign_attributes(attrs)
@document.pages.attach(new_pages) if new_pages.present?
Array(params[:remove_page_ids]).each { |id| @document.pages.find_by(id: id)&.purge_later }
if @document.save
log_event("document", t("flash.documents.document_updated_2", name: @document.name))
redirect_to aircraft_document_path(@aircraft, @document), notice: t("flash.documents.document_updated")
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@document.destroy!
log_event("document", t("flash.documents.document_deleted_2", name: @document.name))
redirect_to aircraft_documents_path(@aircraft), notice: t("flash.documents.document_deleted")
end
private
MAX_BULK_FILES = 500
def bulk_defaults = { doc_type: "OTHER", visibility: nil, document_collection_id: nil, new_collection: nil }
def bulk_error(message)
@bulk_error = message
render :bulk_new, status: :unprocessable_entity
end
# A blob from a direct upload that hasn't been attached anywhere yet, so a
# signed id lifted from an existing file's URL can't be re-attached here.
def unattached_blob(signed_id)
blob = ActiveStorage::Blob.find_signed(signed_id)
blob if blob && !blob.attachments.exists?
end
def upload_filename(upload)
(upload.respond_to?(:original_filename) ? upload.original_filename : upload.filename).to_s
end
# [collection id or nil, error]: the chosen collection, or a new one.
def bulk_collection
return [ scoped_id(@aircraft.document_collections, @bulk[:document_collection_id]), nil ] if @bulk[:new_collection].blank?
col = @aircraft.document_collections.create(name: @bulk[:new_collection].squish, visibility: @bulk[:visibility].presence || "private")
col.persisted? ? [ col.id, nil ] : [ nil, t("flash.documents.collection_failed", errors: col.errors.full_messages.to_sentence) ]
end
def set_document = @document = @aircraft.documents.find(params[:id])
def document_params
p = params.require(:document).permit(:name, :description, :doc_type, :visibility, :document_collection_id, :expires_on, :legacy_record, pages: [])
p[:pages]&.compact_blank!
p.delete(:pages) if p[:pages].blank?
p[:document_collection_id] = scoped_id(@aircraft.document_collections, p[:document_collection_id])
p
end
end