Mario's Blog

The journal of a software developer who has a fondness of cheese

Rails HTTP Streaming

Rails 3.1 introduced HTTP streaming, a feature that can speed up the perceived page load times for your users. This feature doesn’t actually speed up any of your code, but rather starts sending the response before the entire page is rendered.

What this means is that your app can start sending out parts of the page that do not require any heavy calculation almost immediately. This really helps if you have some complex actions which take a long time to completely render.

The best part of this feature is that by rendering the top part of the page quickly, the users browser can begin to download assets such as CSS straight away, while your application works on getting the rest of the page out the door. This can speed up end user page load times dramatically.

OSX Lion Colour Sampling Using DigitalColor Meter

Previously when I was running Mac OSX Snow Leopard I would use the built in tool ‘DigitalColor Meter’ to grab RGB values from my screen. Using the tool is is very quick to grab colours from websites, photoshop files, anything really.

When I upgraded to OSX Lion and opened the colour meter for the first time I realised that it had changed and I could no longer quickly grab colour values to use. I realised though by changing one of the menu settings I could get back the previous functionality; here’s how:

Open Sourcing Like a Pro

For a little while I haven’t had any spare time to do any original open source work. I’d still been contributing to Locomotive CMS, fixing bugs and generally just helping out, but I haven’t had the chance to work on anything new. Thankfully for a client project of mine I had the chance to create a new gem.

So, yesterday at work I released the activemerchant-bpoint gem. A small plugin for Active Merchant that provides a billing gateway for the commonwealth bank’s BPOINT merchant gateway service. It’s really great to work in an environment that encourages open source work. Hopefully this is just the start and I hope in the future that i’ll be able to open source much more of the work I do.

Ruby 1.9.3rc With RVM

The Ruby MRI 1.9.3 RC1 has just been released and I wanted to try it out, the latest version of rvm only lists the 1.9.3 preview1 and 1.9.3-head which doesn’t appear to work correctly. Here’s how I managed to get 1.9.3 installed using rvm:

1
2
3
4
 rvm install ruby-1.9.3-tv1_9_3_rc1 --with-libyaml-dir=$HOME/.rvm/usr
 rvm alias create 1.9.3-rc ruby-1.9.3-tv1_9_3_rc1

 rvm use 1.9.3-rc

Ruby 1.9.3 works quite well (all of my tests still seem to be green), although there are a few small issues at the moment. For example ruby-debug19 doesn’t currently install. Running a quick test shows that 1.9.3 does indeed seem to load up a rails environment much quicker, here is a comparison of running rspec and cucumber tests for a rails 3.1 project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 Using /Users/mario/.rvm/gems/ruby-1.9.3-tv1_9_3_rc1

 time rspec spec
 Finished in 22.81 seconds

 real  0m28.469s
 user  0m11.000s
 sys   0m0.895s

 time cucumber
 0m4.816s

 real  0m12.955s
 user  0m10.579s
 sys   0m0.701s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 Using /Users/mario/.rvm/gems/ruby-1.9.2-p290

 time rspec spec
 Finished in 25.69 seconds

 real  0m43.826s
 user  0m18.193s
 sys   0m1.367s

 time cucumber
 0m6.007s

 real  0m24.559s
 user  0m19.324s
 sys   0m1.200s

As you can see the actual tests themselves ran faster but not by much, the total execution time went down a lot though as 1.9.3 has some new fixes to dramatically speed up the time it takes to require libraries.

Update

1.9.3rc1 is now available directly through RVM. Simply run:

1
2
3
 rvm get head
 rvm reload
 rvm install 1.9.3

Locomotive CMS

A few months ago a friend and coworker of mine pointed me towards Locomotive CMS, a content management system built on ruby on rails which has multi site support out of the box and has a really pretty admin interface.

I have been helping out Locomotive CMS for a while now in my spare time. I’ve been fixing bugs here and there and helping out users that require assistance. A couple of days ago I was appointed as one of the maintainers for the project, YAY.

Locomotive is really neat, you should totally check it out!

Squeel

I’ve been using a really neat gem lately called Squeel, it provides extra support for active record to do lots of neat things. There’s heaps of examples at the homepage.

Today I managed to get a patch into squeel (YAY!). I found a bug which was only occurring on ruby 1.9.2 sporadically. I wrote a failing spec and the author Ernie created a fix on the very same day.

Using Arel Directly in Rails

Today I was creating a model method which would return some associated objects. It wasn’t hugely complex but I basically needed to match an sql string which looked something like this:

1
2
 SELECT * FROM table WHERE column_one = something AND column_two = something
 OR column_three = something AND column_four = something

After reading some arel documentation, I came up this:

1
2
3
 version  = Version.arel_table
 versions = Version.where(version[:c1].eq(something).and(version[:c2].eq
 (something)).or(version[:c3].eq(something).and(version[:c4].eq(something))))

If this is a bit too fiddly for you then there are some nice gems that do this for you, such as Squeel.