summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-13 03:22:37 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-13 03:22:37 +0200
commit70653b681d10917b77dced08f577446ced7568f1 (patch)
tree3c92383e29da79beb3e1419c153952a4aa7dc1ab
parent65c7c40f74c315c1a39fb15da8ce341fb8b9b05e (diff)
Add RelatedAssetsController: search, attach, detach, reorder
Backend for the asset-picker rebuild -- replaces the plan to dump every image asset in the system into a hidden, unfiltered browse panel on every node edit (the actual current behavior, confirmed by reading nodes/edit.html.erb directly) with a small, name-scoped search endpoint plus create/destroy/update for attach, detach, and reorder. No schema change needed -- RelatedAsset already had everything this requires (asset_id, page_id, an acts_as_list position). search excludes assets already attached to the page, keeping results meaningfully small given hundreds of assets total but only a handful per node in practice. create is find_or_create_by! rather than a bare create!, guarding against the same asset being attached twice from two separate search results. update leans on acts_as_list's own insert_at rather than custom position-shifting logic. Node#editable_page (autosave || draft || head) is extracted since this is now its third call site with identical logic -- deliberately not touching nodes#show, which resolves draft || head without autosave on purpose, a different and correct semantic for "current committed state" versus "what's actively being edited."
-rw-r--r--app/controllers/related_assets_controller.rb50
-rw-r--r--app/models/node.rb4
-rw-r--r--config/routes.rb6
-rw-r--r--test/controllers/related_assets_controller_test.rb83
4 files changed, 143 insertions, 0 deletions
diff --git a/app/controllers/related_assets_controller.rb b/app/controllers/related_assets_controller.rb
new file mode 100644
index 0000000..906c02c
--- /dev/null
+++ b/app/controllers/related_assets_controller.rb
@@ -0,0 +1,50 @@
1class RelatedAssetsController < ApplicationController
2 before_action :login_required
3 before_action :find_node
4
5 def search
6 term = params[:q].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)
13 results = Asset.images
14 .where("name ILIKE ?", "%#{term}%")
15 .where.not(id: attached_ids)
16 .limit(10)
17
18 render json: results.map { |a|
19 { id: a.id, name: a.name, thumb_url: a.upload.url(:thumb) }
20 }
21 end
22
23 def create
24 asset = Asset.find(params[:asset_id])
25 related = @node.editable_page.related_assets.find_or_create_by!(asset: asset)
26
27 render json: {
28 id: related.id,
29 asset_id: asset.id,
30 name: asset.name,
31 thumb_url: asset.upload.url(:thumb)
32 }
33 end
34
35 def destroy
36 @node.editable_page.related_assets.find(params[:id]).destroy
37 head :ok
38 end
39
40 def update
41 @node.editable_page.related_assets.find(params[:id]).insert_at(params[:position].to_i)
42 head :ok
43 end
44
45 private
46
47 def find_node
48 @node = Node.find(params[:node_id])
49 end
50end
diff --git a/app/models/node.rb b/app/models/node.rb
index 1d0a089..0361c1e 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -328,6 +328,10 @@ class Node < ApplicationRecord
328 unique_path.length == 3 && unique_path[0] == "updates" 328 unique_path.length == 3 && unique_path[0] == "updates"
329 end 329 end
330 330
331 def editable_page
332 autosave || draft || head
333 end
334
331 # Returns immutable node id for all new nodes so that the atom feed entry ids 335 # Returns immutable node id for all new nodes so that the atom feed entry ids
332 # stay the same eventhough the slug or positions changes. 336 # stay the same eventhough the slug or positions changes.
333 # Can be removed after a year or so ;) 337 # Can be removed after a year or so ;)
diff --git a/config/routes.rb b/config/routes.rb
index 92301e5..5d61bae 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -55,6 +55,12 @@ Cccms::Application.routes.draw do
55 put :revert 55 put :revert
56 end 56 end
57 57
58 resources :related_assets, only: [:create, :destroy, :update] do
59 collection do
60 get :search
61 end
62 end
63
58 resources :revisions do 64 resources :revisions do
59 collection do 65 collection do
60 post :diff 66 post :diff
diff --git a/test/controllers/related_assets_controller_test.rb b/test/controllers/related_assets_controller_test.rb
new file mode 100644
index 0000000..a3082c2
--- /dev/null
+++ b/test/controllers/related_assets_controller_test.rb
@@ -0,0 +1,83 @@
1require 'test_helper'
2
3class RelatedAssetsControllerTest < ActionController::TestCase
4 test "search finds assets by name, excluding ones already attached" do
5 login_as :quentin
6 node = Node.root.children.create!(:slug => "related_assets_search_test")
7
8 attached = Asset.create!(:name => "biometrics-scan", :upload_content_type => "image/png")
9 node.draft.assets << attached
10
11 Asset.create!(:name => "biometrics-poster", :upload_content_type => "image/png")
12 Asset.create!(:name => "chaostreff-flyer", :upload_content_type => "image/png")
13
14 get :search, params: { :node_id => node.id, :q => "biometrics" }
15
16 json = JSON.parse(response.body)
17 names = json.map { |a| a["name"] }
18 assert_includes names, "biometrics-poster"
19 assert_not_includes names, "biometrics-scan"
20 assert_not_includes names, "chaostreff-flyer"
21 end
22
23 test "search returns an empty list for a blank term" do
24 login_as :quentin
25 node = Node.root.children.create!(:slug => "related_assets_blank_search_test")
26
27 get :search, params: { :node_id => node.id, :q => "" }
28
29 assert_equal [], JSON.parse(response.body)
30 end
31
32 test "create attaches an asset to the node's editable page" do
33 login_as :quentin
34 node = Node.root.children.create!(:slug => "related_assets_create_test")
35 asset = Asset.create!(:name => "erfa-photo", :upload_content_type => "image/png")
36
37 post :create, params: { :node_id => node.id, :asset_id => asset.id }
38
39 assert_response :success
40 assert_includes node.draft.reload.related_assets.map(&:asset_id), asset.id
41 end
42
43 test "create does not duplicate an already-attached asset" do
44 login_as :quentin
45 node = Node.root.children.create!(:slug => "related_assets_dup_test")
46 asset = Asset.create!(:name => "erfa-photo-2", :upload_content_type => "image/png")
47 node.draft.assets << asset
48
49 post :create, params: { :node_id => node.id, :asset_id => asset.id }
50
51 assert_response :success
52 assert_equal 1, node.draft.reload.related_assets.count
53 end
54
55 test "destroy removes the attached asset" do
56 login_as :quentin
57 node = Node.root.children.create!(:slug => "related_assets_destroy_test")
58 asset = Asset.create!(:name => "old-photo", :upload_content_type => "image/png")
59 node.draft.assets << asset
60 related = node.draft.related_assets.first
61
62 delete :destroy, params: { :node_id => node.id, :id => related.id }
63
64 assert_response :success
65 assert_equal 0, node.draft.reload.related_assets.count
66 end
67
68 test "update reorders the attached assets" do
69 login_as :quentin
70 node = Node.root.children.create!(:slug => "related_assets_reorder_test")
71 first = Asset.create!(:name => "first-photo", :upload_content_type => "image/png")
72 second = Asset.create!(:name => "second-photo", :upload_content_type => "image/png")
73 node.draft.assets << first
74 node.draft.assets << second
75
76 second_related = node.draft.related_assets.find_by(:asset_id => second.id)
77 patch :update, params: { :node_id => node.id, :id => second_related.id, :position => 1 }
78
79 assert_response :success
80 ordered_asset_ids = node.draft.reload.related_assets.map(&:asset_id)
81 assert_equal [second.id, first.id], ordered_asset_ids
82 end
83end