Files
simple-aircraft-manager-rails/app/controllers/logbook_imports_controller.rb
T
rickyandClaude Opus 5.5 89ed0a76f9 Move all user-facing text into config/locales/en.yml for localization
- Views use lazy t(".key") lookups; sentences with values or links stay
  whole (interpolation, _html keys); counts use one/other plurals
- Stored-value labels (statuses, types, priorities...) are LabelSets read
  from labels: in the current locale; field names and validation messages
  under activerecord:; flash and activity text under flash:
- Services, models, helpers, mailer subjects, the logbook PDF, procedures
  templates, records package README and transcription warnings localized;
  the PDF reuses LogbookEntry#certification_statement instead of a copy
- Scripts read js: strings from a JSON tag via app/javascript/i18n.js
- Dates through I18n.l (app, weekday, month_year, long formats); html lang
  follows the locale
- APP_LOCALE picks the language (falls back to English); missing
  translations raise in development and tests
- README: Translating section and APP_LOCALE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:08:20 -05:00

56 lines
2.4 KiB
Ruby

# Upload scanned logbook pages (images or PDFs) as a document and, when an
# Anthropic API key is configured, have Claude transcribe them into logbook
# entries in the background (LogbookTranscriptionJob).
class LogbookImportsController < ApplicationController
include AircraftScoped
before_action :require_owner!
def new
@collections = @aircraft.document_collections.ordered
end
def create
files = Array(params[:pages]).compact_blank
return redirect_to(new_aircraft_logbook_import_path(@aircraft), alert: t("flash.logbook_imports.choose_the_scanned_pages")) if files.empty?
# Page order follows the file names ("page-002" before "page-010").
files = files.sort_by { |f| f.original_filename.to_s.downcase.scan(/\d+|\D+/).map { _1.match?(/\d/) ? [ 0, _1.to_i ] : [ 1, _1 ] } }
collection = find_or_build_collection
document = @aircraft.documents.new(name: params[:document_name].presence || collection.name, doc_type: doc_type,
document_collection: collection)
document.pages.attach(files)
unless collection.valid? && document.valid?
@collections = @aircraft.document_collections.ordered
flash.now[:alert] = (collection.errors.full_messages + document.errors.full_messages).to_sentence
return render :new, status: :unprocessable_entity
end
import = nil
ActiveRecord::Base.transaction do
collection.save!
document.save!
import = @aircraft.logbook_imports.create!(user: current_user, document: document, log_type: params[:log_type],
transcribe: params[:transcribe] == "1" && LogbookImport.ai_available?)
end
log_event("document", t("flash.logbook_imports.pages_uploaded", name: document.name, count: files.size))
LogbookTranscriptionJob.perform_later(import.id)
redirect_to aircraft_logbook_import_path(@aircraft, import)
end
def show
@import = @aircraft.logbook_imports.find(params[:id])
end
private
def find_or_build_collection
if params[:collection_id].present? && params[:collection_id] != "new"
@aircraft.document_collections.find(params[:collection_id])
else
@aircraft.document_collections.new(name: params[:collection_name].presence || "Logbooks", visibility: "maintenance")
end
end
def doc_type = Document::TYPES.key?(params[:doc_type]) ? params[:doc_type] : "LOG"
end