logo
  • Jobs
  • About Me
  • Contact
  • Home

Archive for January, 2009

Testing Helper Modules with Rails 2.2

I recently found myself in a situation where I wanted to make sure that the current year is always displayed in the copyright statement at the bottom of a Rails application. At first glance, this is easy enough. All that a person needs to do is add the following to their application.html.erb:

Copyright &copy; 2007-<%= Date.today.year.to_s %>

However, as we start to implement this, it becomes apparent that this is not very test driven. I sought to find a way to implement and verify this functionality with a breaking test first.

Since view tests can often be very brittle, I really wanted to implement this in my ApplicationHelper module, so that I could hopefully test it easier, but also reuse to functionality in other areas.

Testing Rails helpers has been hard historically, but as I investigated how I might test this functionality in the ApplicationHelper module, I discovered the ActionView::TestCase base class. This class provided all the hook up that I needed to call methods on my helpers. Armed with this, I set out to write my test:

class ApplicationHelperTest < ActionView::TestCase
  def test_copyright_includes_current_year
    actual_copyright = copyright_text()
    assert(actual_copyright.include?(Date.today.year.to_s), "Copyright must include current year.")
  end
end

This fails pretty quickly, since it cant find ActionView::TestCase. The easiest way to resolve this is to add require 'action_view/test_case' to your test_helper.rb. When we run the test again, it fails again, since the copyright_text method does not exist, so we flip back over to the ApplicationHelper module and implement the method.

def copyright_text
  "Copyright &copy; 2007-#{Date.today.year}, Company Name. All Rights Reserved."
end

Voila. Our tests now pass. The last step is to add a call to this helper method in the view:

<%= copyright_text %>

We may take this a step further and also implement additional checks around the copyright statement, which can be easily done with the assert_match method. This was not needed in my particular case though.

No Comments

New MacBook Pro

About 18 months ago, I purchased my first MacBook Pro… a 17 inch monster laptop. I absolutely loved the big (1920×1200) display that I purchased as an additional feature.

Over the course of the past year, I realized that I was not really very mobile with that behemoth. Obviously, at more than 17 inches wide and almost 7 pounds, it can be quite a cumbersome unit to drag around with you. Even more, the space it takes up on your lap makes it almost unusable. Calling it a “laptop” was a bit of a stretch.

No more… earlier today, I traded in that MacBook Pro and got the new 15″ unibody MBP. 320gb of drive space, with 4gb of ram packed into a nice little 5 pound chassis. Not quite as small as my wife’s 13″ MacBook, but certainly more inline with what I would consider a laptop. All that said, I really do miss the 1920×1200 resolution, even though the LED display on this is much brighter. If I do want that resolution, I can always plug it into my wife’s 24″ Cinema display.

1 Comment

Hiding an email address from spam harvesters

Using a little css trickery, you can completely hide your email address from spam harvesters. The drawback to this approach is that it will only work on sites that read left-to-right as it uses CSS to reverse the direction of text.

Add this class somewhere in your CSS file.

span.reverse {
  unicode-bidi: bidi-override;
  direction: rtl;
}
 

At the point that you are ready to present the email address, code it in your HTML, but just key it in backwards. For example:

<span class="reverse">moc.rehtrebttam@retsambew</span>

The CSS above will then override the reading direction and present the text to the user in the correct order.

Pretty clever technique.

7 Comments
flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010