Country list in YAML format, countries.yml

I made myself a list of countries in YAML format for use in a Rails app today.

Here it is: countries.yml

You can put this file into a directory called db/seed_data and then set up a rake task to load it into your database. Put this into the file seed_data.rake in the lib/tasks directory:

namespace :seed_data do
  desc 'Load seed data into the database of the current environment'
  task :load => :environment do
    require 'active_record/fixtures'
    Dir.glob(RAILS_ROOT + '/db/seed_data/*.yml').each do |file|
      Fixtures.create_fixtures('db/seed_data', File.basename(file, '.*'))
    end
  end
end

You can then issue the following command to load the seed data into the database. It will load into the development database by default — use the RAILS_ENV=production parameter to get it into the production database.

rake seed_data:load

Before you do that, however, you are going to need a Country model to access your countries through.

(more…)

XHTML and CSS validation on Rails

You can build XHTML and CSS validation right into your Ruby on Rails Rspec tests - thanks to a plugin called be_valid_asset.

Combine this with autotest and voila - you can be alerted the moment you change something in the view code that breaks your site’s valid markup.

Take this one step further and add your tests into your Capistrano deployment script - and you literally cannot deploy your site when it is in a state that is not standards-compliant.

Get started by installing the be_valid_asset gem (or plugin), and inserting the following line into your Rspec spec_helper.rb file:

config.include BeValidAsset

(more…)

Automatic rails testing with autotest

I found a really good way to speed up your workflow: a library called autotest that automatically (and intelligently) triggers the tests in your Ruby on Rails project whenever it detects files that are modified.

This is great because it means that you can fiddle around with your code with a greater level of confidence, because you know that the moment you break existing functionality, you will be alerted.

You can install autotest simply by installing the ZenTest gem:

gem install ZenTest

And then type the following from your Rails project directory:

autotest -rails

I even found a way in which you can set up autotest to use your desktop notification system to alert you of when tests have passed or failed, making it even more visible to you as you code. I am running Ubuntu, so I set it up through the Gnome notification system, but according to this article on getting started with autotest you can also enable this on OSX and Windows.

autotest in action with Gnome notifications

Event tracking in Google Analytics

I did some experimentation with a really nice feature in Google Analytics called event tracking the other day, and I wrote about it in the thruSITES blog.

Theoretically, you could trigger an event for almost any sort of event within the user interface of your web site. You could even do tricky stuff like record how far users get through a form before bailing out. Or even how long users are taking to get through a form on average, using the optional_value field.

Read the whole post: Event tracking in Google Analytics.

Deploying to multiple environments with Capistrano

Deploying your web app to multiple environments (e.g. development, test, production) is a cinch using Capistrano. Let me show you how.

Capistrano is written in Ruby, so this is your main pre-requisite. Get Ruby and Rubygems installed, and then simply type the following command to install the Capistrano gem:

gem install capistrano

Getting your application ready for deployment starts with the following command, issued from inside your app’s top-level directory:

capify .

This two files: Capfile and config/deploy.rb. The latter is where we are going to tell Capistrano how we want it to deploy our application.

set :application, "myapp"
set :repository,  "git@github.com:johngrimes/myapp.git"
set :scm, "git"

The first part of our deploy.rb gives our app a name and tells Capistrano where to pull the latest copy of our app from. This example has us using Github as our source code repository.

(more…)