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:
The call to validates_each ought to be smarter about whether it's dealing with AR's validations or whether it's dealing with John Nunemaker's Validatable gem, but I'll leave that for another day.
Finally, Paperclip depends on the instance having a logger method, as classes that inherit from ActiveRecord::Base have. To work around this, I just cheated and defined a logger method in my class that simply returned Rails.logger.
In the end, it didn't take much work, so it should be fairly simple to get these changes back into Paperclip, should the interest be there. I'm also going to check out Carrierwave, which recently added MongoMapper support.