Open daniel-rikowski opened 4 years ago
url_for(doc) # => /documents (NOT EXPECTED)
I'd rather say that's what I expected in this case 🤔
Decorators encapsulate models isolating them from the outer code. Here, the outer code sees a document. If you'd like it to see an input one, just define InputDocumentDecorator
.
A quite common use case for decorators is to follow controllers hierarchy. Thus, one model class can be decorated with different decorators depending on the controller scope. In this case it seems quite natural for URLs to match decorator names and not the model ones.
I'm using Draper 4.0.1 and I have these models:
I also have a
DocumentDecorator
, but no dedicated decorators for the STI sub classes.When I call
url_for
using a decoratedInputDocument
instance the wrong path helper is generated/called.Example:
I traced the
url_for
call through the various Rails methods. Eventuallydoc.model_name
gets called, which seems to be the cause of this bug:Draper doesn't delegate the
model_name
call todoc.object.class
but todoc.class.object_class
instead.Subsequentially Rails gets the wrong
ActiveModel::Name
and generates the wrong path helper name.IMHO
model_name
should be delegated to the decorated object's actual class (or even to the instance itself?) and not the class inferred by the decorator, but I'm not sure this is the best course of action...