Configuring Spree Commerce to use the British Pound currency

I’ve been playing around with Spree for a couple of weeks and ran into a slightly confusing issue wrt. using a non-US Dollar currency, in my case British Pounds.

At the time of this writing, using Rails 3.1.x and Spree 0.70, the following steps will enable a ‘£’ (a pound sterling) sign to be displayed.

Add the I18n gem to your Gemfile and run bundle install:

gem 'rails-i18n'

Open a shell and cd into the config/locales folder of your Spree/Rails project. Download the British locale file:

wget https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/en-GB.yml

You’ll also need copy the contents of Spree’s British I18n file into the bottom of of your config/locales/en-GB.yml file.

Edit your config/application.rb and set your default locale to ‘British’:

config.i18n.default_locale = :"en-GB"

Create a new file in config/locales/en-GB_numbers.yml with the following contents:

---
en-GB:
number:
currency:
format:
format: "%u%n"
unit: "£"
precision: 2
separator: '.'
delimiter: ','

Spree uses the above file to set the currency settings. Be aware that Spree is currently a single currency application.

Bootstrapping a ‘deploy’ user with Capistrano on EC2

Amazon’s EC2 is rightly so the best thing since sliced bread. All of our hosted services at Vamosa run off EC2. Getting our Ubuntu instances provisioned these days is easily achieved using Capistrano, but when we were still get familiar with ‘cap’ it wasn’t always the case.

Amazon EC2 uses private/public keys files for root user authentication but you want to use those credentials as infrequently as possible. As RubyOnRails users, we are used to setting up a deploy user which we use to run Apache 2 and Phusion Passenger under. We use that same deploy user to connect to our github repositories and pull in code updates. All pretty much standard stuff.

When we using off-the-shelf Ubuntu 8.0.4 AMIs we couldn’t find any nice Capistrano recipes to setup that initial deploy user and enabling key-based login to that deploy user using our own personal keys.

The following recipe does just that:

  desc "uploads id_rsa.pub to the EC2 instance's deploy users authorized_keys2 file"
  task :bootstrap_deploy_user do
    system "ssh -i #{aws_private_key_path} root@#{domain} "groupadd admin""
    system "ssh -i #{aws_private_key_path} root@#{domain} "useradd -d /home/#{user} -s /bin/bash -m #{user}""
    system "ssh -i #{aws_private_key_path} root@#{domain} "echo #{user}:#{password} | chpasswd""
    system "ssh -i #{aws_private_key_path} root@#{domain} "usermod -a -G admin deploy""
    system "ssh -i #{aws_private_key_path} root@#{domain} "mkdir /home/#{user}/.ssh""
    for key in ssh_options[:keys]
      system "cat  #{key}.pub | ssh -i #{aws_private_key_path} root@#{domain} "cat >> /home/#{user}/.ssh/authorized_keys2""
    end
    system "ssh -i #{aws_private_key_path} root@#{domain} "chown -R #{user}:#{user} /home/#{user}/.ssh""
    system "scp -i #{aws_private_key_path} config/deploy_sudoers root@#{domain}:/etc/sudoers"
  end

You’ll need to setup Capistrano variables as you can see to make this work. My .caprc file contains the following definition:

set :aws_private_key_path, "/Users/ijonas/.ec2/ec2keypair.pem"

Our config/deploy.rb contains the following variables:

set :use_sudo, false
set :user, 'deploy'
set :password, 'xxxxx'
set :application, "yyyyy.vamosa.com"
set :domain, "yyyyy.vamosa.com"
set :deploy_to, "/var/www/apps/#{application}"

The approach is a little bit ‘hackish’ but it works for EC2, and keeps your Capistrano setup as close as possible to the best-practice.

Running MySQL and RubyOnRails on Windows

rails.jpgIf you’re trying to get RubyOnRails connected to MySQL on Windows Vista as we have done recently at Vamosa, then keep in mind that the current latest versions of MySQL and the MySQL gem are incompatible with each other.

At the time of writing, the current versions of MySQL is 5.1 and the MySQL gem is currently at version 2.7.3.

We were unable to get this combination to work together and ended up reverting to MySQL 5.0 and MySQL gem 2.7.1.

gem install -v "2.7.1" mysql

I don’t think you’re missing anything substantial from either MySQL 5.1 or the 2.7.3 gem.

Quick and dirty RubyAndRails

First of all, an admission… My name is Ijonas, I used to be a Java programmer. I’ve been clean for a year and a half. So much for my “Java Anonymous” meeting.

In that period I’ve Ruby Crystalbeen experimenting and pondering about Ruby and of course RubyOnRails. During such ponderings I’ve pondered why so many experienced Java programmers are jumping ship onto duck-typed languaged such as Ruby and Python (of which I’m a massive fan).

I’m going to write a bunch of posts about Ruby and the impact on solving certain class of computing science problems, that I believe are now more readily or more elegantly solvable – hence my interest in the Ruby language.

But first I thought I’d share with you, what I think is possibly the easiest, quickest, and dirtiest way of getting a taste of Ruby and Rails.

First of all… tooling.

Now… I wouldn’t bother buying any books until later…  I started off with books, but I find screencasts far better in conveying relevant & concise information.

With that in mind I recommend you purchase the following screencasts:

Both are from PeepCode, last about an hour, and cost $9 each. Well worth it. I recommend starting with Rails as opposed to “Pure Ruby”, because that way you get to see some of Ruby’s magic applied in the familiar setting of building a webapp. Coming from Java, it’ll be quiet a refreshing experience – no more xml deployment descriptors.

Having watched both screencasts, you can start building Rails websites with some competence. However, you’ll run into quiet a few “How do I do that ?” questions early on, such as “How do I authenicate visitors to my site?”. And before you head off to Amazon or your local bookstore to buy a Rails Cookbook… I recommend you take a look at Railscasts, which is a free (donations welcome) video podcast providing really helpful solutions to common probems.

Next… you may want to take a look at how Ruby works underneath the covers and how some of its magic manifests itself in such advancements as domain-specific languages (DSL).

Having spent many years programming Java and C#, I found that the recent advancements in test-driven development, mock objects, continuous integration are all worthy pursuits but often these advancements fought with the friction caused by stronly typed languages, i.e. all that test code I was writing was pretty verbose and clunky. Ruby’s take on the aforementioned pursuits and approaches, is positively refreshing and enjoyable.

If you’ve ever questioned why the hell you should write a unit test before the implementation itself, take a look at RSpec. RSpec is another DSL, like Rails is a DSL, this time to facilitate the writing of specification, i.e. the expected behaviour of a software programme. First you describe how your app should behave, then you write the behaviour… It takes a few moments to get your head around why you would want to do so… but then the penny drops… and once again its magical! For a great intro into RSpec I can recommend another Peepcode screencast:

Now if you’re anything like me… Java programmer or C# programmer… many years writing Enterprise applications… lots of XML… You probably know most of what there is to know about OO programming. Classes, interfaces, and object instances are your staple day-in day-out building blocks. You know about inner classes and anonymous methods but only use those if the moon is particularly blue.

Ruby’s take on “OO” borrows from many different OO languages and to fully appreciate some of the magic and how to cast your own Ruby spells, you need to look at Ruby’s object-oriented basics. To that end, I recommend downloading all five episodes (2.5 hours) of:

from the guys at Pragmantic Bookshelf, which will set you back a grant total of $25.

Consuming all this information will take some time… If you’ve got a “day job” that needs to pay the bills… you’ll probably take a week or two to really sift through and appreciate what amounts to a couple of days of screencasts.

All the screencasts I’ve mentioned are of the highest quality and provide amazing value for money… Far more so than the books I’ve purchased.