5

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...

4

2 に答える 2

4

:if次のように条件を使用します。

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

私は実際にこれを試したことはありませんが、コードに記載されているので、うまくいくはずです。

于 2012-08-02T13:46:17.277 に答える
2

@shioyama が指摘したように、 :if を使用して条件を指定できます。

ただし、逆の条件 (例: !icon_only?) を実行するには、少し作業が必要です。

process :resize_to_limit => [300, 300], :if => Proc.new {|version, options| !version.send(:icon_only?, options[:file])} do
于 2013-05-14T02:23:20.000 に答える