Skip to content

Commit 237adad

Browse files
goosysnickcharlton
authored andcommitted
Add a guide for using Rails view variants
This adds a guide and two examples of using Rails' view variant support. Closes thoughtbot#2578 https://guides.rubyonrails.org/layouts_and_rendering.html#the-variants-option
1 parent 936da16 commit 237adad

8 files changed

+78
-4
lines changed

docs/guides.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ title: Guides
55
- [Hiding Dashboards from the Sidebar](./guides/hiding_dashboards_from_sidebar)
66
- [Customising the search](./guides/customising_search)
77
- [Scoping HasMany Relations](./guides/scoping_has_many_relations.md)
8+
- [Switching templates with view variants](./guides/switching_templates_with_view_variants.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Switching templates with view variants
3+
---
4+
5+
You can switch to different templates using Rails' support for [view
6+
variants][], which can be used to override the template used inside the
7+
controller.
8+
9+
By setting the `request.variant` option to any value (in this case, `:admin`)
10+
at any point in any controller, you can add a new variant to Rails' template
11+
lookup tree (in this case, `.html+admin.erb`).
12+
13+
For example, to add a button to become the admin to view certain functionality:
14+
15+
```ruby
16+
class CustomersController < Admin::ApplicationController
17+
before_action :with_variant, only: %i[index]
18+
19+
private
20+
21+
def with_variant
22+
if @current_user.admin?
23+
request.variant = :admin
24+
end
25+
end
26+
end
27+
```
28+
29+
```erb
30+
<!-- app/views/admin/customers/_index_header.html.erb -->
31+
<p class="identity__banner">
32+
You are logged in as <em><%= pundit_user.name %></em>.
33+
<%= link_to("Become the Admin", become_admin_customer_path("admin"),
34+
class: "identity__become-action")%>
35+
</p>
36+
```
37+
38+
```erb
39+
<!-- app/views/admin/customers/_index_header.html+admin.erb -->
40+
<p class="identity__banner identity__banner--admin">
41+
You are logged in as <em><%= pundit_user.name %></em>.
42+
</p>
43+
```
44+
45+
[view variants]: https://guides.rubyonrails.org/layouts_and_rendering.html#the-variants-option

spec/example_app/app/controllers/admin/customers_controller.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Admin
22
class CustomersController < Admin::ApplicationController
3+
before_action :with_variant, only: %i[index]
4+
35
def become
46
user_id = params[:id]
57
if user_id == "admin"
@@ -15,5 +17,11 @@ def become
1517
def scoped_resource
1618
Customer.where(hidden: false)
1719
end
20+
21+
def with_variant
22+
if @current_user.admin?
23+
request.variant = :admin
24+
end
25+
end
1826
end
1927
end
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
module Admin
22
class StatsController < Admin::ApplicationController
3+
before_action :with_variant, only: %i[index]
4+
35
def index
46
@stats = {
57
customer_count: Customer.count,
68
order_count: Order.count
79
}
810
end
11+
12+
private
13+
14+
def with_variant
15+
if @current_user.admin?
16+
request.variant = :admin
17+
end
18+
end
919
end
1020
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<% content_for(:header_middle) do %>
2+
<p class="identity__banner identity__banner--admin">You are logged in as <em><%= pundit_user.name %></em>.</p>
3+
<% end %>
4+
5+
<%= render template: 'administrate/application/_index_header', locals: local_assigns %>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% content_for(:header_middle) do %>
2-
<p class="identity__banner<%= " identity__banner--admin" if pundit_user.admin? %>">You are logged in as <em><%= pundit_user.name %></em>. <%= link_to("Become the Admin", become_admin_customer_path("admin"), class: "identity__become-action") unless pundit_user.admin? %></p>
2+
<p class="identity__banner">You are logged in as <em><%= pundit_user.name %></em>. <%= link_to("Become the Admin", become_admin_customer_path("admin"), class: "identity__become-action")%></p>
33
<% end %>
44

55
<%= render template: 'administrate/application/_index_header', locals: local_assigns %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div style="padding: 20px">
2+
<h1>Stats</h1>
3+
<br>
4+
<p><b>Total Customers:</b> <%= @stats[:customer_count] %></h1>
5+
<br>
6+
<p><b>Total Orders:</b> <%= @stats[:order_count] %></h1>
7+
</div>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<div style="padding: 20px">
22
<h1>Stats</h1>
33
<br>
4-
<p><b>Total Customers:</b> <%= @stats[:customer_count] %></h1>
5-
<br>
6-
<p><b>Total Orders:</b> <%= @stats[:order_count] %></h1>
4+
<p>Stats are only visible to Admin. <%= link_to("Become the Admin", become_admin_customer_path("admin"), class: "identity__become-action") %></p>
75
</div>

0 commit comments

Comments
 (0)