My computer loves autotest-fsevent

Posted 30 days ago

I’m a big fan of autotest for testing, unfortunately it does stress my poor MacBook Pro and makes the fan go berserk if running anything other than the most simple of test suites. This is due to autotest having to check each file in your project for changes.

No more will autotest stress out my mac, autotest-fsevent is a great gem that uses OS X’s FSEvent system to be notified when files have changed rather than having to poll each file. You need mac OS X 10.5 or later to take advantage of FSEvent.

The other nice thing autotest-fsevent does is take care of all the .autotest config options, I managed to delete my entire config file which I’ve been tweaking for as long as I can remember trying to get the perfect setup.

I can now run even the most demanding of test suites and my computer barely breaks a sweat. Thanks bitcetera, my computer ♥’s you.

DRYing up multiple user contexts with shoulda macros

Posted 43 days ago

Today I’ve been writing tests for a legacy Rails application I inherited recently. The application has several user roles, each role having varying permissions. To deal with this nicely I setup shoulda macro’s to create contexts for each of the user roles, public user, standard user, admin user etc. then in my tests I could write…

public_context do

  context "on GET to :index" do
    setup do
      get :index
    end

    should_redirect_to("root url") { root_url }
  end
end

signed_in_user_context do

  context "on GET to :index" do
    setup do
      get :index
    end

    should_redirect_to("user url") { user_url }
  end
end

This is pretty standard practice now and something I picked up from looking at the code produced by the guys at Thought Bot. While working on the test suite it became apparent many of the methods behaved in the same way for multiple user roles. I wanted to come up with a way to run a group of tests under multiple user roles without having to duplicate any code. Shoulda macros to the rescue again! After creating another macro to deal with multiple contexts I can write my tests like this…

multiple_contexts ['public_context', 'signed_in_user_context'] do

  context "on GET to :show" do
    setup do
      @advert = Factory(:advert)
      get :show, :id => @advert.to_param
    end

    should_render_with_layout :application
    should_render_template :show
    should_not_set_the_flash
    should_assign_to( :advert ) { @advert }  
    should_respond_with :success
  end
end

And the shoulda macro code itself…

def multiple_contexts(contexts, &blk)
  contexts.each { |context|
    send(context, &blk) if respond_to?(context)
  }
end

def public_context(&blk)
  context "The public" do
    setup { sign_out }
    merge_block(&blk)
  end
end

def signed_in_user_context(&blk)
  context "A signed in user" do
    setup do
      @user = Factory(:user)
      sign_in_as @user
    end
    merge_block(&blk)
  end
end

Running JSLint through textmate

Posted 67 days ago

I’ve been developing with JavaScript a lot recently and found JSLint to be a rather useful tool for picking up syntax errors. Being primarily a ruby developer I have a nasty habit of omitting semicolons from the end of lines.

I found an article on running JSLint through a textmate command but found Ryan’s script didn’t quite work with the output from the version of JSLint I’m using so I thought I’d share my modifications.

#!/usr/bin/env ruby
require 'cgi'

lint = `/usr/bin/java -jar ~/Library/JSLint/js.jar ~/Library/JSLint/jslint.js "$TM_FILEPATH"`

lint.gsub!(/(line \d+ character \d+:) ([^\n]+)\n([^\n]+)/m) do
  "<p><strong>#{CGI.escapeHTML($1)}</strong> #{CGI.escapeHTML($2)}</p>" <<
    ($3 ? "<pre>#{CGI.escapeHTML($3)}</pre>" : '')
end

lint.gsub!(/^(jslint:.*at )/, '')

print <<HTML
<!doctype>
<html>
<head>
  <style type="text/css">
    p { margin-bottom: 0; }
    pre {
      background: #f5f5f5;
      border: 1px solid #cfcfcf;
      font-size: 12px;
      margin-top: 2px;
      padding: 2px 4px;
    }
  </style>
</head>
<body>
  #{lint}
</body>
</html>
HTML

New bars and rubber for the steed

Posted 193 days ago

Lemond Fillmore with bull horn bars and orange rubber

A couple of weeks ago I had to replace the old tires on my Fillmore as I’d worn through the green ones that came with teh bike. Took a bit of getting used to the orange but it’s definitely growing on me.

This last weekend I managed a puncture the inner tube while pulling the pump off which required a trip to Strada Cycles. I ended up replacing the bars and break callipers so it turned out to be a much more expensive trip than I was expecting. Just shy of £100 got me a new inner tube and I was back on the road again.

javascript_auto_include plugin updated

Posted 213 days ago

I’ve updated the javascript_auto_include plugin to include changes from abradburne and jimp79. Thanks to these guys the plugin now supports nested controllers so you can add javascript to a paths like admin/users and admin/users/show/1 etc.

MySQL order by rand on large data sets

Posted 214 days ago

More of a note to self than a real post. Great solution to extremely slow order by rand() queries when using large data sets by Paperplanes. Using this technique a query previously taking nearly 0.2 seconds was reduced to around 0.01 seconds.

Running Merb on a shared host without compiler permissions

Posted 224 days ago

I ran into a problem earlier today when testing a Merb application in a shared hosting environment running Phusion Passenger. Like most shared hosts access to compilers is not allowed for obvious security reasons, this causes a problem when trying to run a Merb app. Merb makes use of RubyInline which needs to compile a C application in your home folder before it can run so when the user you’re running Merb under doesn’t have permission to run C compilers you’ll get an error similar to:

sh: /usr/bin/gcc: Permission denied

The way to get around this is to have a privileged user compile the C application for you, then copy it to your users home folder. The easiest way to do this is to generate and run a Merb app as the root user:

merb-gen app tmp-merb
merb
Ctrl+C

Then copy the RubyInline C application to the users home folder:

cp -R /root/.ruby_inline /home/<username>

Make sure the file ownership is set correctly:

chown -R <username>:<username> /home/<username>/.ruby_inline

After this you should be able to run Merb as expected. If you are still getting errors after doing this relating to file permissions you’ll need to combine this fix with from Vignet and put the .ruby_inline folder in a place accessible to both the apache user and the user running the Merb app, I’ve not tested this but it should work.

Updating RubyGems on OSX

Posted 259 days ago

Slightly late to the show I updated to RubyGems 1.3.0 today, the upgrade isn’t as straight forward as usual on OSX 10.5 and requires a little extra work.

wincent.com posted up some instructions on how to upgrade to RubyGems 1.3.0 which are useful for anyone having issues.

Update: It looks like this issue is not specific to OSX, I was updating a couple of Linux servers and had to follow the same procedure.

Colour profiles in Photoshop for web design

Posted 280 days ago

While getting the photo of my new business card ready for the web I fired up Photoshop on the laptop. I soon realised I’d not used Photoshop on the laptop before, when I hit the save for web button my lovely photo changed into something hideously washed out. As someone who rarely uses Photoshop any more I always forget how to set-up colour profiles and remember spending quite some time doing this on the desktop machine.

To my amazement a quick google search and a couple of minutes later I was all set-up and exported my photo exactly how I wanted it. Thanks Usability Post for your amazingly simple and accurate guide to setting up colour profiles for web design.

My new business cards have arrived

Posted 280 days ago

I’ve been making do with some terrible old business card for the last few years and thought it was about time for some nice new ones. I wanted to go for something nice, clean and simple so designed them with just a little type, a bit of custom kerning then shot them off to the nice little moo bot to print. I have to say I do quite like the playful tone and nice little touches moo uses in their communications, it makes you feel happy.

Jamie Dyer business card