Michael Schuerig wrote:
[...]
>> What's the point of helper "polymorphism"?  I'm having trouble coming
>>  up with a good use case for it.
> 
> format_value, cancel_link are just two examples where generic names are
> perfectly understandable within a context, although they may need to do
> different things depending on context.

Does cancel_link need to do different things depending on context?  If 
so, what?

> 
>> meaningful to_s.  Complex helpers should probably be made into
>>  partials. There's a fairly narrow band in between where helpers are
>>  really the best solution.
>> 
>> Or maybe I don't understand what helpers are good for.  Can you give
>>  me an example of what you think an appropriate helper method is?
> 
> Say you an overview page where you want to consistently display numeric
> or monetary values with a specific precision (or format in general).
> Instead of sprinkling the view with
> 
>   <%= number_to_currency foo.amount, :precision => 0 %>
>   <%= number_to_currency bar.amount, :precision => 0 %>
>   ...
> 
> you'd better define a suitable helper method:
> 
>   <%= format_money foo.amount %>
>   <%= format_money bar.amount %>
>   ...
> 
> Then, in case you change your mind about how things ought to look,
> there's only one place to change. And, besides, the helper methods name
> can be used to convey some semantics.
> 
> Now consider you have a more detailed view of similar information where
> values are shown with full precision. There you could still use
> format_money, but with a suitable implementation.

I agree that the second option is better than the first, but I think 
they're both kind of bad.  This is putting too much logic in the view 
IMHO (although I admit I sometimes do it anyway, until I refactor. :) ).

What I would do instead is make foo.amount and bar.amount Money objects 
(using composed_of if ActiveRecord is involved).  Then define 
Money#to_s, and the view will simply be

<%= foo.amount %>
<%= bar.amount %>

I don't see any need for a helper here.  Am I missing something?

> 
> Before you ask, I don't like things like format_foo_bar_baz_money at
> all. I don't want this kind of forced disambiguation, because I don't
> want to be overly specific. It's up to the context what format_money is
> supposed to mean. 

I'm not sure what that technique would even be, so don't worry -- I 
wasn't thinking of it! :)

> Nicely, this even works with partials as they use just
> those helper methods which are available in the given context.

True.  But that's only an issue if partials are shared between 
controllers.  In my applications, I find that that does not happen 
often, but YMMV.

> 
> Michael
> 
> --
> Michael Schuerig
> mailto:mich...@schuerig.de
> http://www.schuerig.de/michael/

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to