Decorator method call from assosiation
Closed this issue · 8 comments
It looks like when you have a situation:
class Order < ActiveRecord::Base
has_one :coupon
end
class Coupon < ActiveRecord::Base
belongs_to :order
end
module CouponDecorator
def type_description
#some logic here
end
end
Now when in the view I call
<%= order.coupon.type_description %>
I've got NoMethod exception, but if I call in the another view
<%= coupon.type_description %>
it works fine.
The only one workaround I found for now is to create a method in Coupon model with the same name as decorator
def type_description
ActiveDecorator::Decorator.instance.decorate(self).type_description
end
and now it will work with the calls such as
<%= order.coupon.type_description %>
but it does not looks good to me, is there any better solution?
I have the same problem.
Same problem here...
Same problem...
Another quick workaround. Just make a helper method:
def decorate(var)
ActiveDecorator::Decorator.instance.decorate var
end
And then call it in your view when needed
- for coupon in @order.coupons
- decorate coupon
= coupon.type_description
Same problem
The docs suggest moving the coupons to a partial, passing order.coupons as local. Looks reasonable, as it also avoids a Law Of Demeter issue.
The docs suggest moving the coupons to a partial, passing order.coupons as local. Looks reasonable, as it also avoids a Law Of Demeter issue.
https://github.com/amatsuda/active_decorator#decorating-associated-objects