Draper recommends decorating objects at the end of a controller method or alternatively using their decorates_associated
method to automatically decorate them.
I like the idea of the view explicitly declaring what it expects to receive (granted the rest of Rails doesn't work like this but it still feels nice). I'd therefore prefer to decorate objects at the top of the view rather than at the end of the controller:
So in users#show I would like to do:
- @user = @user.decorate
%h1= @user.full_name
%p= @user.description
Instead of decorating in the users controller like this
class UsersController < ApplicationController
...
def show
@user = User.find(params[:id]
@user = @user.decorate
end
end
I suspect there's some caching issue with doing things this way round but it feels nice. What if anything am I missing? Is what I'm proposing bad practice?
I think this is a very good question, because draper confuses the traditional MVC logic a bit. As you can read in the draper README which you have linked, draper aims to replace cluttered helper definitions with a more structured approach.
However, to which part of M-V-C does this belong to? Draper claims to decorate objects, which can be associated to the representation part of the software architecture and therefore neither belongs to the Model nor does it really fit into the Controller.
Following @Damien Roche's comment, it also does not really fit into a View, because one usually does not expect a template file to perform any more actions on an object than displaying its properties.You also do not define your helpers in the template, right?
To my mind, draper can be seen more like an intermediary that extends the objects a controller has selected for displaying before they reach the view. Following that logic I prefer the usage of the
decorate_assigned
command, because that places the decorations somehow between Controller and View.Additional remark: I know what you mean with 'the view declaring what it wants to receive' but this stands in contrast to the fact, that you usually define one decoration per model for the whole application. So there is not so much room for 'special requests'