rails presenters, REST and fat models
September 6th, 2007
So. I've been drinking the kool-aid for the last year or so and trying to build REST-ful type controllers. That's good I think. Not perfect, but good. As well, I've been following Jamis' advice and doing skinny controllers and fat models. That's also good. Very good.
The main consequence of all this is that my controllers are now very focused on marshalling and un-marshalling data for the http request and response. Again, I think that's good. It separates very well the concerns of the domain objects in model and the mechanics of responding to particular types of requests and choosing the type of view to respond with.
Now the problem with this is that I've been ending up pushing shitloads of methods that look like this into my models:
-
# Delegate to BpReading
-
def number_of_bp_readings_in(period)
-
bp_readings.number_of_readings_in period
-
end
This code means that I can do:
-
patient.number_of_readings_in last_two_weeks
in my views.It's not terrible, and its a minor nod to law of Demeter.
It seems not great to me that I'm pushing all these extra methods into my model just to service my views though. They're really not methods that belong to my domain model per se. They're rather methods that only exist to make my views more convenient and to keep long method chains like
-
patient.bp_readings.number_of_readings_in last_two_weeks
out of my views.
I used to do this kind of manipulation in my controllers by doing something like:
-
@number_of_readings = patient.bp_readings.number_of_readings_in last_two_weeks
I'm so happy with my newfound purity of my controllers, only marshalling and unmarshalling responses to requests, that I don't want to go back to that. At the same time, I'm not totally happy pushing all those methods into my model. I'm beginning to think that what's needed is an extra layer of abstraction like a presenter is the way to go, but so far I haven't seen a presenter package that I find really compelling.
Anyone feel like convincing me of the error of my ways?





One comment on “rails presenters, REST and fat models”
01
Well, I think the idea of a Presenter would be to accomodate the transition from your models to your views, without eachother knowing too much about the other. So your presenter design really depends on your domain.
Leave a Reply