From 1b6b82a6ccc601c0b25978f0d7a304deb1ee413f Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Tue, 30 Sep 2025 13:44:51 +0200 Subject: [PATCH] Redirect to cards when searching by card id --- app/assets/stylesheets/bar.css | 4 +++ app/controllers/searches_controller.rb | 15 ++++++++--- .../controllers/auto_submit_controller.js | 1 + app/javascript/controllers/bar_controller.js | 1 + app/views/searches/_results.html.erb | 20 +++++++++++++++ app/views/searches/show.html.erb | 25 ++++--------------- test/controllers/searches_controller_test.rb | 13 ++++++++++ 7 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 app/views/searches/_results.html.erb diff --git a/app/assets/stylesheets/bar.css b/app/assets/stylesheets/bar.css index be5eb6a2e..dc5feaef8 100644 --- a/app/assets/stylesheets/bar.css +++ b/app/assets/stylesheets/bar.css @@ -43,6 +43,10 @@ margin-block-end: calc(var(--footer-height) - 0.3rem); position: fixed; z-index: -1; + + &:has(#bar-content[busy]), &:has(#bar-content:not([complete])), &:has([data-search-redirect]) { + display: none; + } } .bar__placeholder { diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb index 1ccbaa245..b1418174d 100644 --- a/app/controllers/searches_controller.rb +++ b/app/controllers/searches_controller.rb @@ -1,8 +1,17 @@ class SearchesController < ApplicationController - include Search::QueryTermsScoped + include Search::QueryTermsScoped, Turbo::DriveHelper def show - @search_results = Current.user.search(@query_terms).limit(50) - @recent_search_queries = Current.user.search_queries.order(updated_at: :desc).limit(10) + if card = Current.user.accessible_cards.find_by_id(@query_terms) + @card = card + else + perform_search + end end + + private + def perform_search + @search_results = Current.user.search(@query_terms).limit(50) + @recent_search_queries = Current.user.search_queries.order(updated_at: :desc).limit(10) + end end diff --git a/app/javascript/controllers/auto_submit_controller.js b/app/javascript/controllers/auto_submit_controller.js index 2fa3a4ae4..70cf1ee6a 100644 --- a/app/javascript/controllers/auto_submit_controller.js +++ b/app/javascript/controllers/auto_submit_controller.js @@ -2,6 +2,7 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { connect() { + this.element.addEventListener("turbo:submit-end", () => this.element.remove(), { once: true } ) this.element.requestSubmit() } } diff --git a/app/javascript/controllers/bar_controller.js b/app/javascript/controllers/bar_controller.js index 31fc11e3f..4e2fe8c55 100644 --- a/app/javascript/controllers/bar_controller.js +++ b/app/javascript/controllers/bar_controller.js @@ -76,6 +76,7 @@ export default class extends Controller { const autofocusElement = element.querySelector("[autofocus]") autofocusElement?.focus() + autofocusElement?.select() } #hideItem(element) { diff --git a/app/views/searches/_results.html.erb b/app/views/searches/_results.html.erb new file mode 100644 index 000000000..830c7ccf1 --- /dev/null +++ b/app/views/searches/_results.html.erb @@ -0,0 +1,20 @@ +
+ +
+ + diff --git a/app/views/searches/show.html.erb b/app/views/searches/show.html.erb index 905afa257..ffa9b95fd 100644 --- a/app/views/searches/show.html.erb +++ b/app/views/searches/show.html.erb @@ -1,22 +1,7 @@ <%= turbo_frame_tag "bar-content" do %> -
- -
- - + <% if @card %> + <%= auto_submit_form_with url: card_path(@card), method: :get, data: { turbo_action: "advance", turbo_frame: "_top", search_redirect: true } %> + <% else %> + <%= render "results", search_results: @search_results %> + <% end %> <% end %> diff --git a/test/controllers/searches_controller_test.rb b/test/controllers/searches_controller_test.rb index b70fb36b8..68fdccc11 100644 --- a/test/controllers/searches_controller_test.rb +++ b/test/controllers/searches_controller_test.rb @@ -13,4 +13,17 @@ class SearchesControllerTest < ActionDispatch::IntegrationTest assert_select "li", text: /Layout is broken/ end + + test "show with card id" do + get search_path(q: cards(:logo).id) + + assert_select "form[data-controller='auto-submit']" + end + + test "show with non-existent card id" do + get search_path(q: "999999") + + assert_select "form[data-controller='auto-submit']", count: 0 + assert_select ".search__empty", text: "No matches" + end end