Paperclip Apply Processor If Condition

Posted By Weston Ganger

I have a PDF processor on some of my paperclip attachments.
It only supports making images from PDF files so I dont want to use the processor if its not an PDF.

You need to use a lambda to do this.

class Post < ActiveRecord::Base
  has_attached_file :doc, 
    styles: lambda{|attachment| attachment.instance.doc.content_type == 'application/pdf'  ? {thumb: "50x50>", preview: '800x600>'} : {} }
    processors: lambda{|instance| instance.doc.content_type == 'application/pdf' ? [:thumbnail, :ghostscript] : [] }
end

Related External Links:

Article Topic:Software Development - Ruby / Rails

Date:January 15, 2016