'How do I add an uncountable inflection for a non-word character in Rails
In a Rails app I have a number of measurements to display in a human readable form that include both a value and units. Sometimes the units string needs to be pluralized like '1 minute, 2 minutes' but other times not like '1 kg, 2 kg'. I was able to solve this by adding 'kg' do the list of inflect.uncountable
in inflections.rb. However, adding '%' to the list had no effect. I confirmed in the console that '%' was added to @uncountables, but calling pluralize(90, '%')
still returns 90 %s
.
I was able to get around this by doing inflect.irregular '%', '%'
but that doesn't seem like the proper use of irregular.
Is there something I can add like an escape character to get rails to recognize '%' in the uncountables array? Or alternatively, is there a better way to handle this outside of inflections?
For reference, inflections.rb right now looks like
ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
inflect.irregular '%', '%'
inflect.uncountable %w( bpm kg mL )
end
Solution 1:[1]
inflect.uncountable
works only with words, or better said, with whatever Regex deems as being a word. I'm not sure if this is a shortcoming of the implementation or the intended behavior.
inflect.irregular '%', '%'
looks like a perfect workaround to me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mihai Dinculescu |