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
I have different decorators for the same objects, depending on what namespace the user is in. For example, in /admin/posts, I use the Admin::PostsController. The index action of that controller uses the Admin::PostsDecorator. When I iterate through the items of the decorated collection, however, they are decorated by the ::PostDecorator, not the Admin::PostDecorator, which is what I would expect.
I can make the change if this is worth merging. Please advise me if this is not the best approach:
In Draper::CollectionDecorator#item_decorator, replace -> (item, options) { item.decorate(options) } with something like:
a = -> (item, options) {
item_decorator_class_name = # find it here based on self.class.name is. For example, if self.class.name is Admin::PostsDecorator, this should return Admin::PostDecorator
item_decorator_class = if Object.const_defined?(item_decorator_class_name)
item_decorator_class_name.constantize
end
if item_decorator_class
item_decorator_class.decorate(item, options)
else
item.decorate(options)
end
}
The text was updated successfully, but these errors were encountered:
Hey, yes, this would be a good feature. The way you suggest is actually roughly how it used to work (a PostsDecorator used a PostDecorator on its items) but we stopped doing that in order to support STI. I think the better approach would be to continue to infer the decorators (like using item.decorate) but to make sure that the inferred decorator is in the right namespace.
I'm actually working on a closely related feature (reimplementing #480) so I will make sure that this works too.
In the meantime as a workaround, you can just override decorator_class:
I have different decorators for the same objects, depending on what namespace the user is in. For example, in
/admin/posts
, I use theAdmin::PostsController
. The index action of that controller uses theAdmin::PostsDecorator
. When I iterate through the items of the decorated collection, however, they are decorated by the::PostDecorator
, not theAdmin::PostDecorator
, which is what I would expect.I can make the change if this is worth merging. Please advise me if this is not the best approach:
In
Draper::CollectionDecorator#item_decorator
, replace-> (item, options) { item.decorate(options) }
with something like:The text was updated successfully, but these errors were encountered: