Rails

Paperclip Apply Processor If Condition

Posted by Weston Ganger on January 15, 2016

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:


Posted in Rails and Tagged with paperclip rails file 


Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts