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:

RUBY:
  1. # Delegate to BpReading
  2.   def number_of_bp_readings_in(period)
  3.     bp_readings.number_of_readings_in period
  4.   end

This code means that I can do:

RUBY:
  1. 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

RUBY:
  1. 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:

RUBY:
  1. @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?

ruby on rails, Uncategorized | Comments | Trackback Jump to the top of this page

One comment on “rails presenters, REST and fat models”

  1. 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.

    Ismael at October 19th, 2007 around 11:15 am
    Jump to the top of this page

Leave a Reply

  •  
  •  
  •  

You can keep track of new comments to this post with the comments feed.

Recently on Flickr

    mull - 21.jpgmull - 20.jpgmull - 19.jpgmull - 18.jpg

Recently Listened

Meta

The Carousell