Skip to content

Adding decorate method to ActiveModel instances and collections of instances #296

Closed
barelyknown opened this Issue Oct 6, 2012 · 8 comments

2 participants

@barelyknown

If others are interested, I'm thinking about writing a few new ways to decorate objects using less verbose syntax. I'm particularly interested in the following to clean up the way controllers look now.

Post.last.decorate
Post.all.decorate

Any interest?

@steveklabnik
drapergem member

That wouldn't be too bad, I guess. How will you know what to decorate, though?

class MyPost < Draper::Base
  decorates :post
end

class YourPost < Draper::Base
  decorates :post
end

Post.all.decorate # what does this return?
@barelyknown

So far, I've stuck to 1:1 relationship between decorator and model, and also stuck to the naming convention ModelDecorator. That convention is super easy to handle :)

Maybe this is the right direction for the ActiveModel instance:

def decorate(decorator="#{self.class.model_name}Decorator".constantize)
  decorator.decorate(self)
end
@steveklabnik
drapergem member

That convention is super easy to handle :)

I specifically want to enable N models - 1 decorator as a use case in 2.0, so this will end up being at odds with Draper itself... hm.

I do want to make something like that work, for example, see #228.

@barelyknown

You could still specify at decoration time which decorator to use though, right?

object.decorate(PolyDecorator)
@steveklabnik
drapergem member
@barelyknown
@steveklabnik
drapergem member

That seems like a good tradeoff. I will accept a PR for that. :)

@barelyknown

Sounds good. I'll write it when I have a little bit of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.