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 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts