Thanks to maniacmartin <martin@maniacmartin.com> for the patch on which this one is based. This closes Redmine bug #1209.