From 42b996e9197680a1955cfa5639462908677e5a94 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sun, 9 Aug 2026 04:14:30 +0200 Subject: Attach an asset to a page from its edit form --- app/controllers/assets_controller.rb | 12 ++++++++---- config/locales/de.yml | 4 ++-- config/locales/en.yml | 4 ++-- test/controllers/assets_controller_test.rb | 21 +++++++++++++++++++-- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index d86370f4..e58f4f30 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -38,13 +38,14 @@ 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 # POST /assets.xml def create @asset = Asset.new(asset_params) - attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present? + @attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present? respond_to do |format| if @asset.save @@ -54,7 +55,7 @@ class AssetsController < ApplicationController :asset_name => @asset.name, :content_type => @asset.upload_content_type, :path => @asset.upload.url.sub(/\?\d+$/, "")) - attach_to(attach_node) if attach_node + attach_to(@attach_node) if @attach_node format.html { redirect_to(@asset) } format.xml { render :xml => @asset, :status => :created, :location => @asset } else @@ -68,10 +69,12 @@ 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) flash[:notice] = t("flash.assets.updated") + attach_to(@attach_node) if @attach_node format.html { redirect_to(@asset) } format.xml { head :ok } else @@ -108,7 +111,7 @@ class AssetsController < ApplicationController def attach_to node result = node.attach_asset!(@asset, :user => current_user, :headline => params[:headline].present?) - flash[:notice] = + sentence = if result[:attached].zero? t("flash.assets.already_attached", :title => node.title) elsif result[:draft_created] @@ -117,10 +120,11 @@ class AssetsController < ApplicationController t("flash.assets.attached_to_draft", :title => node.title) end case result[:headline] - when :set then flash[:notice] += " " + t("flash.common.now_headline") + when :set then sentence += " " + t("flash.common.now_headline") when :kept_existing then flash[:headline_kept_path] = node_path(node) when :not_eligible then flash[:error] = t("flash.common.headline_ineligible") end + flash[:notice] = [flash[:notice], sentence].compact.join(" ") rescue LockedByAnotherUser flash[:locked_by] = node.lock_owner&.login flash[:locked_node_path] = node_path(node) diff --git a/config/locales/de.yml b/config/locales/de.yml index 92a46768..aab46169 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -656,7 +656,7 @@ de: assets: created: "Asset wurde angelegt." updated: "Asset wurde aktualisiert." - already_attached: "Asset wurde gespeichert. Es war bereits an „%{title}“ angehängt." + 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." events: @@ -721,7 +721,7 @@ de: none_option: "— keine —" attach_to_page: "an Seite anhängen" attach_search_placeholder: "Seite suchen…" - attach_hint: "Optional — hängt dieses Asset an die Seite an, alle ausstehenden Versionen eingeschlossen." + attach_hint: "Optional: Hängt dieses Asset an den Entwurf der Seite an. Es wird mit dem Entwurf veröffentlicht." as_headline: "als Aufmacher der Seite anhängen" headline_hint: "Gilt nur, wenn die Seite noch keinen Aufmacher hat." diff --git a/config/locales/en.yml b/config/locales/en.yml index 458a4069..8da079dc 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -624,7 +624,7 @@ en: assets: created: "Asset was successfully created." updated: "Asset was successfully updated." - already_attached: "Asset saved — it was already attached to “%{title}”." + 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." events: @@ -689,7 +689,7 @@ en: none_option: "— none —" attach_to_page: "attach to page" attach_search_placeholder: "Search for a page…" - attach_hint: "Optional — attaches this asset to that page, all pending versions included." + attach_hint: "Optional: attach this asset to that page's draft, to go live when the draft is published." as_headline: "attach as the page's headline" headline_hint: "Applies only if the page has no headline yet." diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb index aa3e00e6..c2d7ad57 100644 --- a/test/controllers/assets_controller_test.rb +++ b/test/controllers/assets_controller_test.rb @@ -102,7 +102,9 @@ class AssetsControllerTest < ActionController::TestCase assert_response :redirect asset = Asset.last assert_includes node.draft.assets.reload, asset - assert_equal I18n.t("flash.assets.attached_to_draft", :title => node.title), flash[:notice] + assert_equal [I18n.t("flash.assets.created"), + I18n.t("flash.assets.attached_to_draft", :title => node.title)].join(" "), + flash[:notice] end test "create with node_id creates a draft when none is pending" do @@ -116,7 +118,9 @@ class AssetsControllerTest < ActionController::TestCase assert_includes node.draft.assets.reload, Asset.last assert_empty node.head.assets.reload assert_equal users(:quentin), node.draft.editor - assert_equal I18n.t("flash.assets.attached_new_draft", :title => node.title), flash[:notice] + assert_equal [I18n.t("flash.assets.created"), + I18n.t("flash.assets.attached_new_draft", :title => node.title)].join(" "), + flash[:notice] end test "create against a foreign-locked node keeps the asset but refuses the attach" do @@ -159,6 +163,19 @@ 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 -- cgit v1.3