On 19 Apr 2009, at 11:08, Fernando Perez wrote:

  @order.product_titles do |product_title|
    <%= product_title %>
  end


Another problem, is that not only do I need the title, but also a
clickable permalink which uses a url helper (not available to models),
the product price, and maybe other stuff in the future. So I might end
up with a nice messy code.

This is where I start to introduce a presenter layer in between the view and the models.

If you have a Ruby class anywhere in rails, you can access the auto- generated url helpers just by including ActionController::UrlWriter.

class OrderPresenter
  include ActionController::UrlWriter

  def initialize(order)
    @order = order
  end

  def product_titles
    @order.items.map{ |i| i.product.title, product_url(i.product) }
  end
end

Construct the presenter in the Controller. Then in the view:

@order_presenter.product_titles do |product_title, url|
  <%= link_to product_title, url %>
end

For the time being I use delegate, so I traded a dot for an underscore. It's easy to spec, and after all, an item only exists in the context of
an Order, so I think it's fine to break Demeter's 'guideline' and say
that it's cool that Order knows a little bit stuff about Item's
internal. However a Product can exist without an Order, so Order doesn't
need to know anything about Product and vice versa.

But I'll have to think more about it, because as you said making a
little change in a model can result in having to fix places that are far
far away and totoally unexpected.
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Matt Wynne
http://beta.songkick.com
http://blog.mattwynne.net



_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to