Rapid Rails Part 2: Rapid Literally

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 examples from Linux, Lighttpd, Exim and MySQL because these are what I currently use on most of my servers. If you’re just starting out trying to boost your server performance there are some general principles for you, too.

Rails application performance

People new to Rails are often concerned about its performance in production. In my experience, unless you’re lucky enough to be dealing with a ridiculously popular site, you shouldn’t have major performance issues. However, at the very least you should do the following to every application you deploy:

Read More →

Rapid Rails Part 1: Command Line Mastery

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.

Quickly generate migrations

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:

Read More →

Rails Plugin Testing Guide

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:

  • Testing plugins outside Rails applications
  • Creating models and fixtures to test with ActiveRecord plugins
  • Managing a test database schema
  • Testing ActionPack plugins
  • Automating with Rake

Testing Plugins Outside Rails Applications

Testing plugins that are tied to a Rails application is straightforward, and it’s tempting to leave things this way. If you need to distribute your plugin to developers working outside your project, or wish to open source it, you may have to do a little bit of work to create a miniature Rails environment to test in. The upside of this extra effort is that it also forces you to think about the plugin in generic terms, often resulting in better design and cleaner encapsulation.

Read More →

Some Ruby on Rails Plugin Stats

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 think that there’s almost 1000 plugins in one directory, and a good proportion have tests amongst those I could easily analyse.

Read More →

Start Using Test Driven Development Today

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.

  1. Learn how to write basic tests in your chosen language – don’t worry about fully learning the test framework, start by copying examples
  2. When adding a new feature, start by creating tests first. Write code to satisfy these tests
  3. Refactor old code to work well with automated tests
  4. Write test data carefully and patiently, using this as a design technique for exploring the quality of your data modelling
  5. Find a mocking library so you can write encapsulated tests that don’t require external resources
  6. Find a tool for producing coverage reports in your chosen language
  7. Investigate automated testing triggered when deploying/releasing code

10 Essential Ruby Gems

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 emphasis on web design.

Read More →

A Taxonomy of Rails Plugins

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.

Why write plugins?

Writing a plugin will:

Read More →

Constants are Changing

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 occasionally needs to reload associated classes. Ruby makes this possible, but raises warnings when constants are redefined. In this case I decided to ignore the warnings (although eventually re–factored.)

Read More →

Session Encapsulation

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 once session variables start relying on each other for application logic it really is a good time to encapsulate them.

Read More →

Tools for Tests (JavaScript Edition)

In my previous article Tools for tests, I discussed tools designed to help write better quality tests. This article expands on the topic by exploring several tools and libraries that help test JavaScript.

Testing JavaScript seems unnatural on the surface, because it’s difficult to see how it can be automated. Tests require a full implementation of the DOM, so it’s hard to imagine how anything but browser–based tests could be useful. Luckily, most operating systems provide some way of accessing applications in a scriptable fashion — AppleScript in Mac OS is the most obvious example. So before you start using alert() to print values and debug code consider this: automating JavaScript testing is definitely possible and you have no excuse for not doing it.

Code design

Well–designed code is often easy to test. If you write JavaScript inline with your HTML you’re going to have a hard time testing it.

Read More →