Fileupload with Mongoid and Paperclip

Angelegt von andi Sun, 12 Dec 2010 16:05:00 GMT

Last week, a new gem - ’mongoid-paperclip was released. If you are already used to paperclip, you might in deed be sad if you use mongoid and have to disclaim the convenience of paperclip if you want to add file upload to one of your documents.

Anyway, i’m forming a habit of having a closer look at new gems, and at least check out the codebase as far as necessary to know what i’m dealing with. In the case of mongoid-paperclip I was particularly interested, and I was not too much surprised to find out that it’s actually a very easy implementation.

You don’t have to care about how complicated a gem or library you rely on is, as long as it works fine, but i often see people dismiss an external library in the moment it doesn’t perfectly fit their needs. But often code is not that complicated, and it would be worth to have a look, find out how easy it is to tweak it, go for it, and be happy and independent.

To illustrate how easy a gem like this can be, I’d like to give you some code, and show you what the gem actually does.

But first you have to know, how to use mongoid-paperclip. See this example, and keep in mind, that you can use ‘has_attached_file’ exactly the way you’re used to with paperclip, so checkout the paperclip docs if you’re unsure. I keep it with the most easy example without any additional options.

class Profile
  include Mongoid::Document
  include Mongoid::Paperclip

  has_attached_file :avatar
end

Aside from the gemspec and readme, yhe code is actually all in one file, in lib/mongoid_paperclip.rb.

First thing that happens, is to require Paperclip, and output a note if it cannot be loaded.

begin
  require "paperclip"
rescue LoadError
  puts "Mongoid::Paperclip requires that you install the Paperclip gem."
  exit
end

Second is, deactivating Paperclips default logger, because it depends on ActiveRecord.

Paperclip.options[:log] = false

Therefore, if Rails is loaded and a logger specified, we can use this.

if defined?(Rails)
  if Rails.respond_to?(:logger)
    Paperclip.options[:log] = Rails.logger
  end
end

And now we come to the Mongoid::Paperclip module itself. As you see its less then 15 lines of code, and what it does is basically to include paperclip, and to pass the params to paperclips ‘has_attached_file method. Last step is to make sure that mongoid knows about the necessary fields, because paperclip can not deal with mongoids dynamic attributes.

    module ClassMethods

      def has_attached_file(field, options = {})

        include ::Paperclip
        include ::Paperclip::Glue

        has_attached_file(field, options)

        field(:"#{field}_file_name",    :type => String)
        field(:"#{field}_content_type", :type => String)
        field(:"#{field}_file_size",    :type => Integer)
        field(:"#{field}_updated_at",   :type => DateTime)
      end
    end
  end

Well, this was pretty easy to understand, and you might want to come back on it in similar situations.

Trackbacks

Verwenden Sie den folgenden Link zur Rückverlinkung von Ihrer eigenen Seite:
http://praktikanten.brueckenschlaeger.org/trackbacks?article_id=402

Leave a comment

Comments