Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize decorators in the same namespace as the collection decorator #545

Closed
ramontayag opened this issue May 26, 2013 · 3 comments
Closed

Comments

@ramontayag
Copy link

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
}
@ramontayag
Copy link
Author

Perhaps a better location for this would be in the initializer? It will set the @decorator_class variable with the item_decorator_class like above.

@haines
Copy link
Contributor

haines commented May 26, 2013

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:

class Admin::PostsDecorator
  def decorator_class
    Admin::PostDecorator
  end
end

@ramontayag
Copy link
Author

I see. It pays to ask the maintainer/s first. Thanks for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants