diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-07-12 02:15:44 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-07-12 02:15:44 +0200 |
| commit | 45bf65d04d046c0ea4a1150096b2a9b846d6c0b8 (patch) | |
| tree | 24dad426779cb3b0ac59c405245aac1d28109982 | |
| parent | 13c8cb415813e90c883a44b0c0888382161de92a (diff) | |
Give the sitemap its own view, with collapse and descendant counts
Extracted from admin#index's inline table into NodesController#sitemap.
Nested <details>/<summary> per branch, one linear pass over the
existing flat [node, level] list (no added queries) -- each node's own
descendant count computed the same way, via a small stack rather than
re-walking the tree per node. Branches under updates/, club/erfas,
club/chaostreffs, and disclosure start collapsed by default
(CccConventions::SITEMAP_COLLAPSED_PATHS); any branch currently
collapsed, whether by that default or because someone just closed it,
is highlighted via a plain :not([open]) selector -- no state tracked
outside the DOM itself.
Dropped the update?-post exclusion this view used to rely on -- no
longer needed now that updates/ collapses instead of being filtered
out, so its real children (previously silently absent) now show up
correctly. admin#index's own, separate @sitemap query is unchanged;
that view has no collapse mechanism to compensate and wasn't part of
this.
| -rw-r--r-- | app/controllers/nodes_controller.rb | 23 | ||||
| -rw-r--r-- | app/helpers/nodes_helper.rb | 5 | ||||
| -rw-r--r-- | app/views/nodes/sitemap.html.erb | 32 | ||||
| -rw-r--r-- | config/routes.rb | 1 | ||||
| -rw-r--r-- | lib/ccc_conventions.rb | 1 | ||||
| -rw-r--r-- | public/stylesheets/admin.css | 40 | ||||
| -rw-r--r-- | test/controllers/nodes_controller_test.rb | 52 | ||||
| -rw-r--r-- | test/models/helpers/nodes_helper_test.rb | 11 |
8 files changed, 164 insertions, 1 deletions
diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index ede91ad..772bf4b 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb | |||
| @@ -216,6 +216,11 @@ class NodesController < ApplicationController | |||
| 216 | @nodes = nodes_matching_tags(tags) | 216 | @nodes = nodes_matching_tags(tags) |
| 217 | end | 217 | end |
| 218 | 218 | ||
| 219 | def sitemap | ||
| 220 | @sitemap = Node.root.self_and_descendants_ordered_with_level | ||
| 221 | @sitemap_descendant_counts = descendant_counts_for(@sitemap) | ||
| 222 | end | ||
| 223 | |||
| 219 | private | 224 | private |
| 220 | 225 | ||
| 221 | def slug_for(title) | 226 | def slug_for(title) |
| @@ -248,6 +253,24 @@ class NodesController < ApplicationController | |||
| 248 | end | 253 | end |
| 249 | end | 254 | end |
| 250 | 255 | ||
| 256 | def descendant_counts_for(ordered_with_level) | ||
| 257 | counts = Hash.new(0) | ||
| 258 | stack = [] # [node, level, index] | ||
| 259 | ordered_with_level.each_with_index do |(node, level), index| | ||
| 260 | while stack.any? && stack.last[1] >= level | ||
| 261 | ancestor_node, _ancestor_level, ancestor_index = stack.pop | ||
| 262 | counts[ancestor_node.id] = index - ancestor_index - 1 | ||
| 263 | end | ||
| 264 | stack << [node, level, index] | ||
| 265 | end | ||
| 266 | total = ordered_with_level.length | ||
| 267 | while stack.any? | ||
| 268 | ancestor_node, _ancestor_level, ancestor_index = stack.pop | ||
| 269 | counts[ancestor_node.id] = total - ancestor_index - 1 | ||
| 270 | end | ||
| 271 | counts | ||
| 272 | end | ||
| 273 | |||
| 251 | def nodes_matching_tags(tags) | 274 | def nodes_matching_tags(tags) |
| 252 | matching_pages = Page.tagged_with(tags, any: true).reselect(:id) | 275 | matching_pages = Page.tagged_with(tags, any: true).reselect(:id) |
| 253 | base = Node.where(head_id: matching_pages).or(Node.where(draft_id: matching_pages)) | 276 | base = Node.where(head_id: matching_pages).or(Node.where(draft_id: matching_pages)) |
diff --git a/app/helpers/nodes_helper.rb b/app/helpers/nodes_helper.rb index a89f879..1268b63 100644 --- a/app/helpers/nodes_helper.rb +++ b/app/helpers/nodes_helper.rb | |||
| @@ -12,7 +12,6 @@ module NodesHelper | |||
| 12 | end | 12 | end |
| 13 | end | 13 | end |
| 14 | 14 | ||
| 15 | |||
| 16 | def truncated_title_for_node node | 15 | def truncated_title_for_node node |
| 17 | if (title = title_for_node node) && title.size > 20 | 16 | if (title = title_for_node node) && title.size > 20 |
| 18 | "<span title='#{title}'>#{truncate(title, 40)}</span>" | 17 | "<span title='#{title}'>#{truncate(title, 40)}</span>" |
| @@ -63,4 +62,8 @@ module NodesHelper | |||
| 63 | path = node.unique_path | 62 | path = node.unique_path |
| 64 | CccConventions::NODE_KINDS.select { |_, config| config[:parent_match]&.call(path) } | 63 | CccConventions::NODE_KINDS.select { |_, config| config[:parent_match]&.call(path) } |
| 65 | end | 64 | end |
| 65 | |||
| 66 | def sitemap_node_open?(node) | ||
| 67 | !CccConventions::SITEMAP_COLLAPSED_PATHS.include?(node.unique_name) | ||
| 68 | end | ||
| 66 | end | 69 | end |
diff --git a/app/views/nodes/sitemap.html.erb b/app/views/nodes/sitemap.html.erb new file mode 100644 index 0000000..a799c17 --- /dev/null +++ b/app/views/nodes/sitemap.html.erb | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | <h1>Sitemap</h1> | ||
| 2 | |||
| 3 | <div id="sitemap"> | ||
| 4 | <% | ||
| 5 | open_details = [] # levels with a currently-open <details> | ||
| 6 | %> | ||
| 7 | <% @sitemap.each_with_index do |(node, level), index| %> | ||
| 8 | <% while open_details.any? && open_details.last >= level %> | ||
| 9 | </details> | ||
| 10 | <% open_details.pop %> | ||
| 11 | <% end %> | ||
| 12 | |||
| 13 | <div class="sitemap_node"> | ||
| 14 | <h4><%= link_to title_for_node(node), node_path(node) %></h4> | ||
| 15 | <span class="field_hint"><%= link_to_path("#{node.unique_name} ↗", node.unique_name) %></span> | ||
| 16 | <p class="sitemap_node_actions"> | ||
| 17 | <%= link_to 'Show', node_path(node) %> | ||
| 18 | <%= link_to 'Create Child', new_node_path(:parent_id => node.id) %> | ||
| 19 | </p> | ||
| 20 | </div> | ||
| 21 | |||
| 22 | <% next_level = @sitemap[index + 1]&.last %> | ||
| 23 | <% if next_level && next_level > level %> | ||
| 24 | <details<%= ' open' if sitemap_node_open?(node) %>> | ||
| 25 | <summary> | ||
| 26 | <%= pluralize(@sitemap_descendant_counts[node.id], 'descendant', 'descendants') %> | ||
| 27 | </summary> | ||
| 28 | <% open_details.push(level) %> | ||
| 29 | <% end %> | ||
| 30 | <% end %> | ||
| 31 | <% open_details.length.times { %></details><% } %> | ||
| 32 | </div> | ||
diff --git a/config/routes.rb b/config/routes.rb index 2c165d2..bb34bd8 100644 --- a/config/routes.rb +++ b/config/routes.rb | |||
| @@ -43,6 +43,7 @@ Cccms::Application.routes.draw do | |||
| 43 | get :recent | 43 | get :recent |
| 44 | get :mine | 44 | get :mine |
| 45 | get :chapters | 45 | get :chapters |
| 46 | get :sitemap | ||
| 46 | end | 47 | end |
| 47 | 48 | ||
| 48 | member do | 49 | member do |
diff --git a/lib/ccc_conventions.rb b/lib/ccc_conventions.rb index b420452..352dd3c 100644 --- a/lib/ccc_conventions.rb +++ b/lib/ccc_conventions.rb | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | module CccConventions | 1 | module CccConventions |
| 2 | ERFA_PARENT_NAME = "club/erfas" | 2 | ERFA_PARENT_NAME = "club/erfas" |
| 3 | CHAOSTREFF_PARENT_NAME = "club/chaostreffs" | 3 | CHAOSTREFF_PARENT_NAME = "club/chaostreffs" |
| 4 | SITEMAP_COLLAPSED_PATHS = %w[updates club/erfas club/chaostreffs disclosure].freeze | ||
| 4 | 5 | ||
| 5 | NODE_KINDS = { | 6 | NODE_KINDS = { |
| 6 | "top_level" => { | 7 | "top_level" => { |
diff --git a/public/stylesheets/admin.css b/public/stylesheets/admin.css index aa8b288..5c1e489 100644 --- a/public/stylesheets/admin.css +++ b/public/stylesheets/admin.css | |||
| @@ -25,6 +25,7 @@ a:hover { | |||
| 25 | body content, node listing tables, the child-creation shortcuts on | 25 | body content, node listing tables, the child-creation shortcuts on |
| 26 | nodes#show, and the dashboard draft list's Show/Revisions links. */ | 26 | nodes#show, and the dashboard draft list's Show/Revisions links. */ |
| 27 | #page_editor a, | 27 | #page_editor a, |
| 28 | #sitemap a, | ||
| 28 | table.node_table a, | 29 | table.node_table a, |
| 29 | table.assets_table a, | 30 | table.assets_table a, |
| 30 | table.events_table a, | 31 | table.events_table a, |
| @@ -104,6 +105,11 @@ input[type=radio] { | |||
| 104 | width: 100%; | 105 | width: 100%; |
| 105 | box-sizing: border-box; | 106 | box-sizing: border-box; |
| 106 | } | 107 | } |
| 108 | |||
| 109 | #sitemap details details { | ||
| 110 | margin-left: 0.75rem; | ||
| 111 | padding-left: 0.5rem; | ||
| 112 | } | ||
| 107 | } | 113 | } |
| 108 | 114 | ||
| 109 | #metadata, | 115 | #metadata, |
| @@ -773,6 +779,40 @@ form.button_to button[type="submit"] { | |||
| 773 | white-space: nowrap; | 779 | white-space: nowrap; |
| 774 | } | 780 | } |
| 775 | 781 | ||
| 782 | .sitemap_node { | ||
| 783 | padding-bottom: 0.5rem; | ||
| 784 | margin-bottom: 0.5rem; | ||
| 785 | border-bottom: 1px solid #ececec; | ||
| 786 | } | ||
| 787 | |||
| 788 | .sitemap_node h4 { | ||
| 789 | margin: 0; | ||
| 790 | } | ||
| 791 | |||
| 792 | .sitemap_node .field_hint { | ||
| 793 | display: block; | ||
| 794 | margin: 2px 0 0; | ||
| 795 | } | ||
| 796 | |||
| 797 | .sitemap_node p { | ||
| 798 | margin: 2px 0 0; | ||
| 799 | } | ||
| 800 | |||
| 801 | #sitemap details details { | ||
| 802 | margin-left: 1.5rem; | ||
| 803 | border-left: 1px solid #e8e8e8; | ||
| 804 | padding-left: 0.75rem; | ||
| 805 | } | ||
| 806 | |||
| 807 | #sitemap summary { | ||
| 808 | padding: 4px 0; | ||
| 809 | } | ||
| 810 | |||
| 811 | #sitemap details:not([open]) > summary { | ||
| 812 | color: #ff9600; | ||
| 813 | font-weight: bold; | ||
| 814 | } | ||
| 815 | |||
| 776 | /* ============================================================ | 816 | /* ============================================================ |
| 777 | Page editor / metadata forms | 817 | Page editor / metadata forms |
| 778 | ============================================================ */ | 818 | ============================================================ */ |
diff --git a/test/controllers/nodes_controller_test.rb b/test/controllers/nodes_controller_test.rb index 05cb195..b43d2de 100644 --- a/test/controllers/nodes_controller_test.rb +++ b/test/controllers/nodes_controller_test.rb | |||
| @@ -575,4 +575,56 @@ class NodesControllerTest < ActionController::TestCase | |||
| 575 | get :chapters | 575 | get :chapters |
| 576 | assert_select "h1", "Chapters" | 576 | assert_select "h1", "Chapters" |
| 577 | end | 577 | end |
| 578 | |||
| 579 | test "sitemap collapses configured paths but leaves others open" do | ||
| 580 | club = Node.root.children.create!(:slug => "club") | ||
| 581 | erfas = club.children.create!(:slug => "erfas") | ||
| 582 | erfas.children.create!(:slug => "one_chapter") | ||
| 583 | other = Node.root.children.create!(:slug => "sitemap_controller_open_test") | ||
| 584 | other.children.create!(:slug => "sitemap_controller_open_child") | ||
| 585 | |||
| 586 | login_as :quentin | ||
| 587 | get :sitemap | ||
| 588 | assert_response :success | ||
| 589 | |||
| 590 | doc = Nokogiri::HTML::DocumentFragment.parse(response.body) | ||
| 591 | |||
| 592 | erfas_node_div = doc.css('.sitemap_node').find { |div| div.at_css('.field_hint')&.text&.include?(erfas.unique_name) } | ||
| 593 | other_node_div = doc.css('.sitemap_node').find { |div| div.at_css('.field_hint')&.text&.include?(other.unique_name) } | ||
| 594 | |||
| 595 | erfas_details = erfas_node_div.next_element | ||
| 596 | other_details = other_node_div.next_element | ||
| 597 | |||
| 598 | assert_equal 'details', erfas_details.name | ||
| 599 | assert_equal 'details', other_details.name | ||
| 600 | assert_not erfas_details.key?('open') | ||
| 601 | assert other_details.key?('open') | ||
| 602 | end | ||
| 603 | |||
| 604 | test "sitemap shows how many descendants a collapsed branch is hiding" do | ||
| 605 | club = Node.root.children.create!(:slug => "club") | ||
| 606 | erfas = club.children.create!(:slug => "erfas") | ||
| 607 | erfas.children.create!(:slug => "one_chapter") | ||
| 608 | erfas.children.create!(:slug => "another_chapter") | ||
| 609 | |||
| 610 | login_as :quentin | ||
| 611 | get :sitemap | ||
| 612 | assert_response :success | ||
| 613 | |||
| 614 | doc = Nokogiri::HTML::DocumentFragment.parse(response.body) | ||
| 615 | erfas_node_div = doc.css('.sitemap_node').find { |div| div.at_css('.field_hint')&.text&.include?(erfas.unique_name) } | ||
| 616 | erfas_details = erfas_node_div.next_element | ||
| 617 | |||
| 618 | assert_equal 'details', erfas_details.name | ||
| 619 | assert_match "2 descendants", erfas_details.at_css('summary').text | ||
| 620 | end | ||
| 621 | |||
| 622 | test "sitemap shows Show and Create Child, not Revisions" do | ||
| 623 | node = Node.root.children.create!(:slug => "sitemap_actions_test") | ||
| 624 | login_as :quentin | ||
| 625 | get :sitemap | ||
| 626 | assert_select ".sitemap_node_actions", :text => /Show/ | ||
| 627 | assert_select ".sitemap_node_actions", :text => /Create Child/ | ||
| 628 | assert_select ".sitemap_node_actions", :text => /Revisions/, :count => 0 | ||
| 629 | end | ||
| 578 | end | 630 | end |
diff --git a/test/models/helpers/nodes_helper_test.rb b/test/models/helpers/nodes_helper_test.rb index 5d91a88..5ab924f 100644 --- a/test/models/helpers/nodes_helper_test.rb +++ b/test/models/helpers/nodes_helper_test.rb | |||
| @@ -22,4 +22,15 @@ class NodesHelperTest < ActionView::TestCase | |||
| 22 | page = FakePage.new([]) | 22 | page = FakePage.new([]) |
| 23 | assert_nil default_event_tag_list(page) | 23 | assert_nil default_event_tag_list(page) |
| 24 | end | 24 | end |
| 25 | |||
| 26 | test "sitemap_node_open? is false for a configured collapsed path" do | ||
| 27 | club = Node.root.children.create!(:slug => "club") | ||
| 28 | erfas = club.children.create!(:slug => "erfas") | ||
| 29 | assert_equal false, sitemap_node_open?(erfas) | ||
| 30 | end | ||
| 31 | |||
| 32 | test "sitemap_node_open? is true for anything not configured as collapsed" do | ||
| 33 | node = Node.root.children.create!(:slug => "sitemap_open_test") | ||
| 34 | assert_equal true, sitemap_node_open?(node) | ||
| 35 | end | ||
| 25 | end | 36 | end |
