[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

Collection decoration should use correct decorator with STI #468

Closed
emarthinsen opened this issue Feb 12, 2013 · 2 comments · Fixed by #470
Closed

Collection decoration should use correct decorator with STI #468

emarthinsen opened this issue Feb 12, 2013 · 2 comments · Fixed by #470

Comments

@emarthinsen
Copy link

Here's the scenario. Imagine three classes like this in a Rails app:

class Post; end
class Article < Post; end
class Image < Post; end

If the DB contains data that looks like this in the posts table:

id | title      | type
 1 | An article | Article
 2 | An image   | Image

Then this works great:

post = Post.find(1)
puts post.class           #Article
puts post.decorate.class  #ArticleDecorator

However, the following does not work so well (assuming Post.published returns a query):

posts = Post.published.decorate
puts posts.map { |p| p.class } #[PostDecorator, PostDecorator]

My expectation would be that the classes returned when decorate is called on a collection would be:

[ArticleDecorator, ImageDecorator]

But, it doesn't. I assume I'm not missing something obvious, but might be.

Assuming this isn't operator error on my part, it seems the easy way to fix this would be to always defer to the item's decorate method. That may or may not be performant. One thing that could be done to see if deferring to item decoration is necessary would be to do a test like this:

Post.column_names.include? Post.inheritance_column

That would, in the case of the Post class, tell you if it was set up for STI or not.That might be premature optimization or there might be an easier or more general way to see if subclassing is a factor.

Is there any code in the works to address this? If not, I'm happy to write it myself and issue the pull request, but a brain dump would be appreciated to make sure I have as much context as possible.

BTW, awesome gem.

@haines
Copy link
Contributor
haines commented Feb 12, 2013

Nuts, I wrote a lengthy reply, and then lost it. GitHub needs to autosave drafts!!

Basically the gist was this:

  1. Thanks for reporting this issue!!
  2. It's due to Post.published.decorate being equivalent to calling PostDecorator.decorate_collection(Post.published). I think the latter should only create PostDecorators, because being a method on PostDecorator it would be a bit surprising if it created anything else. However I agree that the former should infer each item's decorator, because STI.
  3. PostDecorator.decorate_collection calls CollectionDecorator.decorate(..., with: PostDecorator), which decorates each item with PostDecorator. However, CollectionDecorator.decorate(...), or equivalently decorate(..., with: nil), infers each item's decorator.
  4. Therefore Post.published.decorate(with: nil) works.
  5. So the fix is simple: make with: nil the default option: Support STI in Decoratable.decorate #470
  6. Thanks again! ❤️

@haines haines closed this as completed in b156635 Feb 12, 2013
@emarthinsen
Copy link
Author

This is great. Totally simple. Thanks!

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

Successfully merging a pull request may close this issue.

3 participants