Paperclip without Form Upload

I have recently been building a Rails application for which I need to generate a QR code for one of my models that has a URL.  Already using the excellent QREncoder library, I needed to attach the generated PNG to my Rails model.  Being that we are already using Paperclip to manage file attachments, we wanted to utilize the same functionality here.

attr_accessible :qrcode
has_attached_file :qrcode

def after_create
  qr = QREncoder.encode(self.url)
  temp_file = Tempfile.new(['qr', '.png'], "#{Rails.root.to_s}/tmp/")

  begin
    qr.save_png(temp_file.path)
    self.qrcode = File.open(temp_file.path)
    self.save
  ensure
    temp_file.close
    temp_file.unlink # cleanup the temp file...
  end
end

In the code above, after calling a few Paperclip setup methods, we start by encoding the QR code for the desired URL stored in the model.  We then create a temporary PNG file to which we write the QR code via the QREncoder library (save_png). Paperclip then expects a File be presented to be attached, so we open the temporary file via File.open and save the model. Before we go, we make sure to close and remove (unlink) the temporary file for cleanliness.

Hope this is helpful to others, took a bit of playing.

Advertisements

UDID without iTunes

Quick Tip:

It’s possible to find the UDID of an iOS device without iTunes (with the device plugged in to your Mac):

  • Launch “System Information” via Spotlight”
  • Select “USB” from the Hardware section on the left
  • Expand the various sections to find the device and select it
  • In the bottom panel, the “Serial Number” value is the UDID