Michael Schuerig wrote:
>> > If you only include specific helper modules, you can have
>> > multiple methods with the same name, but different
>> > implementations. This can be useful in order to avoid
>> > inadvertently stepping on another method with the same name in
>> > an overcrowded helper namespace.
>>
>> Of course. No argument there. I just don't understand your
>> "procedural" claim.
>
> Well, helper methods *are* procedural.
All methods are procedural.
No. Strictly speaking, helper methods ought to be called helper
functions as they are not attached to specific objects. Methods in the
object-oriented sense are not procedural, however, they mostly are
implemented procedurally.
> Only by judiciously including
> helper modules you can manually achieve the illusion of
> polymorphism (one method name, multiple implementations).
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.
>> > More interestingly,
>> > you can have generically named helper methods with
>> > implementations appropriate for different model classes.
>>
>> But then wouldn't those be better as model methods, not helper
>> methods?
>
> Formatting (e.g.) doesn't belong in the model, does it?
I think simple formatting does -- for example, I like to have a
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.
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. Nicely, this even works with partials as they use just
those helper methods which are available in the given context.
Michael