From 616a0d2d2ba15a7e176938ee829d60366fb6a9c2 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sun, 9 Aug 2026 13:26:23 +0200 Subject: Attach an asset to a page from the asset's own page The picker moves from assets/edit to assets/show, where the list of attached pages already is. A member route, POST assets/:id/attach_to_node, replaces the ride on #update. Supersedes the picker wiring in the previous commit; what remains of it is @attach_node in #create, the flash composition and the corrected hint. --- app/controllers/assets_controller.rb | 16 ++++++++++++-- app/views/assets/edit.html.erb | 17 --------------- app/views/assets/show.html.erb | 11 ++++++++++ config/locales/de.yml | 2 ++ config/locales/en.yml | 2 ++ config/routes.rb | 7 +++++- test/controllers/assets_controller_test.rb | 34 ++++++++++++++++++------------ 7 files changed, 56 insertions(+), 33 deletions(-) diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index e58f4f30..4b27d1c4 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -38,7 +38,6 @@ class AssetsController < ApplicationController # GET /assets/1/edit def edit @asset = Asset.find(params[:id]) - @attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present? end # POST /assets @@ -69,7 +68,6 @@ class AssetsController < ApplicationController # PUT /assets/1.xml def update @asset = Asset.find(params[:id]) - @attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present? respond_to do |format| if @asset.update(asset_params) @@ -102,6 +100,20 @@ class AssetsController < ApplicationController end end + # POST /assets/1/attach_to_node + def attach_to_node + @asset = Asset.find(params[:id]) + node = Node.not_in_trash.find_by(:id => params[:node_id]) + + if node + attach_to(node) + else + flash[:error] = t("flash.assets.attach_no_node") + end + + redirect_to(asset_path(@asset)) + end + private def asset_params diff --git a/app/views/assets/edit.html.erb b/app/views/assets/edit.html.erb index 5214e082..610ae20a 100644 --- a/app/views/assets/edit.html.erb +++ b/app/views/assets/edit.html.erb @@ -36,23 +36,6 @@ ) %> -
<%= t("assets.form.attach_to_page") %>
-
-
- <%= text_field_tag :asset_node_search_term, @attach_node&.title, - :placeholder => t("assets.form.attach_search_placeholder"), :autocomplete => "off" %> - -
- <%= hidden_field_tag :node_id, @attach_node&.id %> - <%= t("assets.form.attach_hint") %> -
- -
-
- - <%= t("assets.form.headline_hint") %> -
-
<%= t("admin.columns.actions") %>
diff --git a/app/views/assets/show.html.erb b/app/views/assets/show.html.erb index d76d312b..f31d34e1 100644 --- a/app/views/assets/show.html.erb +++ b/app/views/assets/show.html.erb @@ -47,6 +47,17 @@ <%= link_to new_node_path(:asset_id => @asset.id), :class => "action_button" do %> <%= icon("file-plus", library: "tabler", "aria-hidden": true) %> <%= t(".new_page_with_attachment") %> <% end %> + <%= form_tag attach_to_node_asset_path(@asset), :class => "asset_attach_form" do %> +
+ <%= text_field_tag :asset_node_search_term, nil, + :placeholder => t("assets.form.attach_search_placeholder"), :autocomplete => "off" %> + +
+ <%= hidden_field_tag :node_id, nil %> + + <%= submit_tag t(".attach_button") %> + <%= t("assets.form.attach_hint") %> + <% end %>
<%= t(".public_path") %>
diff --git a/config/locales/de.yml b/config/locales/de.yml index aab46169..e410e519 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -659,6 +659,7 @@ de: already_attached: "Es war bereits an „%{title}“ angehängt." attached_to_draft: "Zum Entwurf von %{title} hinzugefügt. Zum Veröffentlichen den Entwurf freigeben." attached_new_draft: "Entwurf von %{title} mit diesem Anhang angelegt. Zum Veröffentlichen den Entwurf freigeben." + attach_no_node: "Es wurde keine Seite ausgewählt, es wurde nichts angehängt." events: created: "Termin wurde angelegt." updated: "Termin wurde aktualisiert." @@ -703,6 +704,7 @@ de: pending_only: "nur in ausstehenden Versionen" not_attached: "— an keine Seite angehängt —" new_page_with_attachment: "Neue Seite mit diesem Anhang" + attach_button: "Anhängen" public_path: "Öffentlicher Pfad" copy_url: "URL kopieren" creator: "Urheber" diff --git a/config/locales/en.yml b/config/locales/en.yml index 8da079dc..f8f586b9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -627,6 +627,7 @@ en: already_attached: "It was already attached to “%{title}”." attached_to_draft: "Attached to the draft of %{title}. Publish it to make the change live." attached_new_draft: "Created a draft of %{title} with this attachment. Publish it to make the change live." + attach_no_node: "No page was chosen, so nothing was attached." events: created: "Event was successfully created." updated: "Event was successfully updated." @@ -670,6 +671,7 @@ en: attached_to: "Attached to" pending_only: "pending versions only" not_attached: "— not attached to any page —" + attach_button: "Attach" new_page_with_attachment: "New page with this attachment" public_path: "Public Path" copy_url: "Copy URL" diff --git a/config/routes.rb b/config/routes.rb index a43769e6..e123a3fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,7 +26,12 @@ Cccms::Application.routes.draw do scope '/admin' do resources :events - resources :assets + + resources :assets do + member do + post :attach_to_node + end + end resources :nodes do collection do diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb index c2d7ad57..67f629d9 100644 --- a/test/controllers/assets_controller_test.rb +++ b/test/controllers/assets_controller_test.rb @@ -163,19 +163,6 @@ class AssetsControllerTest < ActionController::TestCase assert_equal users(:quentin), action.user end - test "update with node_id attaches the asset to the node's draft" do - node = Node.root.children.create!(:slug => "asset_update_attach") - asset = Asset.create!(:name => "Existing", :upload_content_type => "image/png") - - put :update, params: { id: asset.id, asset: { name: "Existing" }, node_id: node.id } - - assert_response :redirect - assert_includes node.draft.assets.reload, asset - assert_equal [I18n.t("flash.assets.updated"), - I18n.t("flash.assets.attached_to_draft", :title => node.title)].join(" "), - flash[:notice] - end - # --- edit --- test "get edit" do @@ -235,6 +222,27 @@ class AssetsControllerTest < ActionController::TestCase assert_equal users(:quentin), NodeAction.last.user end + test "attach_to_node attaches the asset to the node's draft" do + node = Node.root.children.create!(:slug => "asset_show_attach") + asset = Asset.create!(:name => "From the asset page", :upload_content_type => "image/png") + + post :attach_to_node, params: { id: asset.id, node_id: node.id } + + assert_redirected_to asset_path(asset) + assert_includes node.draft.assets.reload, asset + assert_equal I18n.t("flash.assets.attached_to_draft", :title => node.title), flash[:notice] + end + + test "attach_to_node without a node says so and attaches nothing" do + asset = Asset.create!(:name => "Unpicked", :upload_content_type => "image/png") + + post :attach_to_node, params: { id: asset.id } + + assert_redirected_to asset_path(asset) + assert_equal I18n.t("flash.assets.attach_no_node"), flash[:error] + assert_empty asset.related_assets.reload + end + # --- URL helpers --- test "upload url returns correct path for original" do -- cgit v1.3