diff options
| -rw-r--r-- | app/controllers/nodes_controller.rb | 10 | ||||
| -rw-r--r-- | app/models/asset.rb | 9 | ||||
| -rw-r--r-- | app/views/assets/show.html.erb | 25 | ||||
| -rw-r--r-- | app/views/nodes/new.html.erb | 18 | ||||
| -rw-r--r-- | test/controllers/nodes_controller_test.rb | 4 |
5 files changed, 64 insertions, 2 deletions
diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index 87d2681..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 |
diff --git a/app/models/asset.rb b/app/models/asset.rb index ba9c2f0..73970e2 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb | |||
| @@ -31,4 +31,13 @@ class Asset < ApplicationRecord | |||
| 31 | def show_credit? | 31 | def show_credit? |
| 32 | image? && has_credit? | 32 | image? && has_credit? |
| 33 | end | 33 | end |
| 34 | |||
| 35 | # Nodes whose current lifecycle rows (head, draft or autosave) carry | ||
| 36 | # this asset. Historical revisions also hold RelatedAsset rows; they | ||
| 37 | # are excluded by construction here, since nothing points at them. | ||
| 38 | def attached_nodes | ||
| 39 | page_ids = related_assets.select(:page_id) | ||
| 40 | Node.where("head_id IN (:ids) OR draft_id IN (:ids) OR autosave_id IN (:ids)", | ||
| 41 | :ids => page_ids).distinct | ||
| 42 | end | ||
| 34 | end | 43 | end |
diff --git a/app/views/assets/show.html.erb b/app/views/assets/show.html.erb index e551e35..2723821 100644 --- a/app/views/assets/show.html.erb +++ b/app/views/assets/show.html.erb | |||
| @@ -21,6 +21,31 @@ | |||
| 21 | <div class="node_content"><%= image_tag @asset.upload.url(:medium), style: "max-width: 300px; max-height: 300px;" %></div> | 21 | <div class="node_content"><%= image_tag @asset.upload.url(:medium), style: "max-width: 300px; max-height: 300px;" %></div> |
| 22 | <% end %> | 22 | <% end %> |
| 23 | 23 | ||
| 24 | <div class="node_description">Attached to</div> | ||
| 25 | <div class="node_content"> | ||
| 26 | <% nodes = @asset.attached_nodes %> | ||
| 27 | <% if nodes.any? %> | ||
| 28 | <ul class="search_results_list"> | ||
| 29 | <% nodes.each do |node| %> | ||
| 30 | <li> | ||
| 31 | <%= link_to node.title, node_path(node) %> | ||
| 32 | <% if node.editable_page&.headline_asset == @asset %> | ||
| 33 | <%= icon("star", library: "tabler", "aria-hidden": true) %> | ||
| 34 | <% end %> | ||
| 35 | <% unless node.head && node.head.related_assets.exists?(:asset_id => @asset.id) %> | ||
| 36 | <span class="field_hint">pending versions only</span> | ||
| 37 | <% end %> | ||
| 38 | </li> | ||
| 39 | <% end %> | ||
| 40 | </ul> | ||
| 41 | <% else %> | ||
| 42 | — not attached to any page — | ||
| 43 | <% end %> | ||
| 44 | <%= link_to new_node_path(:asset_id => @asset.id), :class => "action_button" do %> | ||
| 45 | <%= icon("file-plus", library: "tabler", "aria-hidden": true) %> New page with this attachment | ||
| 46 | <% end %> | ||
| 47 | </div> | ||
| 48 | |||
| 24 | <div class="node_description">Public Path</div> | 49 | <div class="node_description">Public Path</div> |
| 25 | <div class="node_content"> | 50 | <div class="node_content"> |
| 26 | <% public_path = @asset.upload.url.sub(/\?\d+$/, "") %> | 51 | <% public_path = @asset.upload.url.sub(/\?\d+$/, "") %> |
diff --git a/app/views/nodes/new.html.erb b/app/views/nodes/new.html.erb index 805fbc9..1303e9e 100644 --- a/app/views/nodes/new.html.erb +++ b/app/views/nodes/new.html.erb | |||
| @@ -48,6 +48,24 @@ | |||
| 48 | <span class="field_hint">This preview updates as you type. The final path can still be changed afterward by editing the title (for the last segment) or moving the node to a different parent (for everything before it).</span> | 48 | <span class="field_hint">This preview updates as you type. The final path can still be changed afterward by editing the title (for the last segment) or moving the node to a different parent (for everything before it).</span> |
| 49 | </div> | 49 | </div> |
| 50 | 50 | ||
| 51 | <% if @attach_asset %> | ||
| 52 | <div class="node_description">attachment</div> | ||
| 53 | <div class="node_content"> | ||
| 54 | <ul class="thumbnail_list"> | ||
| 55 | <li> | ||
| 56 | <% if @attach_asset.has_variant?(:thumb) %> | ||
| 57 | <%= image_tag @attach_asset.upload.url(:thumb) %> | ||
| 58 | <% end %> | ||
| 59 | <%= link_to @attach_asset.name, asset_path(@attach_asset), | ||
| 60 | :target => "_blank", :rel => "noopener" %> | ||
| 61 | </li> | ||
| 62 | </ul> | ||
| 63 | <span class="field_hint">This asset will automatically be attached to the new page.</span> | ||
| 64 | <label><%= check_box_tag :asset_headline, "1" %> as the page's headline</label> | ||
| 65 | <%= hidden_field_tag :asset_id, @attach_asset.id %> | ||
| 66 | </div> | ||
| 67 | <% end %> | ||
| 68 | |||
| 51 | <div class="node_description"></div> | 69 | <div class="node_description"></div> |
| 52 | <div class="node_content"><%= submit_tag "Create" %></div> | 70 | <div class="node_content"><%= submit_tag "Create" %></div> |
| 53 | 71 | ||
diff --git a/test/controllers/nodes_controller_test.rb b/test/controllers/nodes_controller_test.rb index b8f9278..55da524 100644 --- a/test/controllers/nodes_controller_test.rb +++ b/test/controllers/nodes_controller_test.rb | |||
| @@ -409,7 +409,7 @@ class NodesControllerTest < ActionController::TestCase | |||
| 409 | 409 | ||
| 410 | get :show, params: { id: node.id } | 410 | get :show, params: { id: node.id } |
| 411 | assert_response :success | 411 | assert_response :success |
| 412 | assert_select "a", text: "add event" | 412 | assert_select "a", text: "Add event" |
| 413 | assert_select "a[href*='tag_list=open-day']" | 413 | assert_select "a[href*='tag_list=open-day']" |
| 414 | assert_select "a[href*='auto_tag_source=erfa-detail']" | 414 | assert_select "a[href*='auto_tag_source=erfa-detail']" |
| 415 | end | 415 | end |
| @@ -420,7 +420,7 @@ class NodesControllerTest < ActionController::TestCase | |||
| 420 | 420 | ||
| 421 | get :show, params: { id: node.id } | 421 | get :show, params: { id: node.id } |
| 422 | assert_response :success | 422 | assert_response :success |
| 423 | assert_select "a", text: "add event" | 423 | assert_select "a", text: "Add event" |
| 424 | assert_select "a[href*='tag_list=']", count: 0 | 424 | assert_select "a[href*='tag_list=']", count: 0 |
| 425 | end | 425 | end |
| 426 | 426 | ||
