Author Archives: dev

Enabling repeating keys in Mac OS

By default, Mac apps don’t treat a key that’s being held down as a repeated keystroke (Terminal is the exception), which can be a hassle if you’re trying to use a vim emulation plugin.  To change this behaviour, you just need to enter this in Terminal:

defaults write -g ApplePressAndHoldEnabled 0

Delaying all your mail with DelayedJob

Messing with your mail in Rails can be a bit of a pain because of the way ActionMailer was designed. Delayed Job has something called a PerformableMailer, which helps you send out individual mails by calling delay on your mailers:
FriendMailer.delay.send_my_friend_a_message(@friend, 'hi')
But I wanted to send all my mail through delayed_job, and I didn’t want to be running around sticking .delay in everywhere. So here’s what I’ve got.
# lib/delayed/mail_delivery.rb
module Delayed
  class MailDelivery
    class << self
      attr_accessor :method
    def initialize(settings)
    def deliver!(mail)
      self.delay(:queue => 'mail').pass_to_delivery_method(mail.to_yaml)
    def pass_to_delivery_method(message_as_yaml)
      method = self.class.method
      klass = ActionMailer::Base.delivery_methods[method]
      settings = ActionMailer::Base.send(:"#{method.to_s}_settings")!(Mail::Message.from_yaml(message_as_yaml))

The idea is we’re going to ask Rails to use a custom delivery method called :delayed. What this delivery method does is serialize the mail message using Mail::Message’s to_yaml function, and enqueue it using Delayed Job. When the time comes to send the message, it deserializes it and passes it to the real delivery method.

Now all we have to do is wire it up. First we tell rails about our new delayed delivery method, and then we tell our delayed method that the real sending should be done with SMTP.

# config/initializers/delayed_mail.rb
require 'delayed/mail_delivery'
ActionMailer::Base.add_delivery_method :delayed, Delayed::MailDelivery
Delayed::MailDelivery.method = :smtp
The only thing left to do is to tell Rails to use this method 🙂
# config/application.rb
module FriendSend
  class Application < Rails::Application
    # mailer
    config.action_mailer.delivery_method = :delayed

Don’t forget to run the Delayed Job background process.

Note: this post assumes that you’re somewhat familiar with using delayed_job. If you’re not, check it out on asciicasts or github (for a more up-to-date version).

jQuery special key codes

From the jQuery source.
ALT: 18,
COMMA: 188,
DOWN: 40,
END: 35,
ENTER: 13,
HOME: 36,
LEFT: 37,
PAGE_UP: 33,
PERIOD: 190,
RIGHT: 39,
SHIFT: 16,
SPACE: 32,
TAB: 9,
UP: 38,

SPF Records and Spam Filters

If you have a legitimate, non-spammy reason to send bulk email to users (service updates, newsletters, etc), it’s important to make sure that your mail is getting out and not getting trapped by spam filters, or scuttled by problems with your domain’s reputation.

Check your SPF record

Sender Policy Framework records are a way for servers receiving email to see if an email coming from your domain is coming from a legitimate server – this prevents spammers from sending email and pretending that it’s from you. To see who your domain says is allowed to send email:

dig TXT
If you have an SPF record, you should see an entry which looks something like this:
;; ANSWER SECTION:     5   IN  TXT  "v=spf1 ~al

This means that the only servers that can send mail for are All others are flagged as a soft-fail, which means mail from them will be allowed, but flagged as suspicious. (More on SPF record syntax here.) The two important things to check in your SPF record are 1) your server is allowed to send email for your domain, and 2) everybody else’s server isn’t, or at least isn’t allowed to without careful examination (nb: don’t use +all).

Useful references:

Gmail’s bulk email guidelines
SPF record syntax
Scott Kitterman’s SPF testing tools

Using colortail with Rails logs

I recently switched over from using Mongrel as my default server when developing with Rails to using Unicorn, mostly because I wanted to see what alternatives to Passenger are out there, but also because I liked a lot of what the github guys had to say about it, and at least partially because I was curious why rails 3.1 was changing from commented-out-mongrel to commented-out-unicorn in the default Gemfile. In any case, when using ‘unicorn_rails’ as the server, it doesn’t automatically tail the log with pretty colors (well, pretty if you like cyan) in the same way that webrick and mongrel do. So (this is a long preamble), I found colortail.


Colortail is basically Linux’s tail with the ability to customize colors based on extended GNU regexes. It can be installed either with the colortail gem or using apt-get on Ubuntu/Debian. Simple enough, right? You set up your colors in a config file and read it using the -k parameter:

colortail -k /tools/colortail.rails -f log/development.log
Here’s my starter rails config. I’m sure it will grow quickly!
#define BLACK         "\033[0;30m"
#define RED           "\033[0;31m"
#define GREEN         "\033[0;32m"
#define YELLOW        "\033[0;33m"
#define BLUE          "\033[0;34m"
#define MAGENTA       "\033[0;35m"
#define CYAN          "\033[0;36m"
#define WHITE         "\033[0;37m"
#define BRIGHTBLACK   "\033[1;30m"
#define BRIGHTRED     "\033[1;31m"
#define BRIGHTGREEN   "\033[1;32m"
#define BRIGHTYELLOW  "\033[1;33m"
#define BRIGHTBLUE    "\033[1;34m"
#define BRIGHTMAGENTA "\033[1;35m"
#define BRIGHTCYAN    "\033[1;36m"
#define BRIGHTWHITE   "\033[1;37m"
COLOR green
 ^.*Started (.*) for.*at.*$
COLOR yellow
COLOR brightwhite
^Completed 1[0-9][0-9] .*$
^Completed 2[0-9][0-9] .*$
^.*Parameters: (.*)$
COLOR cyan
^Completed 3[0-9][0-9] .*$
^Completed 4[0-9][0-9] .*$
^Completed 5[0-9][0-9] .*$
Note that I pasted in those colors from a source file at the top just so I remember what my options are, they’re just comments.

Colors in git

Stolen from Garry Dolley at

Edit your gitconfig file!

vim ~/.gitconfig
Add these
  branch = auto
  diff = auto
  status = auto
[color "branch"]
  current = yellow reverse
  local = yellow 
  remote = green
[color "diff"]
  meta = yellow bold
  frag = magenta bold
  old = red bold
  new = green bold
[color "status"]
  added = yellow
  changed = green
  untracked = cyan
You won’t look back!

Adding a second Gmail account in Android

This has been bothering me for a while. Whenever I tried to connect secondary email accounts in Android (by pressing the Menu button -> Accounts -> Add Account), it would ask for my login and password, and then attempt to connect to the Gmail servers. And fail, with this error: “Can’t establish a reliable data connection with the server”.

I tried it over 3G, I tried it over wifi. Neither worked, so I gave it up as a connection problem and tried again a few hours/days/weeks later. Finally I realized that this must be some kind of Android Gmail bug and went looking for a solution/workaround. So here it is:

  1. Open up the YouTube app on your Android phone
  2. Press the menu button and sign into My Account with the credentials you want to add to Gmail.
  3. Once you’ve logged into YouTube successfully, close the app and open Gmail.
  4. Open Accounts from the menu – your account should now show up and you just have to fetch the mail.

Pretty simple, right? Android just needs to link the account to your phone somehow, and the YouTube login must use a different code path. This also works for Google Apps accounts, btw.

Source: comments.

A better workstation

The working world is becoming more aware that sitting all day might not be so great for your health. We’ve known for a while that desk work weakens your posture, causes tight hip flexors and hunched shoulders, and a craned neck, but now we’re talking heart disease and bowel cancer.

“The best posture probably is the next posture” (Galen Kranz)

The general conclusion, as far as I can tell, is that our bodies are not designed to be in any one static position for stretches of 8-10 hours a day. So the logical step for anybody who is working a desk job is to find an adjustable-height desk. Easy enough, right?

It turns out that a slab of wood that can be raised and lowered on demand is not so cheap to acquire. There are many different models by various “ergonomic solution” companies available, but they tend to clock in between $1000 and $1800. If your work is paying for your desk, or you’ve got $2000 to drop, you can stop reading and go buy yourself a new rig. But I’m looking for a more economical solution; is it really true that a table-top (~$100), a set of legs, and a lifting mechanism costs $1000 to put together? What’s the markup on these kinds of things?

The criteria:
  1. Lowered desk height must be maximum of 71cm (28”).
  2. Raised desk height must be minimum of 110cm (43”) – I wish I were a little shorter.
  3. It must be strong enough to hold a monitor, laptop and keyboard, plus miscellaneous desk items. Given that a 24” monitor weighs about 22lb, and a laptop another 5-6lbs, I figure it should hold 40lbs minimum to be safe.
  4. It must be able to be readjusted without taking everything off. No screws, bolts, etc.
  5. I’d prefer hand cranking to electrical because the less things there are to plug in, the better.
  6. Preference given to desks that are easy to disassemble and pack
  7. Preference given to desks with wheels
If you’re a little shorter, you might be in luck. There are several models similar to this that I’ve seen on different sites. They look a tad flimsy, and their maximum height is only 36”, but they’ll probably do the trick for some people.

Damage: $220

Ergotron has a lot of sit/stand solutions, and one in the right price range is this, which isn’t a desk exactly, but more of a keyboard/monitor mount with a neat little adjustable height arm. Unfortunately, it can only hold 31 lbs.

Damage: $400

I don’t have the time to go full DIY on this, but I thought some kind of adjustable work bench might work, especially if you slapped a small Ikea table surface on top. But I don’t feel too good about having to adjust that on the fly. I think I might end up with a pile of electronics in my lap (if I’m lucky).

Damage: $50 + table top

If you do have time and energy to go DIY, you can go for things like this crank mechanism at Lee Valley, or build something crazy using an automotive jack, as somebody at Metafilter suggested.

Damage: I’m guessing about $300 – $500

Another option is to use a musical keyboard stand like this Freedom X-Stand from SolidStand. I’m still looking into the weight rating but if it’ll hold some of the beefier keyboards, it will hold 40lbs of computer gear. The only thing about it is I don’t like having that X right in front of me, I like to put my legs under the desk without bumping into things.

Damage: $400

GeekDesk mini: this is getting back into the pricier range but it looks sturdy as hell, has wheels, and is nice looking to boot. A good look at it with pictures here.

Damage: $750

SolidStand Freedom Breakdown: Also on the pricier side, but this thing looks solid as a rock. If it can handle two keyboards being pounded on by a keyboard player, you don’t have to worry about anything coming off. It would need some kind of table-top to set the monitor on, but that wouldn’t be too much work. It also disassembles for easy transport by gigging musicians.

Damage: $700 + table top

Update: the Freedom X-Stand will hold 150lbs.

Adding welcome emails to dm-devise

I was looking for a way to have users greeted with a welcome email when they created accounts, and Devise doesn’t have a ‘welcomeable’ module. Initially I forked devise and started building one, but it’s actually so simple to add (at least with DataMapper) that I can see why there isn’t one included. So, here we go.

Subclass Devise’s mailer

We do this to keep the rest of this as parallel to Devise as possible.

class AppMailer < ::Devise::Mailer
  def welcome_message(resource)
    setup_mail(resource, :welcome_message)
And in config/initializers/devise.rb:
config.mailer = "AppMailer"

Add the template for your message

In app/views/devise/mailer/welcome_message.html.erb:

<p>Welcome <%= %>!</p>
Thanks for signing up for App.  
App is one of the greatest things you could sign up for. 
You won't regret it!

Add the title to the devise localization file

In config/devise/en.yml:

        subject: "Welcome!"

Connect the creation of a user to sending the mail

We’re going to use DataMapper’s CRUD hooks to do this. These are declared either before or after the CRUD operations. It used to be the case that dm hooks would work on any function at all, but that was deprecated a while back.

class User
  include DataMapper::Resource
  devise :database_authenticatable, :registerable,
            :recoverable, :rememberable, :trackable, :validatable
  after :create, :send_welcome_message
  def send_welcome_message
Now, because devise looks for the templates to render in the views directory, under the name of the mailer class, we need to move all our views from app/views/devise/mailer to app/views/app_mailer. Once that is done, everything should be up and running.