
My Rails tutorial (part 1 of 4) has been published in Linux Format 108. It shows you how to build a Rails-powered photo gallery, by using basic techniques and popular plugins.
Grab it from your favourite newsagent and let me know what you think!
I’ve been writing technical articles on this blog for 3 years, and never mentioned much about my personal or professional life. Here’s what’s been going down:

Changes is a fast and friendly diff tool. You can quickly list files that have changed in a project, and view differences between files. It also works with Subversion and Git (and several other version control systems), so it will fit right into your typical workflow. Oh, and it also has a TextMate bundle.
Changes gets perhaps the highest accolade for a mac application: I like Changes so much it actually has a permanent place in m…
Welcome to Rapid Rails Part 3: Desktop mastery, the third article in my series focussing on making Rails (and yourself) faster.
A good programmer recognises when to reuse and therefore reduce code. A great programmer applies this tendency to their own workflow. Whether you use an IDE or text editor, working with Rails can be made more pleasant and efficient by observing commonly performed tasks and simplifying them.
The examples given below have a heavy bias toward TextMate, Vim and Mac OS. If you work in Windows or Linux, at the very least consider the following 10 ideas.
This is part 2 of the Rapid Rails series. Part 1 featured tips on how to work more efficiently with Rails by making the most of the bundled and related command line tools. This part discusses how to make your Rails application perform faster, with particular focus on server optimisation. Why? Because systems administration requires a very different skill set to programming, and I’ve been often been expected to manage sysadmin tasks on my Rails contracts—and I bet you have too!
I’ve included real-world e…

Rapid Rails is a series of articles containing succinct tips to increase your productivity when working with Ruby on Rails. This is the first part, and shows you how to make the most of the command-line tools that come with Rails.
The Rails generator script (found inside an application’s directory at script/generate) allows you to quickly create templates for anything you need within Rails. As well as models and controllers, you can also use it to write entire migrations:
scr…
This article is an introduction to testing Rails plugins. It’s a relatively lengthy post, so if you’re reading this in an RSS reader flag it and come back when you’re not too busy. It follows the “taxonomy” style of my previous plugin article, A taxonomy of Rails plugins, where examples are used from open source software.
Knowledge of both unit and functional testing is assumed. The following topics are covered:
There are now almost 1000 entries in the main resource for Ruby on Rails plugins, Agile Web Development’s Plugins Directory. Of these, 596 have repositories listed that are accessible. I wanted to see how many of these plugins came with some form of tests, so I created a spider (using a web spider library) and performed some basic analysis on each repository.
After reading a few magazines, and watching two entire TV shows, the spider came back with a result: 54% of the plugins have tests.
It’s amazing to …
Start using Test Driven Development today is a post I wrote over on my company’s blog. It covers how TDD has improved my work since I started Helicoid Limited, and how you can start using TDD right now.
Despite leaving it for a while, I’m going to continue my previous article, “A taxonomy of Rails plugins”, relatively soon. I’ve been inspired by the excellent plugins by errfree and come up with a few ideas of my own during the heavy workload of 2007. In particular, I’d like to clarify testing Rails plugins. But more of that in the near future.
For now, have a look at 10 essential ruby gems. It’s hosted on my company’s new blog, where you’ll eventually find more articles by me with a greater emph…
A common stumbling block for Rails developers is learning the basics required to write plugins. This is made more complicated by the fact that Ruby is inherently dynamic and offers many techniques for code reuse. Luckily, if you can write Rails applications you can write plugins by simply drawing on a handful of basic patterns.
The purpose of this article is to demystify writing plugins using examples of common patterns used by popular plugins.
Writing a plugin will:

Boards of Canada wrote a song called Constants are Changing. In Ruby even constants are dynamic, holding a reference to an object rather than the object itself. This consistency makes modifying constants possible, and rather than being something considered distasteful it may form an integral part of the design of a system.
Changing a constant’s reference will result in a warning, but there are times when ignoring these warnings might be arguably legitimate. I wrote a simple tool that runs for long periods of time, and …

Have you ever been working on a project that has session variables controlling important aspects of functionality? When they start falling out of controllers and views into a big pile of soup on the floor it’s probably time to encapsulate them.
Although relying on the session object in controllers and views is relatively easy to understand, you should be very wary when you’re tempted to use them this way. Thinking, “I’ll just add a session variable for this” will probably lead to misunderstandings or bugs later on. And on…
Writing test code isn’t easy at first, and writing good test code is even harder. I’ve reviewed several tools to help write better tests here, focussing on ruby.
Code coverage tools attempt to analyse how much of your code has been tested. Reports are generated based on your test code, with columns expressing how much code has been tested.
Incentives for using code coverage tools are:
‘Push Back’ is a simple concept succinctly explained in 37signals’ book, Getting Real. When translating requirements into code, issues often crop up that make implementation impractical. Something about the structure of your project resists a new feature.
Getting Real suggests ways to mitigate push back. I’m going to explain how to spot push back occurring in your code.
Methods or variables become hard to name
If you’re having issues naming things, perhaps you don’t fully understand the problem you’re trying to solve. If the concept is ill-defined,...
Have you found that any of your Rails projects get hits to controller methods that expect parameters? A neat way of handling this is the ActionController::Verification module.
By adding calls to verify in your controller, you can elegantly catch all kinds of unexpected but reasonable uses of your system: from mistyped URLs and browser history auto-complete to web crawlers blindly following links. This way, you can redirect people somewhere logical instead of flashing them with an error message.
You can even insert a message into the flash:
verify :params => “user”,
:only => ...
Have you ever tried concatenating your JavaScript and CSS files for performance improvements? The idea is that latency is a bigger issue than file size when loading web pages, so stuffing all your JavaScript into a monolithic file for deployment should improve performance.
I wrote a rake task to do this for some of my applications (such as tiktrac). This is slightly more cumbersome than a feature I spied in the ActionPack changelog:
Added caching option to AssetTagHelper#stylesheet_link_tag and
AssetTagHelper#javascript_include_tag [DHH]. Examples:
stylesheet_link_tag :all, :cache => ...
With hpricot you can do anything!
puts ((Hpricot(open(URI.escape(“http://www.google.com/search?q=#{term}”))))/”a.l”).collect { |link| ”#{link.innerHTML}: #{link.attributes[‘href’]}” }.join(”\n”)
Apparently, Opera 9 supports Server-Sent Events which you can read about in the WHATWG Web Applications 1.0 specification. Their demo application is a little chat program, which is a very obvious example of the technology.
There’s many times when I’ve wanted to add this to my applications: in Bugtagger I have to ask the service if a new message from a customer has been posted to a bug, every n seconds. It would be far more efficient for the serve to send a message using this technology.
And, since Opera’s so popular on embedded and mobile platforms, if this makes its way to mobile p…
I needed a quick way of exporting data as zlib from a controller in Rails, so I came up with this:
def export
send_data compress_string(Document.find_all.to_xml), :filename => 'backup.xml.gz'
end
def compress_string(data)
gz = Zlib::GzipWriter.new(StringIO.new(''))
gz.write data
gz.close.string
rescue
gz.close
raise
end
Another way would be to use tempfiles with Tempfile—I wanted to benchmark and profile using files compared to StringIO, but that’ll be an exercise for another day.
This could also work nicely with Minitar.