-
Notifications
You must be signed in to change notification settings - Fork 526
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
When decoration fails, show original class #821
Conversation
@@ -69,16 +69,16 @@ def decorator_class? | |||
# `Product` maps to `ProductDecorator`). | |||
# | |||
# @return [Class] the inferred decorator class. | |||
def decorator_class | |||
def decorator_class(called_on = self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pascalbetz Thanks for the contribution! I am not crazy about a public(ish) method taking a parameter that is only relevant to draper's internals. However, I can't think of a better way to accomplish this and it definitely solves a longstanding (and very annoying) problem.
Can you rebase
with master before I merge? There has been some changes to this method in master as well and want to make sure the build passes. After that, I think we are good to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codebycliff I understand.
This was the easiest way to solve the problem... Alternatively we could change from recursion to iteration, this would get rid of the parameter. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pascalbetz It looks like however you merged, it brought in master
changes to this pull request... Can you try again with rebase
?
As far as the solution goes, I'm not sure. I'm not super opposed to this solution, however, I am interested in what the iteration solution looks like. Especially now that decorator_class
returns nil
if can't be found. It might make the iteration easier.
@codebycliff sorry for messing up the rebase (thought I can easily do this while eating, watching a movie and solve some SQL stuff I'm working on. Looks like I'm not as smart as I thought ;-) ). Will clean this up when you decide to merge the recursive solution instead of the iterative solution at #823 |
@pascalbetz I think we'll go with the recursive solution for now. It's much simpler to read and understand. Plus, |
@codebycliff PR should be cleaner now. Here is the iterative solution for documentation purpose:
|
@pascalbetz Thanks again! Sorry it took me so long to get it merged. |
No problem at all. Thanks for your work on Draper. I use it in various projects. |
Description
Thanks for the work on Draper to all the maintainers 👍
This provides a fix for #819
When decorating an object without decorator, then the last class which was checked for a decorator is contained in the error message. In case of Rails this is most often
ActiveRecord::Base
.This is a bit misleading and it would be better if the class which was actually tried to decorate is shown in the error message.
This is caused by the recursive nature of the
decorate
method which moves up the hierarchy when no decorator class found. And raises when it can not move further up.Before:
After:
Testing
Outline steps to test your changes.
Alternatively:
ActiveRecord::Base
(The fix is not restricted to AR)
References