I’m not a huge fan of Rails view helpers, which is a post for a different day, but put simply I prefer to encapsulate presentation logic in a presenter object rather than mix it in globally across all templates. There’s a few ways to do this, the simplest being the Good Ol’ Plain Ol’ Ruby Object:
class FooPresenter
def description_for(foo)
Date.today.tuesday? ? "50% off! #{foo.description}" : foo.description
end
end
Usually, I’ll instantiate the presenter in a controller and then call it within the view:
class FoosController
def index
@foos = Foo.all
@foo_presenter = FooPresenter.new
end
end
Everything is simple and feels where it should be, but we’ve gotten this at the cost of losing view helpers mixed in from Rails. One of the helpers I find particularly helpful is strip_tags. If we want access to strip_tags we can do as Rails does when mixing it into the view scope and include it in our presenter:
class FooPresenter
include ActionView::Helpers::SanitizeHelper
def description_for(foo)
description = strip_tags(foo.description)
html = Date.today.tuesday? ? "50% off! #{description}" : description
html.html_safe
end
end
This feels a bit wrong as well. For one, we’ve increased the number of public methods on our presenter by at least 4, and only really needed one of them. Two, we have little control over how we are stripping the tags, and so any unit tests we’ve written for our presenter must integrate and essentially test parts of Rails. What we really want is something that can do what strip_tags is doing. Luckily, and due Rails becoming more modular, we already something to do this for us in the Rails library html-scanner. Under the hood, strip_tag is, unless configured differently, passing your string down to the sanitize method on an instance of an HTML::FullSanitizer object. We can do something similar in our presenter:
require 'html/sanitizer'
class FooPresenter
def initialize(sanitizer=HTML::FullSanitizer.new)
@sanitizer = sanitizer
end
def description_for(foo)
description = @sanitizer.sanitize(foo.description)
html = Date.today.tuesday? ? "50% off! #{description}" : description
html.html_safe
end
end
Now we have an object that is easy to test purely as a unit, and also has the ability to be extended with different sanitizers.
While this works well with strip_tags, unfortunately not every helper in Rails is as nicely decoupled. The work being done in the number_to_* methods, for instance, are written completely in modules. It may be nice at some point to pull these into corresponding objects, but a short term solution could be to just have an object that includes the module:
class NumberCruncher
include ActionView::Helpers::NumberHelper
end
> NumberCruncher.new.number_to_currency(1)
=> "$1.00"
About the Author