diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-07-13 03:22:37 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-07-13 03:22:37 +0200 |
| commit | 70653b681d10917b77dced08f577446ced7568f1 (patch) | |
| tree | 3c92383e29da79beb3e1419c153952a4aa7dc1ab /test | |
| parent | 65c7c40f74c315c1a39fb15da8ce341fb8b9b05e (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."
Diffstat (limited to 'test')
| -rw-r--r-- | test/controllers/related_assets_controller_test.rb | 83 |
1 files changed, 83 insertions, 0 deletions
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 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class 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 | ||
| 83 | end | ||
