views:

189

answers:

3

Is there a helper to avoid this sort of code?

= @leads.length == 1 ? 'is' : 'are'

I know about pluralize but it doesn't help in this case. I know that writing a helper would be trivial, I want to know if there's something in the Rails API that I have overlooked.

Thanks,

-Tim

+1  A: 

Maybe you could use pluralize and add a inflection for 'is'

Inflector.inflections do |inflection|
  inflection.irregular "is", "are"
end

so that

'is'.pluralize # => 'are'

(I didn't test the solution, but it should work.)

t6d
A: 

Another option is to use the i18n translation library included with rails.

This allows you to define a word or sentence (with interpolations) with both a singular and plural version.

Although it may seem like over complication, I highly recommend using i18n for every project from the get go as its not only the logical place for active record errors but makes your app ready for deployment in another language pretty easy.

It also provides lots of extra addons and benefits with date and time localization. Read about i18n inside out here

Josh K
+1  A: 

As t6d said you need to add is/are to the Rails inflector. Just the code from td6, but you need to add the ActiveSupport namespace:

ActiveSupport::Inflector.inflections do |inflection|
  inflection.irregular "is", "are"
end

Now the helper Rails has built in is pluralize, but in a view that won't do what you want. For example:

pluralize(@leads.length,'is')

outputs (for 2 leads)

2 are

For what you want you need to make your own helper that will pluralize the word, but not output the count. So if you base it on the current rails pluralize:

def pluralize_no_count(count, singular, plural = nil)
  ((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
end
ScottD