Re-saving ActiveRecord Objects Rake Task

Posted by dave
on Thursday, October 29

A quick rake task to allow you to quickly re-save all objects in a model class (maybe you’ve added some new code in an ActiveRecord filter and need to run the code on existing persisted objects).

namespace :db do
  desc "Resave all objects in a model class." 
  task :resave_objects => :environment do
    raise 'USAGE: rake db:resave_objects MODEL=MyModelName' if ENV["MODEL"].nil?
    unsaved = []
    model_class.find(:all).each do |o|
      if o.save
        puts "#{model_class.to_s}(#{o.id}) re-saved\"" 
      else
        unsaved << o
      end
    end
    if unsaved.length > 0
      puts "Some objects failed to save.  Here are their IDs:" 
      unsaved.each do |o|
        puts "#{o.id} : #{o.errors.full_messages}" 
      end
    end
  end
end

def model_class
  if (ENV["MODEL"].split('::').size > 1)
    ENV["MODEL"].split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
  else
    Object.const_get(ENV["MODEL"])
  end
end

BreadMachine: a 3-D Secure capable XPay gem for Ruby

Posted by dave
on Wednesday, October 07

We’ve currently got a requirement to integrate credit card payment processing in a way where

  • we can deal with some fairly strange, long-running business logic (payments that can take a month before they’re settled) and
  • we don’t really want to store full credit card info for security and liability reasons.

As both Dan and Matt have had good experiences in the past when working with SecureTrading, and they can handle our strange payment flows without any trouble, we’ve decided to go ahead and write a gem to integrate our app with SecureTrading’s XPay gateway.

Our code could only charitably be called “pre-alpha” in its current form, but we have successfully run some transactions through the test gateway. 3-D Secure verification, beloved by application developers everywhere, works in our tests – our app shoots card info at BreadMachine, which does the 3-D Secure enrollment check. Based on what BreadMachine tells it, our app then either does a standard auth check for funds, or redirects to the acquiring bank’s 3D-Secure verification page. The user then punches in their info, and get redirected back to our app, which either does a 3-D Secure auth request for funds or tells the user to get lost depending on the results of the 3-D Secure verification check.

The whole process is a huge hassle from a development point of view, but it’s required for certain cards which see widespread use in the UK (such as Maestro), and may be required by many more acquiring banks in future despite the many criticisms of the system. As far as we can tell, we’re the first ones to attempt to deal with 3-D Secure in a rubygem format. ActiveMerchant don’t do it, and given the much more ambitious nature of their multi-gateway code, that’s probably a smart choice (there is a fork available which adds some 3-D Secure integration, though). However, if you’re using SecureTrading and need to do 3-D Secure (or regular) auth, you can

gem install breadmachine

or fork the code on Github.

The bulk of the code was written by Matt. Currently there are no plans to turn this into a multi-gateway gem, a la ActiveMerchant, but I guess it really depends on how forky people want to get with the code.

Gedit On Rails

Posted by dave
on Thursday, October 01

As part of a general quest to slim down my programming environment (driven largely by the purchase of a Samsung NC-10 netbook and a love of programming on trains), I’ve switched to gedit as my main editor. It’s been hugely enjoyable to code in a lightweight environment – and the gedit-mate git repo in combination with apt-get install gedit-plugins makes it easy to set up a decent Ruby environment.

The only small problem I’ve had with the gedit-mate installer was that syntax highlighting wasn’t working for me out of the box. To fix it, I’ve forked the github repo and pushed a commit that installs the ruby_on_rails.lang file so that everything works properly on Ubuntu 9.04.

Patching the QCT Commit Tool

Posted by dave
on Thursday, October 01

The qct commit tool is a very nice lightweight svn client, but in the standard Ubuntu 9.04 repos it suffers from one small flaw: it doesn’t actually work. This patch (stolen from upstream) gets it back in working order:

for line in recs:
   if len(line) < 7: # <== add that
       continue # <== and that
   status = line[0]
   fname = line[7:]

Drop those two changed lines into /usr/share/python-support/qct/qctlib/vcs/svn.py after line 93 and you’ll have a nice lightweight svn commit tool.