As I was looking to implement the new Spaces feature (see forem/forem#16842), I began looking at how to adjust the admin menu. I found the Menu and AdminMenu. I wanted to add the "spaces" item under the `:content_manager` scope. Looking at the implementation, I saw I would need some additional branching logic. To get an understanding of the implementation, I chose factor towards classes. This refactor does a few things: 1) Clarifies a method name (e.g. "children?" becomes "has_multiple_children?") 2) Adds some tests of the menu implementation. 3) Removes deeply nested hashs in favor of first class objects. 4) Removes a complex method for determining menu visibility, instead favoring a method that either takes a boolean OR a lambda. There are further refactors to consider, especially in regards to the helper methods and some of the coercion of strings into different formats. But this refactor gets me the part I most am interested in: Making it easier to specify a menu item as visible or not.
41 lines
1.6 KiB
Text
41 lines
1.6 KiB
Text
<nav aria-label="Admin">
|
|
<ul class="m-0">
|
|
<li>
|
|
<a class="c-link c-link--block c-link--icon-left" href="<%= admin_path %>" aria-current="<%= "page" if controller.controller_name == "overview" %>">
|
|
<%= crayons_icon_tag("stack-line", class: "c-link__icon") %>
|
|
Overview
|
|
</a>
|
|
</li>
|
|
|
|
<% menu_items.each do |group_name, group| %>
|
|
<li>
|
|
<a
|
|
class="c-link c-link--block c-link--icon-left <%= "c-link--current" if current?(request, group, group_name) %>"
|
|
aria-current="<%= "page" if current?(request, group, group_name) && !group.has_multiple_children? %>"
|
|
href="<%= nav_path(group, group_name) %>">
|
|
<%= crayons_icon_tag(group.svg, class: "c-link__icon", aria_hidden: true) %>
|
|
<%= display_name(group_name) %>
|
|
</a>
|
|
<% if group.has_multiple_children? %>
|
|
<ul class="ml-6 mb-1 <%= current?(request, group, group_name) ? "" : "hidden" %>">
|
|
<% group.children.each do |item| %>
|
|
<% if item.visible %>
|
|
<li>
|
|
<a
|
|
class="c-link c-link--block"
|
|
href="<%= admin_path %>/<%= group_name %>/<%= item.controller %>"
|
|
<% if deduced_controller(request) == item.controller.to_s %>
|
|
style="--bg: transparent"
|
|
aria-current="page"
|
|
<% end %>>
|
|
<%= item.name.to_s.titleize %>
|
|
</a>
|
|
</li>
|
|
<% end %>
|
|
<% end %>
|
|
</ul>
|
|
<% end %>
|
|
</li>
|
|
<% end %>
|
|
</ul>
|
|
</nav>
|