Apply a processor using a paper clip if true

I have a model with a paperclip ruby pearl. I defined an attachment with two processors ( thumbnail and watermark ).

The question is whether there is a way to use a watermark processor if the condition is true . (the idea does not define new attachments without a watermark processor)

Thanks in advance.

I am trying to use this code, but dosen't works. If there is a process with the watermark else in the eid field, if null processes only the thumbnail

 :processors => lambda { |a| if a.eid.nil? [:thumbnail,:watermark] else [:thumbnail] end }, 
+4
source share
2 answers

The processors option can accept proc , so you can make your processors depend on the instance:

 :processors => lambda{ |attachment| attachment.instance.some_method_to_get_processors_here }, 
+6
source

According to current Paperclip docs, lambda is called differently for processors than for styles. Styles passed attachment:

 class User < ActiveRecord::Base has_attached_file :avatar, :styles => lambda { |attachment| { :thumb => (attachment.instance.boss? ? "300x300>" : "100x100>") } } end 

With attachment.instance is an instance of your model. But the processors are passed by the instance itself:

 class User < ActiveRecord::Base has_attached_file :avatar, :processors => lambda { |instance| instance.processors } attr_accessor :watermark end 

This last example worked for me. I had User#processors return an array of processors (but if you just want the default processor, return [:thumbnail] , not an empty array).

+4
source

Source: https://habr.com/ru/post/1387388/


All Articles