From 5f56fa867f073d8d1aecbfbba4a340ea414cfb0d Mon Sep 17 00:00:00 2001 From: erdgeist Date: Mon, 13 Jul 2026 03:48:28 +0200 Subject: Add loadOnFocus to initSearchPicker; asset search shows recent by default initSearchPicker's input handler has always short-circuited on an empty term -- correct for every existing picker, none of which want results appearing before anything's typed. The asset picker does want exactly that (show the last few uploaded images without requiring a search first), so this adds an opt-in loadOnFocus option instead of changing the shared default: it fires once when the input gains focus with no term yet, reusing the same request/render path a real search uses rather than a separate code path. The AJAX call itself was pulled out into a named runSearch function so both triggers could share it without duplicating the success/render logic. RelatedAssetsController#search now treats a blank term as "show the most recently created, unattached images" (limit 5) rather than returning nothing, matching the new client behavior. A real search term still returns up to 10 name-matched results, unchanged. --- app/controllers/related_assets_controller.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/related_assets_controller.rb b/app/controllers/related_assets_controller.rb index 906c02c..ae37d2f 100644 --- a/app/controllers/related_assets_controller.rb +++ b/app/controllers/related_assets_controller.rb @@ -3,17 +3,15 @@ class RelatedAssetsController < ApplicationController before_action :find_node def search - term = params[:q].to_s.strip - if term.blank? - render json: [] - return - end - + term = params[:search_term].to_s.strip attached_ids = @node.editable_page.related_assets.pluck(:asset_id) - results = Asset.images - .where("name ILIKE ?", "%#{term}%") - .where.not(id: attached_ids) - .limit(10) + scope = Asset.images.where.not(id: attached_ids) + + results = if term.present? + scope.where("name ILIKE ?", "%#{term}%").limit(10) + else + scope.order(created_at: :desc).limit(5) + end render json: results.map { |a| { id: a.id, name: a.name, thumb_url: a.upload.url(:thumb) } -- cgit v1.3