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
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.
The text was updated successfully, but these errors were encountered:
Nuts, I wrote a lengthy reply, and then lost it. GitHub needs to autosave drafts!!
Basically the gist was this:
Thanks for reporting this issue!!
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.
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.
Here's the scenario. Imagine three classes like this in a Rails app:
If the DB contains data that looks like this in the posts table:
Then this works great:
However, the following does not work so well (assuming Post.published returns a query):
My expectation would be that the classes returned when decorate is called on a collection would be:
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:
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.
The text was updated successfully, but these errors were encountered: