diff options
Diffstat (limited to 'app/controllers/assets_controller.rb')
| -rw-r--r-- | app/controllers/assets_controller.rb | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index becfe13..fbede0a 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb | |||
| @@ -28,6 +28,7 @@ class AssetsController < ApplicationController | |||
| 28 | # GET /assets/new.xml | 28 | # GET /assets/new.xml |
| 29 | def new | 29 | def new |
| 30 | @asset = Asset.new | 30 | @asset = Asset.new |
| 31 | @attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present? | ||
| 31 | 32 | ||
| 32 | respond_to do |format| | 33 | respond_to do |format| |
| 33 | format.html # new.html.erb | 34 | format.html # new.html.erb |
| @@ -44,10 +45,12 @@ class AssetsController < ApplicationController | |||
| 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) |
| 48 | attach_node = Node.not_in_trash.find_by(:id => params[:node_id]) if params[:node_id].present? | ||
| 47 | 49 | ||
| 48 | respond_to do |format| | 50 | respond_to do |format| |
| 49 | if @asset.save | 51 | if @asset.save |
| 50 | flash[:notice] = 'Asset was successfully created.' | 52 | flash[:notice] = 'Asset was successfully created.' |
| 53 | attach_to(attach_node) if attach_node | ||
| 51 | format.html { redirect_to(@asset) } | 54 | format.html { redirect_to(@asset) } |
| 52 | format.xml { render :xml => @asset, :status => :created, :location => @asset } | 55 | format.xml { render :xml => @asset, :status => :created, :location => @asset } |
| 53 | else | 56 | else |
| @@ -91,4 +94,23 @@ class AssetsController < ApplicationController | |||
| 91 | def asset_params | 94 | def asset_params |
| 92 | params.require(:asset).permit(:name, :upload, :creator, :source_url, :license_key) | 95 | params.require(:asset).permit(:name, :upload, :creator, :source_url, :license_key) |
| 93 | end | 96 | end |
| 97 | |||
| 98 | def attach_to node | ||
| 99 | result = node.attach_asset!(@asset, :user => current_user, | ||
| 100 | :headline => params[:headline].present?) | ||
| 101 | flash[:notice] = | ||
| 102 | if result[:attached].zero? | ||
| 103 | "Asset saved — it was already attached to “#{node.title}”." | ||
| 104 | else | ||
| 105 | "Asset was successfully created and attached to “#{node.title}”." | ||
| 106 | end | ||
| 107 | case result[:headline] | ||
| 108 | when :set then flash[:notice] += " It is now the page's headline." | ||
| 109 | when :kept_existing then flash[:headline_kept_path] = node_path(node) | ||
| 110 | when :not_eligible then flash[:error] = "This asset type cannot be a headline." | ||
| 111 | end | ||
| 112 | rescue LockedByAnotherUser | ||
| 113 | flash[:locked_by] = node.lock_owner&.login | ||
| 114 | flash[:locked_node_path] = node_path(node) | ||
| 115 | end | ||
| 94 | end | 116 | end |
