From 8b6d3c76451d4ece5fd829d0e2d96a00aa19122d Mon Sep 17 00:00:00 2001 From: erdgeist Date: Wed, 22 Jul 2026 20:44:34 +0200 Subject: Add attached nodes subsection in assets#show and thumbnail in nodes#create --- app/controllers/nodes_controller.rb | 10 ++++++++++ app/models/asset.rb | 9 +++++++++ app/views/assets/show.html.erb | 25 +++++++++++++++++++++++++ app/views/nodes/new.html.erb | 18 ++++++++++++++++++ 4 files changed, 62 insertions(+) (limited to 'app') 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 @node = Node.new node_params @selected_kind = CccConventions::NODE_KINDS.key?(params[:kind]) ? params[:kind] : "generic" @parent = Node.find(params[:parent_id]) if params.has_key?(:parent_id) + @attach_asset = Asset.find(params[:asset_id]) if params.has_key?(:asset_id) end def create @@ -51,10 +52,19 @@ class NodesController < ApplicationController :action => "create", :title => params[:title], :path => @node.unique_name) + if params[:asset_id].present? && (asset = Asset.find(params[:asset_id])) + result = @node.attach_asset!(asset, :user => current_user, + :headline => params[:asset_headline].present?) + flash[:notice] = "Page created with “#{asset.name}” attached." + flash[:notice] += " It is the page's headline." if result[:headline] == :set + flash[:error] = "This asset type cannot be a headline." if result[:headline] == :not_eligible + end + redirect_to(edit_node_path(@node)) else @selected_kind = CccConventions::NODE_KINDS.key?(params[:kind]) ? params[:kind] : "generic" @parent = Node.find(params[:parent_id]) if params.has_key?(:parent_id) + @attach_asset = Asset.find(params[:asset_id]) if params.has_key?(:asset_id) render :new end 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 def show_credit? image? && has_credit? end + + # Nodes whose current lifecycle rows (head, draft or autosave) carry + # this asset. Historical revisions also hold RelatedAsset rows; they + # are excluded by construction here, since nothing points at them. + def attached_nodes + page_ids = related_assets.select(:page_id) + Node.where("head_id IN (:ids) OR draft_id IN (:ids) OR autosave_id IN (:ids)", + :ids => page_ids).distinct + end 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 @@
<%= image_tag @asset.upload.url(:medium), style: "max-width: 300px; max-height: 300px;" %>
<% end %> +
Attached to
+
+ <% nodes = @asset.attached_nodes %> + <% if nodes.any? %> + + <% else %> + — not attached to any page — + <% end %> + <%= link_to new_node_path(:asset_id => @asset.id), :class => "action_button" do %> + <%= icon("file-plus", library: "tabler", "aria-hidden": true) %> New page with this attachment + <% end %> +
+
Public Path
<% 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 @@ 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).
+ <% if @attach_asset %> +
attachment
+
+ + This asset will automatically be attached to the new page. + + <%= hidden_field_tag :asset_id, @attach_asset.id %> +
+ <% end %> +
<%= submit_tag "Create" %>
-- cgit v1.3