diff options
Diffstat (limited to 'app/controllers')
| -rw-r--r-- | app/controllers/assets_controller.rb | 22 | ||||
| -rw-r--r-- | app/controllers/nodes_controller.rb | 16 |
2 files changed, 33 insertions, 5 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 |
diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index 6caa827..ce8f053 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb | |||
| @@ -30,6 +30,7 @@ class NodesController < ApplicationController | |||
| 30 | @node = Node.new node_params | 30 | @node = Node.new node_params |
| 31 | @selected_kind = CccConventions::NODE_KINDS.key?(params[:kind]) ? params[:kind] : "generic" | 31 | @selected_kind = CccConventions::NODE_KINDS.key?(params[:kind]) ? params[:kind] : "generic" |
| 32 | @parent = Node.find(params[:parent_id]) if params.has_key?(:parent_id) | 32 | @parent = Node.find(params[:parent_id]) if params.has_key?(:parent_id) |
| 33 | @attach_asset = Asset.find(params[:asset_id]) if params.has_key?(:asset_id) | ||
| 33 | end | 34 | end |
| 34 | 35 | ||
| 35 | def create | 36 | def create |
| @@ -51,10 +52,19 @@ class NodesController < ApplicationController | |||
| 51 | :action => "create", | 52 | :action => "create", |
| 52 | :title => params[:title], :path => @node.unique_name) | 53 | :title => params[:title], :path => @node.unique_name) |
| 53 | 54 | ||
| 55 | if params[:asset_id].present? && (asset = Asset.find(params[:asset_id])) | ||
| 56 | result = @node.attach_asset!(asset, :user => current_user, | ||
| 57 | :headline => params[:asset_headline].present?) | ||
| 58 | flash[:notice] = "Page created with “#{asset.name}” attached." | ||
| 59 | flash[:notice] += " It is the page's headline." if result[:headline] == :set | ||
| 60 | flash[:error] = "This asset type cannot be a headline." if result[:headline] == :not_eligible | ||
| 61 | end | ||
| 62 | |||
| 54 | redirect_to(edit_node_path(@node)) | 63 | redirect_to(edit_node_path(@node)) |
| 55 | else | 64 | else |
| 56 | @selected_kind = CccConventions::NODE_KINDS.key?(params[:kind]) ? params[:kind] : "generic" | 65 | @selected_kind = CccConventions::NODE_KINDS.key?(params[:kind]) ? params[:kind] : "generic" |
| 57 | @parent = Node.find(params[:parent_id]) if params.has_key?(:parent_id) | 66 | @parent = Node.find(params[:parent_id]) if params.has_key?(:parent_id) |
| 67 | @attach_asset = Asset.find(params[:asset_id]) if params.has_key?(:asset_id) | ||
| 58 | render :new | 68 | render :new |
| 59 | end | 69 | end |
| 60 | end | 70 | end |
| @@ -77,7 +87,7 @@ class NodesController < ApplicationController | |||
| 77 | "This page has unsaved changes from a previous session, shown below. " \ | 87 | "This page has unsaved changes from a previous session, shown below. " \ |
| 78 | "Save to keep them, or use \"Discard Autosave\" below to go back to the last saved version." | 88 | "Save to keep them, or use \"Discard Autosave\" below to go back to the last saved version." |
| 79 | elsif freshly_locked | 89 | elsif freshly_locked |
| 80 | flash.now[:notice] = "Node locked and ready to edit" | 90 | flash.now[:notice] ||= "Node locked and ready to edit" |
| 81 | end | 91 | end |
| 82 | rescue LockedByAnotherUser => e | 92 | rescue LockedByAnotherUser => e |
| 83 | flash[:error] = e.message | 93 | flash[:error] = e.message |
| @@ -223,10 +233,6 @@ class NodesController < ApplicationController | |||
| 223 | @nodes = index_matching(Node.drafts_and_autosaves) | 233 | @nodes = index_matching(Node.drafts_and_autosaves) |
| 224 | end | 234 | end |
| 225 | 235 | ||
| 226 | def recent | ||
| 227 | @nodes = index_matching(Node.recently_changed) | ||
| 228 | end | ||
| 229 | |||
| 230 | def mine | 236 | def mine |
| 231 | base = Node.joins(:pages) | 237 | base = Node.joins(:pages) |
| 232 | .where("pages.user_id = ? or pages.editor_id = ?", current_user, current_user) | 238 | .where("pages.user_id = ? or pages.editor_id = ?", current_user, current_user) |
