mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
- Setup checklist on a new aircraft's Overview (owners): components, standard inspections, DRS AD import, registration/airworthiness certificate, weight and balance; hideable per device - Log Flight: "Save and log next leg" reopens the form on the same date, starting where the leg ended - Component form: limits in two collapsible groups (replacement and expiry; overhaul and TBO), open when in use - Squawk form warns what "Ground Aircraft" does - Logbook entry form: inspection result only for inspection entries (show-when controller); document names default from the file - Dates in pickers, the activity log, airworthiness lines, search, audits and transcription notes use the app format - No duplicate Report Squawk button on Squawks; "Record service" label Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
137 lines
5.8 KiB
Ruby
137 lines
5.8 KiB
Ruby
class DocumentsController < ApplicationController
|
|
include AircraftScoped
|
|
before_action :require_owner!, 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
|