Squawks: multiple attachments, in-app photo viewer, downloads

Existing single attachments migrate to the new list. Photos open in a
full-screen viewer (swipe/arrow keys); every file has a download link.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-06 09:51:04 -05:00
1 parent 21ef577653
commit ef542ee2a5
12 files changed
+267 -13

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ class AircraftController < ApplicationController
def show
@airworthiness = @aircraft.airworthiness
@active_squawks = @aircraft.squawks.active.by_priority.includes(:component, :reported_by)
@active_squawks = @aircraft.squawks.active.by_priority.includes(:component, :reported_by).with_attached_attachments
@recent_events = @aircraft.events.includes(:user).limit(8)
@notes = @aircraft.notes.recent_first.includes(:user).limit(5)
@oil = ConsumptionStats.new(@aircraft, "oil")
+1 -1
View File
@@ -22,7 +22,7 @@ class ComponentsController < ApplicationController
end
def show
@squawks = @component.squawks.by_priority
@squawks = @component.squawks.by_priority.with_attached_attachments
end
def new
+16 -4
View File
@@ -5,8 +5,8 @@ class SquawksController < ApplicationController
before_action :set_squawk, only: %i[edit update destroy resolve reopen]
def index
@active = @aircraft.squawks.active.by_priority.includes(:component, :reported_by)
@resolved = @aircraft.squawks.resolved.order(resolved_at: :desc).includes(:component).limit(100)
@active = @aircraft.squawks.active.by_priority.includes(:component, :reported_by).with_attached_attachments
@resolved = @aircraft.squawks.resolved.order(resolved_at: :desc).includes(:component).with_attached_attachments.limit(100)
end
def new
@@ -26,7 +26,9 @@ class SquawksController < ApplicationController
def edit; end
def update
if @squawk.update(squawk_params)
@squawk.assign_attributes(squawk_params.except(:attachments))
change_attachments
if @squawk.save
log_event("squawk", "Squawk updated")
redirect_to aircraft_squawks_path(@aircraft), notice: "Squawk updated."
else
@@ -56,7 +58,17 @@ class SquawksController < ApplicationController
def set_squawk = @squawk = @aircraft.squawks.find(params[:id])
# Adds uploads and drops ticked files in one change, so nothing is saved
# (or purged) unless the whole update validates.
def change_attachments
added = Array(squawk_params[:attachments]).compact_blank
removed = Array(params[:remove_attachment_ids]).map(&:to_s)
return if added.empty? && removed.empty?
kept = @squawk.attachments.reject { removed.include?(_1.id.to_s) }.map(&:blob)
@squawk.attachments = kept + added
end
def squawk_params
params.require(:squawk).permit(:priority, :issue_reported, :component_id, :notes, :attachment)
params.require(:squawk).permit(:priority, :issue_reported, :component_id, :notes, attachments: [])
end
end
@@ -0,0 +1,69 @@
import { Controller } from "@hotwired/stimulus"
// In-app photo viewer for a set of image links. Click a photo to open it
// full-screen; ←/→, swipe or the arrow buttons move between photos, Esc or a
// click on the backdrop closes. Ctrl/⌘-click still opens the image in a tab.
export default class extends Controller {
static targets = [ "item", "dialog", "image", "caption", "counter", "download", "original", "nav" ]
connect() {
this.index = 0
}
disconnect() {
if (this.hasDialogTarget && this.dialogTarget.open) this.dialogTarget.close()
}
open(event) {
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.button > 0) return
event.preventDefault()
this.show(this.itemTargets.indexOf(event.currentTarget))
this.dialogTarget.showModal()
}
close() {
this.dialogTarget.close()
}
next() { this.show(this.index + 1) }
previous() { this.show(this.index - 1) }
keydown(event) {
if (event.key === "ArrowRight") { event.preventDefault(); this.next() }
if (event.key === "ArrowLeft") { event.preventDefault(); this.previous() }
}
// The backdrop is the dialog element itself; clicks on the photo or controls aren't.
backdrop(event) {
if (event.target === this.dialogTarget || event.target.dataset.galleryClose !== undefined) this.close()
}
touchstart(event) {
this.touchX = event.changedTouches[0].clientX
}
touchend(event) {
if (this.touchX == null) return
const dx = event.changedTouches[0].clientX - this.touchX
this.touchX = null
if (Math.abs(dx) > 50) dx < 0 ? this.next() : this.previous()
}
show(index) {
const items = this.itemTargets
this.index = (index + items.length) % items.length
const item = items[this.index]
const name = item.dataset.galleryName
this.imageTarget.src = item.href
this.imageTarget.alt = name
this.captionTarget.textContent = name
this.downloadTarget.href = item.dataset.galleryDownload
this.originalTarget.href = item.href
this.counterTarget.textContent = items.length > 1 ? `${this.index + 1} / ${items.length}` : ""
this.navTargets.forEach((el) => { el.hidden = items.length < 2 })
// Preload the neighbours so swiping feels instant.
if (items.length > 1) [ this.index + 1, this.index - 1 ].forEach((i) => { new Image().src = items[(i + items.length) % items.length].href })
}
}
+2 -2
View File
@@ -7,11 +7,11 @@ class Squawk < ApplicationRecord
belongs_to :aircraft
belongs_to :component, optional: true
belongs_to :reported_by, class_name: "User", optional: true
has_one_attached :attachment
has_many_attached :attachments
validates :priority, inclusion: { in: PRIORITIES.keys }
validates :issue_reported, presence: true
validates_uploads :attachment
validates_uploads :attachments
scope :active, -> { where(resolved: false) }
scope :resolved, -> { where(resolved: true) }
+55
View File
@@ -0,0 +1,55 @@
<%# A squawk's files: photos open in the in-app viewer, everything else opens in
a new tab. Every file has a download link. %>
<% files = squawk.attachments.to_a %>
<% if files.any? %>
<% photos, others = files.partition(&:image?) %>
<div class="mt-1" data-controller="gallery">
<ul class="flex flex-wrap gap-1.5">
<% photos.each do |file| %>
<li class="inline-flex items-center overflow-hidden rounded-md text-xs ring-1 ring-slate-300">
<%= link_to rails_blob_path(file, disposition: :inline), class: "px-2 py-1 text-sky-700 hover:bg-slate-100",
title: "Preview #{file.filename}",
data: { gallery_target: "item", action: "gallery#open", gallery_name: file.filename.to_s,
gallery_download: rails_blob_path(file, disposition: :attachment) } do %>
🖼 <%= file.filename.to_s.truncate(28, omission: "…") %>
<% end %>
<%= link_to "⬇", rails_blob_path(file, disposition: :attachment), download: file.filename.to_s,
class: "border-l border-slate-300 px-1.5 py-1 text-slate-500 hover:bg-slate-100 hover:text-sky-700",
title: "Download #{file.filename}", aria: { label: "Download #{file.filename}" } %>
</li>
<% end %>
<% others.each do |file| %>
<li class="inline-flex items-center overflow-hidden rounded-md text-xs ring-1 ring-slate-300">
<%= link_to "📎 #{file.filename.to_s.truncate(28, omission: "…")}", rails_blob_path(file, disposition: :inline),
class: "px-2 py-1 text-sky-700 hover:bg-slate-100", target: "_blank", rel: "noopener", title: "Open #{file.filename}" %>
<%= link_to "⬇", rails_blob_path(file, disposition: :attachment), download: file.filename.to_s,
class: "border-l border-slate-300 px-1.5 py-1 text-slate-500 hover:bg-slate-100 hover:text-sky-700",
title: "Download #{file.filename}", aria: { label: "Download #{file.filename}" } %>
</li>
<% end %>
</ul>
<% if photos.any? %>
<dialog data-gallery-target="dialog" aria-label="Photo viewer"
class="m-0 h-dvh max-h-none w-screen max-w-none bg-transparent p-0 text-white backdrop:bg-black/90"
data-action="click->gallery#backdrop keydown->gallery#keydown touchstart->gallery#touchstart touchend->gallery#touchend">
<div class="flex h-full flex-col" data-gallery-close>
<div class="flex items-center gap-3 bg-black/60 px-4 py-2 text-sm">
<span data-gallery-target="caption" class="min-w-0 flex-1 truncate font-medium"></span>
<span data-gallery-target="counter" class="text-white/60 tabular-nums"></span>
<a data-gallery-target="original" target="_blank" rel="noopener" class="rounded px-2 py-1 hover:bg-white/15">Open</a>
<a data-gallery-target="download" download class="rounded bg-white/15 px-3 py-1 font-medium hover:bg-white/25">⬇ Download</a>
<button type="button" data-action="gallery#close" class="rounded px-2 py-1 text-lg leading-none hover:bg-white/15" aria-label="Close" autofocus>✕</button>
</div>
<div class="relative flex min-h-0 flex-1 items-center justify-center p-2 sm:p-6" data-gallery-close>
<button type="button" data-gallery-target="nav" data-action="gallery#previous" aria-label="Previous photo"
class="absolute left-2 top-1/2 -translate-y-1/2 rounded-full bg-black/50 px-3 py-2 text-2xl hover:bg-black/70">‹</button>
<img data-gallery-target="image" alt="" class="max-h-full max-w-full select-none object-contain shadow-2xl">
<button type="button" data-gallery-target="nav" data-action="gallery#next" aria-label="Next photo"
class="absolute right-2 top-1/2 -translate-y-1/2 rounded-full bg-black/50 px-3 py-2 text-2xl hover:bg-black/70">›</button>
</div>
</div>
</dialog>
<% end %>
</div>
<% end %>
+11 -2
View File
@@ -1,9 +1,18 @@
<%= form_with model: [aircraft, squawk], class: "card card-body space-y-4 max-w-2xl" do |f| %>
<%= form_with model: [aircraft, squawk], multipart: true, class: "card card-body space-y-4 max-w-2xl" do |f| %>
<%= f.errors_summary %>
<%= f.field :priority, as: :select, collection: Squawk::PRIORITIES.invert, hint: "“Ground Aircraft” makes the aircraft not airworthy until resolved." %>
<%= f.field :issue_reported, as: :textarea, label: "Issue", required: true, autofocus: true %>
<%= f.field :component_id, as: :select, label: "Component (optional)", collection: component_options(aircraft) %>
<%= f.field :attachment, as: :file, label: "Photo / attachment" %>
<%= f.field :attachments, as: :file, multiple: true, label: squawk.attachments.attached? ? "Add photos / files" : "Photos / files",
hint: "Select several at once. Images, PDFs or text, up to 50 MB each." %>
<% if squawk.persisted? && squawk.attachments.attached? %>
<div>
<p class="label">Remove attached files</p>
<% squawk.attachments.each do |file| %>
<label class="flex items-center gap-2 text-sm"><%= check_box_tag "remove_attachment_ids[]", file.id, false, id: "remove_attachment_#{file.id}" %> <%= file.filename %></label>
<% end %>
</div>
<% end %>
<% if squawk.persisted? %><%= f.field :notes, as: :textarea, rows: 3 %><% end %>
<div class="flex gap-2">
<%= f.submit squawk.new_record? ? "Report Squawk" : "Save" %>
+1 -1
View File
@@ -7,7 +7,7 @@
<td><%= squawk_priority_badge(s) %></td>
<td class="max-w-md">
<%= s.issue_reported %>
<% if s.attachment.attached? %><div><%= link_to "📎 #{s.attachment.filename}", rails_blob_path(s.attachment, disposition: :inline), class: "link text-xs", target: "_blank" %></div><% end %>
<%= render "squawks/attachments", squawk: s %>
<% if s.notes.present? %><div class="mt-1 text-xs text-slate-500"><%= s.notes %></div><% end %>
</td>
<td><%= s.component&.to_s || "—" %></td>
+1 -1
View File
@@ -17,7 +17,7 @@
<% @resolved.each do |s| %>
<tr>
<td><%= squawk_priority_badge(s) %></td>
<td><%= s.issue_reported %></td>
<td><%= s.issue_reported %><%= render "squawks/attachments", squawk: s %></td>
<td class="text-xs text-slate-600"><%= s.notes %></td>
<td class="text-xs text-slate-500"><%= s.resolved_at&.to_date %></td>
<td class="text-right"><% if owner? %><%= button_to "Reopen", reopen_aircraft_squawk_path(@aircraft, s), method: :patch, class: "btn-secondary btn-sm" %><% end %></td>