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.

Generate a new model with code and name attributes:

script/generate model Country code:string name:string

Then migrate your database to create the new table:

rake db:migrate

I am using this reference data to populate country select lists, so I forked the country_select plugin and modified it to read the countries from the database. My version of the country_select plugin on can be installed by typing the following command:

script/plugin install git://github.com/johngrimes/country_select.git

Now you can use the following code in your views to output a country select list:

< % form_for @whatever do |form| %>
  < %= form.country_select :country %>
< % end %>

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

Then you can use be_valid assertions within your view tests in the following way:

describe "/index.html" do
  it "should be valid XHTML" do
    render 'home/index', :layout => true
    response.should be_valid_xhtml
  end
end

You can test your CSS the following way:

it "should be valid CSS" do
  css = File.read(File.join(RAILS_ROOT, %w(public stylesheets main.css)))
  css.should be_valid_css
end

I would like to find a way expand on this solution and be able to automatically run all CSS includes of a particular view through the checker, instead of having to manually specify each one.

I am also aware that there is another plugin that performs a similar function called assert_valid_asset - it might be more suited to those of you who use Test::Unit.

Analytics for your content contributors

What do you think about this idea - create a tool that makes it dead-easy for owners of user content-driven websites to drop-in the ability for users to access detailed analytics about the content that they have created.

I think that narcissistic social media addicts would love the ability to track how popular their postings are on their favourite sites.

Now that Google have released an API for Google Analytics, it would be relatively easy to piggyback off website owners’ existing GA accounts, slice the data up and present the analytics relevant to a particular page or section of the site that your current user is interested in.

Clicking on a ’stats’ link beside any of their posted content could take the user to a page full of funky flash charts illustrating their recent rise to fame. Widgets such as top 10 content and user popularity rankings could be derived from the analytics data and posted on home and profile pages. Users could be alerted by email or IM when their content has just gone viral, then transported to a real-time updating page where they could watch the action unfold.

I think that the sell to website owners would be pretty easy - they stand to increase user engagement and frequency of visits to their site, and they are giving their most fanatical users exactly what they want - which is always a smart thing to do.