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. --- test/controllers/related_assets_controller_test.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/controllers/related_assets_controller_test.rb b/test/controllers/related_assets_controller_test.rb index a3082c2..7f6d487 100644 --- a/test/controllers/related_assets_controller_test.rb +++ b/test/controllers/related_assets_controller_test.rb @@ -11,7 +11,7 @@ class RelatedAssetsControllerTest < ActionController::TestCase Asset.create!(:name => "biometrics-poster", :upload_content_type => "image/png") Asset.create!(:name => "chaostreff-flyer", :upload_content_type => "image/png") - get :search, params: { :node_id => node.id, :q => "biometrics" } + get :search, params: { :node_id => node.id, :search_term => "biometrics" } json = JSON.parse(response.body) names = json.map { |a| a["name"] } @@ -20,13 +20,19 @@ class RelatedAssetsControllerTest < ActionController::TestCase assert_not_includes names, "chaostreff-flyer" end - test "search returns an empty list for a blank term" do + test "search with a blank term returns the most recently created assets" do + Asset.delete_all + RelatedAsset.delete_all login_as :quentin - node = Node.root.children.create!(:slug => "related_assets_blank_search_test") + node = Node.root.children.create!(:slug => "related_assets_recent_test") - get :search, params: { :node_id => node.id, :q => "" } + Asset.create!(:name => "older-photo", :upload_content_type => "image/png", :created_at => 2.days.ago) + Asset.create!(:name => "newer-photo", :upload_content_type => "image/png", :created_at => 1.hour.ago) - assert_equal [], JSON.parse(response.body) + get :search, params: { :node_id => node.id, :search_term => "" } + + json = JSON.parse(response.body) + assert_equal ["newer-photo", "older-photo"], json.map { |a| a["name"] } end test "create attaches an asset to the node's editable page" do -- cgit v1.3