summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-08-09 04:14:30 +0200
committererdgeist <erdgeist@erdgeist.org>2026-08-09 04:14:30 +0200
commit42b996e9197680a1955cfa5639462908677e5a94 (patch)
tree7f079ddf260fd67fc61686a7800625db47d1837c
parentd16161eab9029a509951146b80334c304c35fd34 (diff)
Attach an asset to a page from its edit form
-rw-r--r--app/controllers/assets_controller.rb12
-rw-r--r--config/locales/de.yml4
-rw-r--r--config/locales/en.yml4
-rw-r--r--test/controllers/assets_controller_test.rb21
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
38 # GET /assets/1/edit 38 # GET /assets/1/edit
39 def edit 39 def edit
40 @asset = Asset.find(params[:id]) 40 @asset = Asset.find(params[:id])
41 @attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present?
41 end 42 end
42 43
43 # POST /assets 44 # POST /assets
44 # POST /assets.xml 45 # POST /assets.xml
45 def create 46 def create
46 @asset = Asset.new(asset_params) 47 @asset = Asset.new(asset_params)
47 attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present? 48 @attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present?
48 49
49 respond_to do |format| 50 respond_to do |format|
50 if @asset.save 51 if @asset.save
@@ -54,7 +55,7 @@ class AssetsController < ApplicationController
54 :asset_name => @asset.name, 55 :asset_name => @asset.name,
55 :content_type => @asset.upload_content_type, 56 :content_type => @asset.upload_content_type,
56 :path => @asset.upload.url.sub(/\?\d+$/, "")) 57 :path => @asset.upload.url.sub(/\?\d+$/, ""))
57 attach_to(attach_node) if attach_node 58 attach_to(@attach_node) if @attach_node
58 format.html { redirect_to(@asset) } 59 format.html { redirect_to(@asset) }
59 format.xml { render :xml => @asset, :status => :created, :location => @asset } 60 format.xml { render :xml => @asset, :status => :created, :location => @asset }
60 else 61 else
@@ -68,10 +69,12 @@ class AssetsController < ApplicationController
68 # PUT /assets/1.xml 69 # PUT /assets/1.xml
69 def update 70 def update
70 @asset = Asset.find(params[:id]) 71 @asset = Asset.find(params[:id])
72 @attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present?
71 73
72 respond_to do |format| 74 respond_to do |format|
73 if @asset.update(asset_params) 75 if @asset.update(asset_params)
74 flash[:notice] = t("flash.assets.updated") 76 flash[:notice] = t("flash.assets.updated")
77 attach_to(@attach_node) if @attach_node
75 format.html { redirect_to(@asset) } 78 format.html { redirect_to(@asset) }
76 format.xml { head :ok } 79 format.xml { head :ok }
77 else 80 else
@@ -108,7 +111,7 @@ class AssetsController < ApplicationController
108 def attach_to node 111 def attach_to node
109 result = node.attach_asset!(@asset, :user => current_user, 112 result = node.attach_asset!(@asset, :user => current_user,
110 :headline => params[:headline].present?) 113 :headline => params[:headline].present?)
111 flash[:notice] = 114 sentence =
112 if result[:attached].zero? 115 if result[:attached].zero?
113 t("flash.assets.already_attached", :title => node.title) 116 t("flash.assets.already_attached", :title => node.title)
114 elsif result[:draft_created] 117 elsif result[:draft_created]
@@ -117,10 +120,11 @@ class AssetsController < ApplicationController
117 t("flash.assets.attached_to_draft", :title => node.title) 120 t("flash.assets.attached_to_draft", :title => node.title)
118 end 121 end
119 case result[:headline] 122 case result[:headline]
120 when :set then flash[:notice] += " " + t("flash.common.now_headline") 123 when :set then sentence += " " + t("flash.common.now_headline")
121 when :kept_existing then flash[:headline_kept_path] = node_path(node) 124 when :kept_existing then flash[:headline_kept_path] = node_path(node)
122 when :not_eligible then flash[:error] = t("flash.common.headline_ineligible") 125 when :not_eligible then flash[:error] = t("flash.common.headline_ineligible")
123 end 126 end
127 flash[:notice] = [flash[:notice], sentence].compact.join(" ")
124 rescue LockedByAnotherUser 128 rescue LockedByAnotherUser
125 flash[:locked_by] = node.lock_owner&.login 129 flash[:locked_by] = node.lock_owner&.login
126 flash[:locked_node_path] = node_path(node) 130 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:
656 assets: 656 assets:
657 created: "Asset wurde angelegt." 657 created: "Asset wurde angelegt."
658 updated: "Asset wurde aktualisiert." 658 updated: "Asset wurde aktualisiert."
659 already_attached: "Asset wurde gespeichert. Es war bereits an „%{title}“ angehängt." 659 already_attached: "Es war bereits an „%{title}“ angehängt."
660 attached_to_draft: "Zum Entwurf von %{title} hinzugefügt. Zum Veröffentlichen den Entwurf freigeben." 660 attached_to_draft: "Zum Entwurf von %{title} hinzugefügt. Zum Veröffentlichen den Entwurf freigeben."
661 attached_new_draft: "Entwurf von %{title} mit diesem Anhang angelegt. Zum Veröffentlichen den Entwurf freigeben." 661 attached_new_draft: "Entwurf von %{title} mit diesem Anhang angelegt. Zum Veröffentlichen den Entwurf freigeben."
662 events: 662 events:
@@ -721,7 +721,7 @@ de:
721 none_option: "— keine —" 721 none_option: "— keine —"
722 attach_to_page: "an Seite anhängen" 722 attach_to_page: "an Seite anhängen"
723 attach_search_placeholder: "Seite suchen…" 723 attach_search_placeholder: "Seite suchen…"
724 attach_hint: "Optional — hängt dieses Asset an die Seite an, alle ausstehenden Versionen eingeschlossen." 724 attach_hint: "Optional: Hängt dieses Asset an den Entwurf der Seite an. Es wird mit dem Entwurf veröffentlicht."
725 as_headline: "als Aufmacher der Seite anhängen" 725 as_headline: "als Aufmacher der Seite anhängen"
726 headline_hint: "Gilt nur, wenn die Seite noch keinen Aufmacher hat." 726 headline_hint: "Gilt nur, wenn die Seite noch keinen Aufmacher hat."
727 727
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:
624 assets: 624 assets:
625 created: "Asset was successfully created." 625 created: "Asset was successfully created."
626 updated: "Asset was successfully updated." 626 updated: "Asset was successfully updated."
627 already_attached: "Asset saved — it was already attached to “%{title}”." 627 already_attached: "It was already attached to “%{title}”."
628 attached_to_draft: "Attached to the draft of %{title}. Publish it to make the change live." 628 attached_to_draft: "Attached to the draft of %{title}. Publish it to make the change live."
629 attached_new_draft: "Created a draft of %{title} with this attachment. Publish it to make the change live." 629 attached_new_draft: "Created a draft of %{title} with this attachment. Publish it to make the change live."
630 events: 630 events:
@@ -689,7 +689,7 @@ en:
689 none_option: "— none —" 689 none_option: "— none —"
690 attach_to_page: "attach to page" 690 attach_to_page: "attach to page"
691 attach_search_placeholder: "Search for a page…" 691 attach_search_placeholder: "Search for a page…"
692 attach_hint: "Optional attaches this asset to that page, all pending versions included." 692 attach_hint: "Optional: attach this asset to that page's draft, to go live when the draft is published."
693 as_headline: "attach as the page's headline" 693 as_headline: "attach as the page's headline"
694 headline_hint: "Applies only if the page has no headline yet." 694 headline_hint: "Applies only if the page has no headline yet."
695 695
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
102 assert_response :redirect 102 assert_response :redirect
103 asset = Asset.last 103 asset = Asset.last
104 assert_includes node.draft.assets.reload, asset 104 assert_includes node.draft.assets.reload, asset
105 assert_equal I18n.t("flash.assets.attached_to_draft", :title => node.title), flash[:notice] 105 assert_equal [I18n.t("flash.assets.created"),
106 I18n.t("flash.assets.attached_to_draft", :title => node.title)].join(" "),
107 flash[:notice]
106 end 108 end
107 109
108 test "create with node_id creates a draft when none is pending" do 110 test "create with node_id creates a draft when none is pending" do
@@ -116,7 +118,9 @@ class AssetsControllerTest < ActionController::TestCase
116 assert_includes node.draft.assets.reload, Asset.last 118 assert_includes node.draft.assets.reload, Asset.last
117 assert_empty node.head.assets.reload 119 assert_empty node.head.assets.reload
118 assert_equal users(:quentin), node.draft.editor 120 assert_equal users(:quentin), node.draft.editor
119 assert_equal I18n.t("flash.assets.attached_new_draft", :title => node.title), flash[:notice] 121 assert_equal [I18n.t("flash.assets.created"),
122 I18n.t("flash.assets.attached_new_draft", :title => node.title)].join(" "),
123 flash[:notice]
120 end 124 end
121 125
122 test "create against a foreign-locked node keeps the asset but refuses the attach" do 126 test "create against a foreign-locked node keeps the asset but refuses the attach" do
@@ -159,6 +163,19 @@ class AssetsControllerTest < ActionController::TestCase
159 assert_equal users(:quentin), action.user 163 assert_equal users(:quentin), action.user
160 end 164 end
161 165
166 test "update with node_id attaches the asset to the node's draft" do
167 node = Node.root.children.create!(:slug => "asset_update_attach")
168 asset = Asset.create!(:name => "Existing", :upload_content_type => "image/png")
169
170 put :update, params: { id: asset.id, asset: { name: "Existing" }, node_id: node.id }
171
172 assert_response :redirect
173 assert_includes node.draft.assets.reload, asset
174 assert_equal [I18n.t("flash.assets.updated"),
175 I18n.t("flash.assets.attached_to_draft", :title => node.title)].join(" "),
176 flash[:notice]
177 end
178
162 # --- edit --- 179 # --- edit ---
163 180
164 test "get edit" do 181 test "get edit" do