Skip to content

How to: Do conditional processing

skyeagle edited this page Jul 29, 2011 · 12 revisions

NOTE: Not included in latest gem release (0.5.4)

If you want to hold both images and regular files as attachments to a model, additional processing (i.e. creating a thumbnail) can be attached only to images.

Here's the example code to create thumbnails when the uploaded file is an image:

version :thumb, :if => :image? do
  process :resize_to_limit => [200, 200]
end

protected

def image?(new_file)
  new_file.content_type.include? 'image'
end

Just a mention, to avoid double checking if it is needless in you case, you could make it like as:

def image?(new_file)
  @is_image ||= new_file.content_type.include? 'image'
end
Clone this wiki locally