Thursday, October 15, 2009

A Slight Rewrite

‹prev | My Chain | next›

As mentioned yesterday, my last minute decision to switch the dates in recipe URLs from dash separated (/recipes/YYYY-MM-DD-short_name) to slash separated (/recipes/YYYY/MM/DD/short_name) caused many things to break. My specs / unit tests caught a few of these failures. My Cucumber scenarios caught many. I am perfectly comfortable with this.

My specs drive design. Through behavior driven development, they ensure that my design is the simplest thing that can possibly work. Simple means fewer bugs today and less legacy tomorrow. And, hey, if they happen to catch a bug or two, that's great. Specs are not for regression testing, they remain to reflect my thought process when I return months (years) later.

This is not to say that regression testing is not important—it is. However, regression testing is not as valuable as clean design is for long term health of a project.

One of the aspects of Cucumber that I value so much is that it can drive features, but also verify them once they are implemented. As such, I rely heavily on Cucumber to catch regressions.

In the case of my change yesterday, I ended up with 10 regressions identified by Cucumber. I am able to fix all of them by updating visit statements in the Cucumber steps and updating a few helpers:
jaynestown% cucumber features
...
39 scenarios (1 pending, 38 passed)
344 steps (1 pending, 343 passed)
0m42.922s
One last thing for me to do is to accommodate really old URLs. When we first put up the site, we baked HTML from XML. Thus all of the URLs had .html extensions on them. The legacy Rails site supported extensions, but linked internally without the extension. But what if someone has a really old bookmark? I would prefer not to dump them onto the Not Found page and, thanks to John Trupiano's Rack::Rewrite, I do not have to worry.

After gem installing rack-rewrite, I add this to my rackup file:
require 'rack-rewrite'

###
# Rewrite
use Rack::Rewrite do
r301 %r{(.+)\.html}, '$1'
end
Now, when I ask for a sausage recipe with the .html extension, I am redirected to its new, permanent location:
jaynestown% curl -I http://localhost:3000/recipes/2008/07/13/sausage.html
HTTP/1.1 301 Moved Permanently
Location: /recipes/2008/07/13/sausage
Content-Length: 14
Connection: keep-alive
Server: thin 1.2.2 codename I Find Your Lack of Sauce Disturbing
Tomorrow I will deploy and close out my chain.

1 comment:

  1. Hey Chris, great usage of Rack::Rewrite there. Good luck closing out the chain tonight!

    ReplyDelete