summaryrefslogtreecommitdiff
path: root/app/controllers/assets_controller.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/assets_controller.rb')
-rw-r--r--app/controllers/assets_controller.rb24
1 files changed, 23 insertions, 1 deletions
diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb
index d150e06..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
@@ -89,6 +92,25 @@ class AssetsController < ApplicationController
89 private 92 private
90 93
91 def asset_params 94 def asset_params
92 params.require(:asset).permit(:name, :upload) 95 params.require(:asset).permit(:name, :upload, :creator, :source_url, :license_key)
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)
93 end 115 end
94end 116end