Is there a way to use pluralize() inside a model rather than a view?
Rather than extend things, I just it like this:
ActionController::Base.helpers.pluralize(count, 'mystring')
Hope this helps someone else!
My favorite way is to create a TextHelper in my app that provides these as class methods for use in my model:
app/helpers/text_helper.rb
module TextHelper extend ActionView::Helpers::TextHelperend
app/models/any_model.rb
def validate_something ... errors.add(:base, "#{TextHelper.pluralize(count, 'things')} are missing")end
Including ActionView::Helpers::TextHelper in your models works, but you also litter up your model with lots of helper methods that don't need to be there.
It's also not nearly as clear where the pluralize method came from in your model. This method makes it explicit - TextHelper.pluralize
.
Finally, you won't have to add an include to every model that wants to pluralize something; you can just call it on TextHelper directly.