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