One of my favorite changes in Capybara 2.1 is ignoring all hidden elements by default. This could be viewed as a limiting of Capybara’s feature set, since you can no longer (easily) test certain elements on a page. I will argue that it steers you into writing tests that are more realistic, causing this limitation to actually enhance the quality of your acceptance tests.
Presenter Sanity
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
<% @foos.each do |foo| %>
<%= foo.name %>
<%= @foo_presenter.description_for(foo) %>
<% 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"
02/25/13: 7304.84 Days of Ruby
Interestings
holepicker
http://psionides.eu/2013/02/18/pick-holes-in-your-gemfiles/
Find gems with vulnerabilities
SF Standup 11/04/11
Interesting Things
Lightning Talks are next week, but no talks have been submitted yet. Unless talks are submitted, it’s going to be a long hour of staring at the wall.
jQuery 1.7 has been released http://blog.jquery.com/2011/11/03/jquery-1-7-released/ . If you use Backbone.js you might want to update jQuery for the improved delegated events performance.
Standup 00110111 – AKA 55 day
Interesting Things
- The Postgres adapter for ActiveRecord will not use an offset unless you also provide a limit.
- Tomorrow Occupy Oakland Is calling for a General Strike. The Port of Oakland, numerous schools, and many other institutions will be shut down, as all their employees will be marching in the streets. Come out in solidarity before, during, or after work. It’s gonna be really fun.
Standup 07/20/2011: I Know What Bowling Is
Ask for Help
“Rubymine can’t call git ls-files in gemspec”
This should have been fixed a while ago in the developer image.
“Anyone built ruby 1.9 from source? I’m not sure what options to configure it with.”
Check to see what options RVM is using when it builds 1.9.
Interesting Things
- nil
David Stevenson
- I’m running a blog using ruby 1.8 off a router.