diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-08-09 04:14:30 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-08-09 04:14:30 +0200 |
| commit | 42b996e9197680a1955cfa5639462908677e5a94 (patch) | |
| tree | 7f079ddf260fd67fc61686a7800625db47d1837c /app/controllers/assets_controller.rb | |
| parent | d16161eab9029a509951146b80334c304c35fd34 (diff) | |
Attach an asset to a page from its edit form
Diffstat (limited to 'app/controllers/assets_controller.rb')
| -rw-r--r-- | app/controllers/assets_controller.rb | 12 |
1 files changed, 8 insertions, 4 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) |
