[rspec-users] Depot app, Demeter's law and troubles cleanly specing

Fernando Perez lists at ruby-forum.com
Sun Apr 19 15:37:27 EDT 2009

>    http://spin.atomicobject.com/2008/01/27/the-exceptional-presenter
Interesting idea too. So basically I need to totally rethink and 
refactor the way my views display the information to the customer:

Let's see how I currently display or not an add to cart button depending 
whether or not the product is free.

My very first quick and very ugly procedural hard to spec solution was 
to do in the view:

<%- if @product.price >= 0 -%>
  <%= display_button %>
<%- else -%>
  This product is free!
<%- end -%>

Then my second solution was to create an instance method so that the 
view doesn't know about the Product internal mechanism about it's 

<%- if @product.free? -%>
 This product is free!
<%- else ... -%>

Yeah I thought I was an OOP master and Demeter could rest in peace!

But thinking about the GOF Builder and Exceptional Presenter design 
patterns you talk about, that would mean that the html output for a 
Product should therefore happen in the Model itself. And then in the 
show.html.erb, I simply call:
<%= @product.display -%>

and all the magic about whether or not the product is free and to 
display the button has already been handled inside the model (or another 
related place) when it gets instantiated. Same applies to 
item.product.title, that would be handled elsewhere than in the view.

However it might clutter the Model, so actually there is more to MVC: 
each Model should have a sub class or something that handles how the 
model instance will be presented to the view.

How do you handle such issue? Are there some open source rails apps that 
I could learn from?
Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list