mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Search across the logbook, squawks, notes, ADs, documents, components, flights and major records (top bar)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c8e3b7374
commit
2953b9a18d
7 files changed
+189
No files matched your search
@@ -0,0 +1,12 @@
|
||||
# One search box across the records of the aircraft the user can see
|
||||
# (or one aircraft, from its own pages).
|
||||
class SearchesController < ApplicationController
|
||||
rate_limit to: 60, within: 1.minute, with: -> { redirect_to root_path, alert: "Too many searches; wait a moment." }
|
||||
|
||||
def show
|
||||
fleet = current_user.accessible_aircraft.ordered.to_a
|
||||
@aircraft = fleet.find { _1.id == params[:aircraft_id].to_i } if params[:aircraft_id].present?
|
||||
@fleet = fleet
|
||||
@search = SiteSearch.new(params[:q], @aircraft || fleet)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,91 @@
|
||||
# One search across an aircraft's records: logbook, squawks, notes, ADs,
|
||||
# documents (names, descriptions and the text read from their pages),
|
||||
# components, flights and major repairs/alterations.
|
||||
class SiteSearch
|
||||
Result = Data.define(:kind, :title, :snippet, :aircraft, :path)
|
||||
LIMIT = 25
|
||||
KINDS = { "logbook" => "Logbook", "squawk" => "Squawks", "note" => "Notes", "ad" => "ADs", "document" => "Documents",
|
||||
"component" => "Components", "flight" => "Flights", "major" => "Major repairs & alterations" }.freeze
|
||||
|
||||
attr_reader :query
|
||||
|
||||
def initialize(query, aircraft)
|
||||
@query = query.to_s.strip.first(100)
|
||||
@aircraft = Array(aircraft)
|
||||
@ids = @aircraft.map(&:id)
|
||||
@by_id = @aircraft.index_by(&:id)
|
||||
@urls = Rails.application.routes.url_helpers
|
||||
end
|
||||
|
||||
def searchable? = @query.length >= 2
|
||||
|
||||
# { kind => [Result] }, kinds with no matches left out.
|
||||
def results
|
||||
return {} unless searchable?
|
||||
@results ||= {
|
||||
"logbook" => find(LogbookEntry.where(aircraft_id: @ids).order(date: :desc), %w[text signoff_person signoff_location]) { |e, s|
|
||||
[ "#{e.date} · #{LogbookEntry::LOG_TYPES[e.log_type]} log", s, @urls.aircraft_logbook_entry_path(e.aircraft_id, e) ] },
|
||||
"squawk" => find(Squawk.where(aircraft_id: @ids).order(created_at: :desc), %w[issue_reported notes deferral_notes]) { |q, s|
|
||||
[ "#{q.issue_reported.truncate(80)}#{' (resolved)' if q.resolved?}", s, @urls.aircraft_squawks_path(q.aircraft_id) ] },
|
||||
"note" => find(AircraftNote.where(aircraft_id: @ids).order(created_at: :desc), %w[text]) { |n, s|
|
||||
[ "Note, #{n.created_at.to_date}", s, @urls.aircraft_notes_path(n.aircraft_id) ] },
|
||||
"ad" => ads,
|
||||
"document" => find(Document.where(aircraft_id: @ids).order(:name), %w[name description pages_text]) { |d, s|
|
||||
[ d.name, s, @urls.aircraft_document_path(d.aircraft_id, d) ] },
|
||||
"component" => components,
|
||||
"flight" => find(FlightLog.where(aircraft_id: @ids).order(date: :desc), %w[departure_location destination_location route notes]) { |f, s|
|
||||
[ "#{f.date} · #{f.route_summary.presence || 'Flight'}", s, @urls.aircraft_flight_log_path(f.aircraft_id, f) ] },
|
||||
"major" => find(MajorRecord.where(aircraft_id: @ids).order(date_performed: :desc), %w[title description stc_number stc_holder notes]) { |m, s|
|
||||
[ m.title.to_s, s, @urls.aircraft_major_record_path(m.aircraft_id, m) ] }
|
||||
}.reject { _2.empty? }
|
||||
end
|
||||
|
||||
def count = results.values.sum(&:size)
|
||||
|
||||
private
|
||||
|
||||
def term = "%#{ActiveRecord::Base.sanitize_sql_like(@query)}%"
|
||||
|
||||
def find(scope, columns)
|
||||
columns = columns.select { scope.klass.column_names.include?(_1) }
|
||||
sql = columns.map { "#{scope.klass.connection.quote_column_name(_1)} LIKE :t ESCAPE '\\'" }.join(" OR ")
|
||||
scope.where(sql, t: term).limit(LIMIT).map do |record|
|
||||
title, snippet, path = yield(record, snippet_for(record, columns))
|
||||
Result.new(kind: nil, title: title, snippet: snippet, aircraft: @by_id[record.aircraft_id], path: path)
|
||||
end
|
||||
end
|
||||
|
||||
# Components, by type name too ("magneto").
|
||||
def components
|
||||
cols = %w[manufacturer model serial_number install_location notes]
|
||||
sql = (cols.map { "components.#{_1}" } + [ "component_types.name" ]).map { "#{_1} LIKE :t ESCAPE '\\'" }.join(" OR ")
|
||||
Component.where(aircraft_id: @ids).left_joins(:component_type).includes(:component_type).where(sql, t: term).order(:id).limit(LIMIT).map do |c|
|
||||
Result.new(kind: nil, title: "#{c} · #{c.manufacturer} #{c.model}".strip, snippet: snippet_for(c, cols),
|
||||
aircraft: @by_id[c.aircraft_id], path: @urls.aircraft_component_path(c.aircraft_id, c))
|
||||
end
|
||||
end
|
||||
|
||||
# ADs are shared: those that apply to these aircraft (directly or through a component).
|
||||
def ads
|
||||
@aircraft.flat_map do |a|
|
||||
find_scope = AirworthinessDirective.applicable_to(a).order(:name)
|
||||
cols = %w[name short_description required_action docket_number]
|
||||
sql = cols.map { "airworthiness_directives.#{_1} LIKE :t ESCAPE '\\'" }.join(" OR ")
|
||||
find_scope.where(sql, t: term).limit(LIMIT).map do |ad|
|
||||
Result.new(kind: nil, title: "AD #{ad.name} · #{ad.short_description.to_s.truncate(70)}", snippet: snippet_for(ad, cols),
|
||||
aircraft: a, path: @urls.aircraft_airworthiness_directives_path(a))
|
||||
end
|
||||
end.first(LIMIT)
|
||||
end
|
||||
|
||||
# The text around the first match, from whichever field has it.
|
||||
def snippet_for(record, columns)
|
||||
columns.each do |col|
|
||||
text = record[col].to_s.squish
|
||||
at = text.downcase.index(@query.downcase) or next
|
||||
start = [ at - 60, 0 ].max
|
||||
return "#{'…' if start.positive?}#{text[start, @query.length + 140]}#{'…' if start + @query.length + 140 < text.length}"
|
||||
end
|
||||
nil
|
||||
end
|
||||
end
|
||||
@@ -36,6 +36,7 @@
|
||||
<div class="absolute right-0 z-30 mt-2 w-56 rounded-md bg-surface py-1 text-slate-700 shadow-lg ring-1 ring-slate-200">
|
||||
<p class="truncate px-4 pt-2 pb-1 text-xs text-slate-500"><%= current_user.email_address %></p>
|
||||
<%= link_to "Profile", edit_profile_path, class: "block px-4 py-2.5 hover:bg-slate-100" %>
|
||||
<%= link_to "Search", search_path, class: "block px-4 py-2.5 hover:bg-slate-100" %>
|
||||
<%= link_to "Help", help_path, class: "block px-4 py-2.5 hover:bg-slate-100" %>
|
||||
<% if current_user.admin? %>
|
||||
<p class="border-t border-slate-100 px-4 pt-2 pb-1 text-xs font-semibold text-slate-500">Admin</p>
|
||||
@@ -48,6 +49,11 @@
|
||||
</details>
|
||||
</nav>
|
||||
<nav class="hidden items-center gap-4 text-sm sm:flex" aria-label="Main">
|
||||
<%= form_with url: search_path, method: :get, class: "hidden md:block", html: { role: "search" } do %>
|
||||
<%= search_field_tag :q, params[:q], placeholder: "Search…", "aria-label": "Search the records",
|
||||
class: "w-40 rounded bg-white/10 px-2 py-1 text-sm text-white placeholder:text-white/60 focus:w-56 focus:bg-white/20 focus:outline-none transition-all lg:w-48" %>
|
||||
<% end %>
|
||||
<%= link_to "Search", search_path, class: "hover:text-sky-300 md:hidden" %>
|
||||
<%= link_to "Fleet", root_path, class: "hover:text-sky-300" %>
|
||||
<% if current_user.admin? %>
|
||||
<details class="relative" data-controller="dropdown" data-action="click@window->dropdown#closeOutside keydown.esc@window->dropdown#close">
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<% content_for :title, @search.query.present? ? "Search: #{@search.query}" : "Search" %>
|
||||
<h1 class="mb-4 text-2xl font-bold">Search</h1>
|
||||
<%= form_with url: search_path, method: :get, class: "card card-body mb-5 flex flex-wrap items-end gap-2", data: { turbo_action: "advance" } do %>
|
||||
<div class="min-w-0 flex-1">
|
||||
<%= label_tag :q, "Search for", class: "label" %>
|
||||
<%= search_field_tag :q, @search.query, class: "input", autofocus: @search.query.blank?, placeholder: "e.g. magneto, KOQU, 2011-10-09, oil filter" %>
|
||||
</div>
|
||||
<% if @fleet.size > 1 %>
|
||||
<div>
|
||||
<%= label_tag :aircraft_id, "Aircraft", class: "label" %>
|
||||
<%= select_tag :aircraft_id, options_for_select([ [ "All aircraft", "" ] ] + @fleet.map { [ _1.tail_number, _1.id ] }, @aircraft&.id), class: "input" %>
|
||||
</div>
|
||||
<% end %>
|
||||
<%= submit_tag "Search", class: "btn-primary", name: nil %>
|
||||
<% end %>
|
||||
|
||||
<% if @search.query.present? && !@search.searchable? %>
|
||||
<p class="text-sm text-slate-600">Type at least two characters.</p>
|
||||
<% elsif @search.searchable? %>
|
||||
<p class="mb-3 text-sm text-slate-600" role="status"><%= pluralize(@search.count, "result") %> for “<%= @search.query %>”<%= " on #{@aircraft.tail_number}" if @aircraft %>.</p>
|
||||
<div class="space-y-5">
|
||||
<% @search.results.each do |kind, results| %>
|
||||
<section class="card">
|
||||
<div class="card-header"><h2 class="card-title"><%= SiteSearch::KINDS[kind] %> <span class="text-sm font-normal text-slate-500">(<%= results.size %><%= "+" if results.size >= SiteSearch::LIMIT %>)</span></h2></div>
|
||||
<ul class="divide-y divide-slate-100">
|
||||
<% results.each do |r| %>
|
||||
<li class="px-4 py-2">
|
||||
<%= link_to r.title, r.path, class: "link font-medium" %>
|
||||
<% if @fleet.size > 1 && !@aircraft %><span class="badge-gray ml-1"><%= r.aircraft&.tail_number %></span><% end %>
|
||||
<% if r.snippet %><p class="mt-0.5 text-sm text-slate-600"><%= highlight(r.snippet, @search.query, highlighter: '<mark class="rounded bg-amber-100 px-0.5 text-slate-900">\1</mark>') %></p><% end %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</section>
|
||||
<% end %>
|
||||
<% if @search.results.empty? %><div class="card"><%= empty_state "Nothing found. Try a shorter word, a part number or an airport code." %></div><% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -116,6 +116,7 @@ Rails.application.routes.draw do
|
||||
end
|
||||
|
||||
get "airports/search", to: "airports#search", as: :airport_search
|
||||
get "search", to: "searches#show", as: :search
|
||||
get "help", to: "help#show", as: :help
|
||||
get "help/records", to: "help#show", defaults: { page: "records" }, as: :records_help
|
||||
get "help/:page", to: "help#show", as: :help_page, constraints: { page: /[a-z-]+/ }
|
||||
|
||||
@@ -36,6 +36,7 @@ Anyone can add an aircraft and becomes its owner. You only see aircraft you have
|
||||
- **Fleet** (top left) lists your aircraft with their airworthiness.
|
||||
- Each aircraft has tabs: **Overview, Components, Squawks, Flights, Oil & Fuel, Oil Analysis, Logbook, Inspections, ADs, Documents, Major Records, W&B, Notes, Activity**, and for owners **Access & Sharing**.
|
||||
- **Log Flight** and **Report Squawk** are at the top of every aircraft page.
|
||||
- **Search** (the box in the top bar, or ☰ → Search on a phone) looks through the logbook, squawks, notes, ADs, documents (including the text of their pages), components, flights and major records of your aircraft at once.
|
||||
- Your name (or ☰ on a phone) leads to your **Profile**, this **Help**, and for admins the **Admin** pages.
|
||||
|
||||
## On a phone
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
require "test_helper"
|
||||
|
||||
# One search box across the records of the aircraft you can see.
|
||||
class SiteSearchTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@owner = create_user("owner@example.com")
|
||||
@a = create_aircraft(owner: @owner, tail_number: "N12345")
|
||||
@other = create_aircraft(tail_number: "N99999") # not the owner's
|
||||
@mag = create_component(@a, component_type: component_type("Magneto"), manufacturer: "Bendix", model: "S4LN-21", serial_number: "10000000")
|
||||
@a.logbook_entries.create!(date: Date.new(2026, 5, 1), entry_type: "MAINTENANCE", text: "Inspected left magneto timing, set to 22° BTC.", aircraft_hours_at_entry: 1000)
|
||||
@a.squawks.create!(issue_reported: "Magneto drop 150 rpm on left", priority: 1)
|
||||
@a.documents.create!(name: "Magneto 500-hour inspection invoice", doc_type: "INVOICE")
|
||||
ad = AirworthinessDirective.create!(name: "2005-12-06", short_description: "Bendix magnetos impulse coupling")
|
||||
ad.components << @mag
|
||||
@other.squawks.create!(issue_reported: "Magneto on someone else's plane", priority: 1)
|
||||
sign_in @owner
|
||||
end
|
||||
|
||||
test "finds matches in every kind of record, with the text around them, only on your aircraft" do
|
||||
get search_path(q: "magneto")
|
||||
assert_response :success
|
||||
%w[Logbook Squawks ADs Documents Components].each { |kind| assert_select "h2", text: /#{kind}/ }
|
||||
assert_select "mark", text: /magneto/i
|
||||
assert_includes response.body, "Inspected left <mark"
|
||||
refute_includes response.body, "someone else" # another owner's aircraft
|
||||
assert_select "a[href='#{aircraft_component_path(@a, @mag)}']"
|
||||
|
||||
get search_path(q: "10000000") # serial number
|
||||
assert_select "h2", text: /Components/
|
||||
get search_path(q: "x")
|
||||
assert_includes response.body, "at least two characters"
|
||||
get search_path(q: "100%") # LIKE wildcards are literal
|
||||
assert_includes response.body, "0 results"
|
||||
end
|
||||
|
||||
test "the top bar has the search box" do
|
||||
get root_path
|
||||
assert_select "form[action='#{search_path}'][role=search] input[name=q]"
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user