You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Imagine a stock portfolio app where a user has_many companies. Say I want to list the value of a user's portfolio broken down by company.
I could add a method portfolio_value(user) to the company decorator, and do
<% user.companies.each do |company| %>
<li><%= company.name %>: <%= company.portfolio_value(user) %>
<% end %>
But given I accessed the company via user.companies, the user could already be available in the context of the company decorator, and I could do simply:
<% user.companies.each do |company| %>
<li><%= company.name %>: <%= company.portfolio_value %>
<% end %>
In my experience this is a common pattern in many apps.
A simple monkey patch to achieve this is:
class Draper::DecoratedAssociation
def call
decorate unless defined?(@decorated)
@decorated.context[:owner] = @owner
@decorated
end
end
I've been using this patch and found it very useful. If the maintainers think this would make a good enhancement to Draper I would be happy to submit a PR.
The text was updated successfully, but these errors were encountered:
Imagine a stock portfolio app where a user has_many companies. Say I want to list the value of a user's portfolio broken down by company.
I could add a method
portfolio_value(user)
to the company decorator, and doBut given I accessed the company via
user.companies
, the user could already be available in the context of the company decorator, and I could do simply:In my experience this is a common pattern in many apps.
A simple monkey patch to achieve this is:
I've been using this patch and found it very useful. If the maintainers think this would make a good enhancement to Draper I would be happy to submit a PR.
The text was updated successfully, but these errors were encountered: