Paperclip and MongoMapper

I started a new project recently that deals with a lot of imported data, so it seemed like a good excuse to try out MongoDB and MongoMapper. The imported data also has a associated images, so I wanted to get the Paperclip plugin working with my Mongo-based models. Fortunately for me, it actually didn't take too much work to pull out the ActiveRecord-specific bits. Here's what I did...

The first hurdle was the has_attached_file method. This class method sets up some callbacks, defines a few instance methods, and sets up some validations. Since classes that include MongoMapper::Document don't have the same validation code that classes descended from ActiveRecord::Base do, I had to tweak how the validations are set up.

The next problem was with one of the interpolations. I always like to use the id_partition interpolation, but the default Paperclip code assumes the id of the instance to which the file is attached is an integer. MongoDB uses alphanumeric strings as ids, though, so I had to check for that to determine which approach to take to create a partitioned path.

Here is the contents of a file that I put in config/initalizers/paperclip.rb to override the bits that needed to be changed:

Comments