Conditional versions/process with Carrierwave

怎甘沉沦 提交于 2019-12-06 03:20:54

问题


I have this uploader class

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  process :resize_to_limit => [300, 300]

  version :thumb do
    process :resize_to_limit => [50, 50]
  end

 ...

Which will process the original file to 300x300 and save a thumb version.

I would like to be able to make a small/thumb version only based on a boolean on my model?

So I did this

if :icon_only?
 process :resize_to_limit => [50, 50]
else
  process :resize_to_limit => [300, 300]
end

protected

 def icon_only? picture
   model.icon_only?
 end

But it always ended up in 50x50 processing. Even when I did like this

 def icon_only? picture
   false
 end

I might got my syntax up all wrong with the : but i also tried asking

if icon_only?

Which told me there was no method name like that.Im lost...


回答1:


Use an :if conditional, like so:

process :resize_to_limit => [50, 50], :if => :icon_only?
process :resize_to_limit => [300, 300], :if => ...

I haven't actually tried this but it's documented in the code, so it should work.




回答2:


As @shioyama pointed out, one can use :if to specify the condition.

However, doing the inverse condition (e.g. !icon_only?) requires a bit of work.

process :resize_to_limit => [300, 300], :if => Proc.new {|version, options| !version.send(:icon_only?, options[:file])} do


来源:https://stackoverflow.com/questions/11778464/conditional-versions-process-with-carrierwave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!