summaryrefslogtreecommitdiff
path: root/app/controllers/related_assets_controller.rb
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-13 03:48:28 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-13 03:48:28 +0200
commit5f56fa867f073d8d1aecbfbba4a340ea414cfb0d (patch)
tree5f39c0386b2e5819289e916ccdb67b987c2131fa /app/controllers/related_assets_controller.rb
parent70653b681d10917b77dced08f577446ced7568f1 (diff)
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.
Diffstat (limited to 'app/controllers/related_assets_controller.rb')
-rw-r--r--app/controllers/related_assets_controller.rb18
1 files changed, 8 insertions, 10 deletions
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
3 before_action :find_node 3 before_action :find_node
4 4
5 def search 5 def search
6 term = params[:q].to_s.strip 6 term = params[:search_term].to_s.strip
7 if term.blank?
8 render json: []
9 return
10 end
11
12 attached_ids = @node.editable_page.related_assets.pluck(:asset_id) 7 attached_ids = @node.editable_page.related_assets.pluck(:asset_id)
13 results = Asset.images 8 scope = Asset.images.where.not(id: attached_ids)
14 .where("name ILIKE ?", "%#{term}%") 9
15 .where.not(id: attached_ids) 10 results = if term.present?
16 .limit(10) 11 scope.where("name ILIKE ?", "%#{term}%").limit(10)
12 else
13 scope.order(created_at: :desc).limit(5)
14 end
17 15
18 render json: results.map { |a| 16 render json: results.map { |a|
19 { id: a.id, name: a.name, thumb_url: a.upload.url(:thumb) } 17 { id: a.id, name: a.name, thumb_url: a.upload.url(:thumb) }