Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Less is more in Capybara 2.1

Brian Butz
Sunday, April 28, 2013

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.

Continue reading →

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Presenter Sanity

Brian Butz
Sunday, March 24, 2013

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"
  • 0 Shares
  • Share on Facebook
  • Share on Twitter

02/25/13: 7304.84 Days of Ruby

Brian Butz
Monday, February 25, 2013

Interestings

holepicker

http://psionides.eu/2013/02/18/pick-holes-in-your-gemfiles/

Find gems with vulnerabilities

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

SF Standup 11/04/11

Brian Butz
Friday, November 4, 2011

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.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Standup 00110111 – AKA 55 day

Brian Butz
Tuesday, November 1, 2011

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.
  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Standup 07/20/2011: I Know What Bowling Is

Brian Butz
Wednesday, July 20, 2011

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.
  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Brian Butz

Brian Butz
San Francisco

Subscribe to Brian's Feed

Author Topics

acceptance-testing (1)
bloggerdome (1)
capybara (1)
helpers (1)
presenters (1)
rails (1)
agile (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >