File versioning in Ruby on Rails with Paperclip & acts_as_versioned

This short tutorial shows you how to manage file versioning in Ruby on Rails by making Paperclip falling in love with acts_as_versioned. Paperclip & acts_as_versioned are plug-ins for Ruby on Rails: Paperclip manages file upload, acts_as_versioned enables models versioning.

Patch Paperclip

I patched Paperclip to keep old files when a new revision is saved. A fork of Paperclip adding the option keep_old_files to make Paperclip working with acts_as_versioned is available here: http://github.com/pcreux/paperclip/tree/master. You can install it running:

script/plugin install pcreux_paperclip

Update your Paperclip + acts_as_versioned model

Just three things to do:

  1. update the url & path to store your files by ‘version’
  2. set the option keep_old_files to true when a new version get saved
  3. add the interpolation of :version
  1. class Document < ActiveRecord::Base  
  2.   has_attached_file :attachment,  
  3.                     :url => "/system/attachments/:id/:version/:style/:basename.:extension",  
  4.                     :path => ":rails_root/public/system/attachments/:id/:version/:style/:basename.:extension",  
  5.                     :keep_old_files => :version_condition_met?  
  6.                       
  7.   acts_as_versioned  
  8.     
  9.   Paperclip.interpolates :version do |attachement, style|  
  10.     attachement.instance.version.to_s  
  11.   end  
  12. end  

I hope it helps. :)

Post to Twitter

. Bookmark the permalink. Both comments and trackbacks are currently closed.
  • Hi, my name is Philippe Creux.

    This blog is about Agile project management, Ruby programming and other cool things. I haven't published anything here since 2011. Find the latest and greatest on pcreux.com!