如果条件为真,请使用回形针处理处理器
我有一个带回形针rubygem的模型。 我定义了一个带有2个处理器( thumbnail
和watermark
)的附件。
问题是如果条件为true
是否存在应用水印处理器的方法。 (没有水印处理器,没有定义新的attach_files的想法)
提前致谢。
我尝试使用这段代码,但不行。 如果字段eid存在带水印的进程,否则如果null进程只有缩略图
:processors => lambda { |a| if a.eid.nil? [:thumbnail,:watermark] else [:thumbnail] end },
processors
选项可以接受proc ,因此您可以使您的处理器依赖于实例:
:processors => lambda{ |attachment| attachment.instance.some_method_to_get_processors_here },
根据目前的Paperclip文档,对于处理器而言,lambda的调用方式与样式不同。 样式通过附件:
class User < ActiveRecord::Base has_attached_file :avatar, :styles => lambda { |attachment| { :thumb => (attachment.instance.boss? ? "300x300>" : "100x100>") } } end
使用attachment.instance
是您的模型的实例。 但处理器通过实例本身:
class User < ActiveRecord::Base has_attached_file :avatar, :processors => lambda { |instance| instance.processors } attr_accessor :watermark end
后一个例子对我有用。 我有User#processors
返回一个User#processors
数组(但如果你只想要默认处理器,则返回[:thumbnail]
,而不是空数组)。