All posts filed under “programming

Rspec + Devise user and current_user different

If you follow the Devise Wiki on setting up Rspec and Devise you may end up wanting to set an expecatation on the currently signed in user.

Don’t fall into the trap of setting it on the original user model as your specs will still fail:

user.should_receive(:blah).once # Won't work

Rather, you should set it on the controller’s current_user:

subject.current_user.should_receive(:blah).once # Will work!

Quickly close database connections on the command line

One slightly annoying thing about Postgres over MySQL is that trying to reset a database with rake db:migrate:reset fails if there are connected clients and as I use Pow for development, killing the process doesn’t always free up the connections.

To get around this you can quickly kill all connections with this command:


I thought I’d have a look at the SpotifyAPI to see what it’s like. Pretty nice that everything is just HTML + JS. I knocked up a small app which simply provides search links for the currently playing track. It also changes as the player’s track changes.

The source is on Github.

Using blocks in Ruby for lazy evaluation

I’ve been working on a templating engine recently and had the need to substitute some variables on the page with the result of an slightly expensive remote API call. The first iteration was just a simple replace on the page:

This “worked” but the problem is the call would be made even if the variable that needed replacing wasn’t on the page. The easy fix is to check it exists before making the substitution:

While this works, once you have a load of results you want to replace there ends up being a lot of duplication, and it’s easy to check the wrong variable compared to what you want to replace. One way to solve it is by passing the api call as a block to the substitution method. This way we can tidy up the code:

The “Bob bug” Syndrome

Some bosses make the mistake of thinking that if the result of someone’s work is easy to use, the work itself must be easy.

A guy goes in to consult for a company and runs across a bug. He raises the issue and the boss says “Oh boy – another ‘Bob bug’ – Bob created a lot of bugs.” (Bob has moved on by now). After the guy was there for a while he comes to realise that Bob wrote most of the code while the rest of the staff wrote very little. The perception that Bob created a lot of bugs was technically correct, but an analysis of bugs per line of code written show that Bob actually made fewer mistakes than his co-workers, but since he generated the majority of the app, he had the majority of the bugs.

Source: Reddit

PDFKit not displaying £ (pound) signs properly

I’m generating PDFs with PDFkit/wkhtmltopdf on a current project. While the output is fine in the browser, £ (pound) signs in the generated PDF were showing up incorrectly as:

The fix was relatively easy as PDFs have their own view layout, so all I had to do was add an extra meta tag to the head:

Devise Omniauth Dynamic Providers

My current project requires different users to have different scopes for their Facebook authentication. Warden supports dynamic providers out of the box. To get it working with Devise was pretty easy, just a couple of minor changes were needed especially to support Facebook.

First up was creating the new route. As I’m using a controller called Omniauth and not Session, the :to attribute is different. The other thing to note is I’m matching /users/auth/facebook/setup, not /auth/facebook/setup like in the Warden documentation.


Next we create the new setup action for Warden to call. The docs use consumer_key and consumer_secret, but facebook expects client_id and client_secret, so be sure to use those instead. This is where I make changes to the scope based on the current user if they are signed in or not. So a user can re-authenticate with Facebook to get more permissions for their account if they wish.


Finally change the devise.rb initializer to support the new setup.


Restart your application and you should be good to go.

NERDTree directory colours

I just realised that my NERDTree directory colour wasn’t inline with my Vim theme, and I couldn’t help but be bugged by it. Seems it’s a simple change. To change colour you need to use the treeDir highlight group.

To keep it synced with the ir_black theme I use, I just added the following to my colour scheme file.

Accessing values in your plist file from your iPhone/iPad app

Recently I’ve been coding some iPhone and iPad apps with multiple targets, all sharing the same codebase. Previously I had been using Preprocessor Macros to get the job done, but that soon turns messy and hard to maintain. I’ve since moved to just storing target specific values in each apps related plist file. Retrieving a value is as simple as:

nginx SSL PEM_read_bio:bad end line

Installing the intermediate certificates on our server gave me the following error while trying to start up nginx again:

The end of the file looked fine, but the problem was half way through it. Somehow the concatenation had joined two lines together:

This just needed a line break to fix:

Paperclip S3 expiring_url with styles and SSL

A current requirement is that assets uploaded to Amazon S3 must be accessed via SSL and use an expiring URL. Using an expiring url with Paperclip is extremely easy.

The problem is expiring_url doesn’t let you choose a style and doesn’t use https, which breaks our SSL site. To add the behaviour was an easy “hack”. I added the file /config/initializers/paperclip.rb with the following code:

Now calling my expiring image URL with a style and over https is just:

‘FBML’ [undefined] is not an object

First let me state how much I dislike developing for the Facebook platform. It’s just one constant headache. Things are always changing and breaking things. Anyways, I’ve been banging my head against the wall all day trying to get some FBJS running on a canvas application that’s embedded on a Fan Page tab. No matter what I did, I kept getting the error:

Even using Facebook’s own FBJS examples left my tab in a hung state, with the spinner just continuing forever. After hours of playing around, the problem is that I’m trying to use Javascript one page deep into the application (The landing page itself has no FBJS). Once I added an opening and closing script tag to the landing page, my FBJS woes vanished.

Why Code Quality Matters

Recently the discussion of code quality has come up with a couple of different people. With the main question:

“If the app does what it’s supposed to do, why does code quality matter?”

In one instance an application that was bringing in over a £1m in revenues a month had one of the worst code bases a friend had ever seen. And I totally agree that there is no correlation between code quality and the earnings/growth potential of an application. Then a few days ago I was reading the brilliant Zed Shaw article “Products For People Who Make Products For People” which completely crystalised why code quality is important:

“… the frontend product is what brings your revenue stream in, but your backend operations quality is what keeps your costs down as you grow. If your backend costs get out of control because of technical debt then you won’t make a profit, or someone who can keep them down will just copy you and wipe you out with less.”

Perfect! That’s it exactly. Code quality matters as that’s what allows you to grow the system beyond your v1. If you never plan/or want to get past v1, then perhaps you can get away with being a lot more lieniet in your “quality”. I know that’s what I do, if it’s a quick app then normally I have only the most basic of tests and cut corners where I wouldn’t normally. Maybe in a sense, code quality is a reflection of technical debt. The lower the quality, the higher the debt and sooner or later you’re going to have to pay that back (Unless you’re fortunate enough to close up shop before that time comes). I’ve worked on projects where the technical debt is so high that you’re paralysed and just can’t do anything. Simple changes become huge problems as you’ve been painted into a corner, and the fear of breaking something is so huge that new feature requests are shied away from.

If you want/expect to be able to grow your business then keep the bar high. Code quality matters a hell of a lot.

Using SCSS with Webby

To get webby using HAML/SASS’ SCSS rendering engine I added the following to my Sitefile. No doubt there’s a cleaner and quicker way of getting it working, but this works.

Rspec/Cucumber Autotest Loop

Autotest would constantly run my tests even though I hadn’t modified anything. Which made it impossible to actually use. Creating the following in a file called .autotest in my application’s root solved the problem for me. This is on Rails 3 RC, RSpec 2 beta 19 and Cucumber 0.8.5.