I use this Capistrano recipe at work to send a broadcast to the office when I deploy an update. We use Jabber, and have a broadcast user that forwards messages to every user.

Copy jabber.yml to config/jabber.yml. Edit the details in it:

username: The Jabber account that will be **SENDING** alerts
          to the broadcast account

password: The password for the above Jabber account

broadcast: The Jabber broadcast account configured to forward
           messages to all users

Drop jabber.rb to config/deploy/jabber.rb. Add load "config/deploy/jabber.rb" to config/deploy.rb.

When you deploy and Capistrano hits the web:disable and web:enable tasks, a message will be sent to your users indicating the system is down. You can customize this message like so:

JABBER_REASON='a database migration' JABBER_DOWNTIME='10 minutes' cap deploy
JABBER_REASON='everything is broken' JABBER_DOWNTIME='a long time' cap deploy:web:disable

I extracted a few pieces of code into Rails Plugins.

  • Cars: Helpers for populating dropdowns with automobile makes and models
  • Host Router: Allows creation of routes based on hostname or port
  • Multisite: Provides site-specific view paths
  • Remote Database: Helpers for working with databases on multiple hosts
  • Fraud Guardian: Helpers for generating fraud reports via FraudGuardian

Nothing spectacular, but maybe they’ll help someone else.

I needed HTTP basic authentication for a Sinatra app running as a Rails Metal. I have two apps, one running Clearance and another running Authlogic.

Using the info from my previous post, I wrote a small Sinatra plugin that works with both Clearance and Authlogic.

Throw the file above in lib/sinatra_authentication.rb. Then, your metal, app/metals/api.rb would look something like this:

Authlogic doesn’t have an authenticate method, so I added this to app/models/user.rb:

Hope it helps someone else.

Recently, I needed to write an API to work with an iPhone application. I used Clearance for authentication. Unfortunately, it doesn’t support HTTP Basic Authentication out of the box, which made it difficult to use in an API.

I found this issue with a patch that worked. However, the Thoughtbot guys said that Rack::Auth::Basic should be used instead. No examples were provided.

I tried for a few days to get things to work with Rack and ended up using that patch.

Today I decided to take another look at this. I found a cached slideshow on Google that had the info I needed to make an API using a Sinatra app as a Rails Metal.

This is basically how I got HTTP Basic Auth working with Clearance:

Hope it helps someone else having this problem.

I work on a few apps that deal with phone numbers. I usually store these as bigint(11) in MySQL. My migrations always seemed to ignore this.

t.integer :phone_number, :limit => 11

Tonight, I found the following method in ActiveRecord that explains this weirdness:

# Maps logical Rails types to MySQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
  return super unless type.to_s == 'integer'

  case limit
  when 1; 'tinyint'
  when 2; 'smallint'
  when 3; 'mediumint'
  when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
  when 5..8; 'bigint'
  else raise(ActiveRecordError, "No integer type has byte size #{limit}")
  end
end

To fix this, try t.integer :phone_number, :limit => 5 instead.

It seems that (at least with the MySQL adapter), limit actually specifies the number of bytes that are using in the column, rather than the display limit.

Hope that helps someone else.

Redmine has an awesome feature built in that lets you check an email account to import new tickets into your project.

Unfortunately, I didn’t see a clear way to do this without exposing your IMAP username/password to your cron.logs. I wrote a small script that reads the username/password/host from your config/email.yml file.

NOTE If you are using a different email account than the one in config/email.yml then you should create config/imap.yml and include that in this script. ** DO NOT ** add extra fields to config/email.yml or you’ll break things.