Skip to content

Commit

Permalink
Add several layout hooks to provide extra content (#2574)
Browse files Browse the repository at this point in the history
This commit allows us to add content in some additional places, from
around the header to around the main body, which means users can more
easily customise their templates without needing to override the whole
file.
  • Loading branch information
goosys authored Sep 30, 2024
1 parent 8ce0514 commit 0829202
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 66 deletions.
4 changes: 4 additions & 0 deletions app/views/administrate/application/_index_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<%= content_for(:title) %>
</h1>

<%= content_for(:header_middle) %>

<% if show_search_bar %>
<%= render(
"search",
Expand All @@ -25,4 +27,6 @@
class: "button",
) if accessible_action?(new_resource, :new) %>
</div>

<%= content_for(:header_last) %>
</header>
18 changes: 15 additions & 3 deletions app/views/administrate/application/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,27 @@ It displays a header, and renders the `_form` partial to do the heavy lifting.
<%= content_for(:title) %>
</h1>

<%= content_for(:header_middle) %>

<div>
<%= link_to(
t("administrate.actions.show_resource", name: page.page_title),
[namespace, page.resource],
class: "button",
) if accessible_action?(page.resource, :show) %>
</div>

<%= content_for(:header_last) %>
</header>

<section class="main-content__body">
<%= render "form", page: page %>
</section>
<%= content_for(:before_main) %>

<% if content_for?(:main) %>
<%= content_for(:main) %>
<% else %>
<section class="main-content__body">
<%= render "form", page: page %>
</section>
<% end %>

<%= content_for(:after_main) %>
30 changes: 19 additions & 11 deletions app/views/administrate/application/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ It renders the `_table` partial to display details about the resources.
)
%>

<section class="main-content__body main-content__body--flush">
<%= render(
"collection",
collection_presenter: page,
collection_field_name: resource_name,
page: page,
resources: resources,
table_title: "page-title"
) %>
<%= content_for(:before_main) %>

<% if content_for?(:main) %>
<%= content_for(:main) %>
<% else %>
<section class="main-content__body main-content__body--flush">
<%= render(
"collection",
collection_presenter: page,
collection_field_name: resource_name,
page: page,
resources: resources,
table_title: "page-title"
) %>

<%= render("pagination", resources: resources) %>
</section>
<% end %>

<%= render("pagination", resources: resources) %>
</section>
<%= content_for(:after_main) %>
18 changes: 15 additions & 3 deletions app/views/administrate/application/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ to do the heavy lifting.
<%= content_for(:title) %>
</h1>

<%= content_for(:header_middle) %>

<div>
<%= link_to t("administrate.actions.back"), :back, class: "button" %>
</div>

<%= content_for(:header_last) %>
</header>

<section class="main-content__body">
<%= render "form", page: page %>
</section>
<%= content_for(:before_main) %>

<% if content_for?(:main) %>
<%= content_for(:main) %>
<% else %>
<section class="main-content__body">
<%= render "form", page: page %>
</section>
<% end %>

<%= content_for(:after_main) %>
54 changes: 33 additions & 21 deletions app/views/administrate/application/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ as well as a link to its edit page.
<%= content_for(:title) %>
</h1>

<%= content_for(:header_middle) %>

<div>
<%= link_to(
t("administrate.actions.edit_resource", name: page.page_title),
Expand All @@ -38,28 +40,38 @@ as well as a link to its edit page.
data: { turbo_confirm: t("administrate.actions.confirm") }
) if accessible_action?(page.resource, :destroy) %>
</div>

<%= content_for(:header_last) %>
</header>

<section class="main-content__body">
<dl>
<% page.attributes.each do |title, attributes| %>
<fieldset class="<%= "field-unit--nested" if title.present? %>">
<% if title.present? %>
<legend><%= t "helpers.label.#{page.resource_name}.#{title}", default: title %></legend>
<% end %>
<%= content_for(:before_main) %>

<% if content_for?(:main) %>
<%= content_for(:main) %>
<% else %>
<section class="main-content__body">
<dl>
<% page.attributes.each do |title, attributes| %>
<fieldset class="<%= "field-unit--nested" if title.present? %>">
<% if title.present? %>
<legend><%= t "helpers.label.#{page.resource_name}.#{title}", default: title %></legend>
<% end %>

<% attributes.each do |attribute| %>
<dt class="attribute-label" id="<%= attribute.name %>">
<%= t(
"helpers.label.#{resource_name}.#{attribute.name}",
default: page.resource.class.human_attribute_name(attribute.name),
) %>
</dt>

<% attributes.each do |attribute| %>
<dt class="attribute-label" id="<%= attribute.name %>">
<%= t(
"helpers.label.#{resource_name}.#{attribute.name}",
default: page.resource.class.human_attribute_name(attribute.name),
) %>
</dt>
<dd class="attribute-data attribute-data--<%=attribute.html_class%>"
><%= render_field attribute, page: page %></dd>
<% end %>
</fieldset>
<% end %>
</dl>
</section>
<% end %>

<dd class="attribute-data attribute-data--<%=attribute.html_class%>"
><%= render_field attribute, page: page %></dd>
<% end %>
</fieldset>
<% end %>
</dl>
</section>
<%= content_for(:after_main) %>
25 changes: 25 additions & 0 deletions docs/customizing_page_views.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,28 @@ rails generate administrate:views:layout
# It only generates the sidebar partial
# -> app/views/admin/application/_navigation.html.erb
```

## Customizing for a specific layout

You can use several hook points to add elements to specific layouts or specific pages:

* header_middle
* header_last
* before_main
* main
* after_main

For example, you can add a button in the middle of the header as follows:

```eruby
<%# app/views/admin/customers/_index_header.html.erb %>
<% content_for(:header_middle) do %>
<div>
You are logged in as <em><%= pundit_user.name %></em>.
<%= link_to("Become the Admin", become_admin_customer_path("admin")) unless pundit_user.admin? %>
</div>
<% end %>
<%= render template: 'administrate/application/_index_header', locals: local_assigns %>
```
31 changes: 3 additions & 28 deletions spec/example_app/app/views/admin/customers/_index_header.html.erb
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
<% content_for(:title) do %>
<%= display_resource_name(page.resource_name) %>
<% end %>

<header class="main-content__header">
<h1 class="main-content__page-title" id="page-title">
<%= content_for(:title) %>
</h1>

<% content_for(:header_middle) do %>
<div>
You are logged in as <em><%= pundit_user.name %></em>.
<%= link_to("Become the Admin", become_admin_customer_path("admin")) unless pundit_user.admin? %>
</div>
<% end %>

<% if show_search_bar %>
<%= render(
"search",
search_term: search_term,
resource_name: display_resource_name(page.resource_name)
) %>
<% end %>

<div>
<%= link_to(
t(
"administrate.actions.new_resource",
name: display_resource_name(page.resource_name, singular: true).downcase
),
[:new, namespace, page.resource_path.to_sym],
class: "button",
) if accessible_action?(new_resource, :new) %>
</div>
</header>
<%= render template: 'administrate/application/_index_header', locals: local_assigns %>

0 comments on commit 0829202

Please sign in to comment.