views:

215

answers:

2

For example, why are the date helpers written like this:

time_ago_in_words(@from_time)

Instead of like this:

@from_time.time_ago_in_words

Is this a clear design error / inconsistency? Or is there some reason for this?

+2  A: 

Great question!

Most of the helpers are for selecting dates / times.

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html

I'm guessing whoever wrote it wanted a couple of util methods and so added time_ago_in_words and distance_of_time_in_words to their helper with the selects.

As time_ago_in_words and distance_of_time_in_words are presentaton focused (i.e. you may want to localize it) the developer maybe felt that the helper isn't a bad place for them.

RichH
I'm not sure the inference in the last paragraph follows -- e.g., "to_s" is presentation-focused, and you might want to localize it, but we still do @from_time.to_s rather than to_s(@from_time).
Horace Loeb
I agree - I did use the word maybe ;) Updated to make my answer more general!
RichH
+3  A: 

Helpers are methods on the view object, rather than on the object they're displaying. This makes sense in an OO sense because the view is displaying the data in a particular format, so it's in charge of converting the models to that format.

Sarah Mei