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 support making a pdf from image files so I dont want to use the processor if its not an image.

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.index("image/") == 0 ? {thumb: "50x50>", preview: '800x600>'} : {} }
    processors: lambda{|instance| instance.doc.content_type.index("image/") == 0 ? [:thumbnail, :ghostscript] : [] }

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