How to use the "number_to_currency" helper method in the model rather than view?
I know this thread is very old, but someone can look for solution for this problem in Rails 4+. Developers added ActiveSupport::NumberHelper, which can be used without accessing view related modules/classes using:
ActiveSupport::NumberHelper.number_to_currency(amount, precision: 0)
I agree with all of you that this could be breaking the MVC pattern but there is always reasons to break a pattern, in my case I needed these currency formatter methods to use them in a template filter (Liquid in my case).
At the end I found out I could access to these currency formatter methods using things like this:
ActionController::Base.helpers.number_to_currency
It’s not available because its use in a model (typically) violates MVC (and it does seem to in your case). You're taking data and manipulating it for presentation. This, by definition, belongs in the view, not the model.
Here are some solutions:
Use a presenter or view model object to mediate between the model and view. This almost definitely requires more initial work than other solutions, but is almost always a better design. Using helpers in a presenter/view-model doesn’t violate MVC, as they reside in the view layer, replacing traditional custom Rails helpers and logic-filled views.
Explicitly
include ActionView::Helpers::NumberHelper
inJobsHelper
instead of depending on Rails to have magically loaded it for you. This is still not great, as you shouldn’t access a helper from a model.Violate MVC & SRP. See fguillen’s answer for how to do this. I won’t echo it here because I don’t agree with it. Even more so, though, do I disagree with polluting your model with presentation methods as in Sam’s answer.
If you think “but I really need this to write my to_csv
& to_pdf
methods in my model!”, then your entire premise is wrong—after all, you don’t have a to_html
method, do you? And yet your object is very often rendered as HTML. Consider creating a new class for generating your output instead of making your data model know what a CSV is (because it shouldn’t).
As for using helpers for ActiveModel validation errors in the model, well, I’m sorry but ActiveModel/Rails has screwed us all there by forcing error messages to be realized in the data layer, rather than returning the semantic idea of an error to be realized later—sigh. You can get around this, but it basically means not using ActiveModel::Errors anymore. I’ve done it, it works well.
As an aside, here’s a useful way to include helpers in a presenter/view-model without polluting its set of methods (because being able to do e.g. MyPresenterOrViewModel.new.link_to(...)
makes no sense):
class MyPresenterOrViewModel
def some_field
helper.number_to_currency(amount, :precision => 0)
end
private
def helper
@helper ||= Class.new do
include ActionView::Helpers::NumberHelper
end.new
end
end