Skip to content

Paperclip Configuration

Andy edited this page Oct 31, 2013 · 1 revision

Paperclip is a very versatile utility GEM that allows attachment of arbitrary files to a Rails model object. The file attachments can include images and allows for image scaling if ImageMagic is running on the target server. This feature is implemented with the mongoid-paperclip GEM, which in turn relies on the underlying paperclip GEM.

Our sample Project resource, which includes the model, controller, and views have been modified to allow you to attach a project charter document. The project charter can then be downloaded from the Project#show view. Storage is using the default filesystem storage. If you want to configure storage on a service like Amazon S3, you can configure Paperclip with the appropriate credentials. An over view of using AWS S3 with Paperclip is here

A snapshot of the Project model code and using Paperclip for the charter_doc attribute is shown below:

class Project
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paperclip

  # Add call to strip leading and trailing white spaces from all atributes
  strip_attributes  # See strip_attributes for more information

  field :name, type: String
  field :description, type: String

  
  ## VALIDATIONS -------------------------------------------------------
  
  validates_presence_of :name
  validates_presence_of :description
  validates_presence_of :user_id
  
  ## RELATIONSHIPS -----------------------------------------------------
  
  belongs_to :user
  has_and_belongs_to_many :groups
  has_mongoid_attached_file :charter_doc
  
  ## GROUP METHOD INJECTION --------------------------------------------
  
  include GroupRelations
  
  ######################################################################
  # The GroupRelations module has some utility methods that will enable
  # the project to interact with user groups. The group_relate method
  # uses the relate_groups utility method to relate groups to the
  # the current instance of the Project model class.
  ######################################################################
  def group_relate(group_ids)
    relate_groups(group_ids: group_ids, resource: self)
  end

end

Clone this wiki locally