I am working with a collection of objects that have a growing number of states. Each state requires a different layout/html wrapper. To reduce the need for conditional statements within the partial or template, I thought it might be nice if the layout option could be set dynamically using a proc or better yet a method.
An overly simplistic example would be something like this…
<%# app/views/users/_public_layout.html.erb %>
<h2><%= user.name %></h2>
<a href="users/profile">
<%= yield %>
</a>
<%# app/views/users/_private_layout.html.erb %>
<h2>User is private</h2>
<section>
<%= yield %>
</section>
<%# app/views/users/index.html.erb %>
<%= render partial: "user", collection: users, layout: -> user { user.private? ? "private_layout" : "public_layout" } %>
``
I imagine there are already nice patterns to handle this sort of case but I thought I’d throw the idea out there! Thanks!