Partials should have an easier time delegating to other partials.
Closed this issue · 0 comments
andrewculver commented
Given a app/views/account/shared/menu/_item.html.erb
like so:
<% yield p = np %>
<% method ||= nil %>
<% active ||= nil %>
<%= render "themes/light/menu/item", label: label, method: active do |dp| %>
<% dp.content_for :icon, p.content_for :icon %>
<% end %>
Note the repetition of content_for
for and :icon
on that one line. Also, this is only for one named content area. If there were more, the duplication would be even worse. To DRY-up the latter problem, I would probably:
<% [:icon, ...].each do |name| %>
<% dp.content_for :icon, p.content_for :icon %>
<% end %>
But this is still both ugly as sin and not obvious to someone reading the code what is happening. For example, it's easy to miss that there is a p
view context and a dp
view context (short for "delegate partial").
I think some good alternatives to this would be something like one of the following:
<% p.delegate_to dp %>
<% dp.delegate_from p %>
With the final result looking something like this:
<% yield p = np %>
<% method ||= nil %>
<% active ||= nil %>
<%= render "themes/light/menu/item", label: label, method: active do |dp| %>
<% d.delegate_to dp %>
<% end %>