开发者

rails 3.0 and carrierwave how to prevent version creation for non-image type

I'm struggling a bit trying to achieve something that appeared simple initially to me. In the context of a rails 3.0 app using carrierwave. The user must be able do download any type of document except .exe files. in Carriewave there is the whitelist

def extension_white_list
    %w(jpg jpeg gif png)
end

I whish there was a blacklist as well it would easier in my case. Anyway this is the not the main concern.

for image file I set 2 version in my uploader class.

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


  version :small do
     process :resize_to_fit => [125, 125]
  end

I have to admit that I'm little bit confused by this syntax. What kind of ruby code "version" define. I mean, it is not a method, so what's that in a class?

开发者_开发问答

I tried to create a method image?

def image?
%w(jpg jpeg gif png).include?(filename.extension.to_s)
end

and then called it from the version piece of code to prevent creation of those version when the file is not an image

version :thumb do
  if image?
    process :resize_to_fit => [50, 50]  
  end
end

but this code throw an error

undefined method `image?' for #<Class:0x000001017274f8> 

any help would be appreciated.

thanks.


Here's how you actually can do this. The current version of carrierwave supports conditional version processing now. See the wiki page https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing

To only create versions for specific types, do this:

version :thumb, :if => :image? do
  process :resize_to_fit => [50, 50]  
end

protected

def image?(new_file)
  new_file.content_type.include? 'image'
end


Finally a clear and clean answer for this question: https://gist.github.com/995663

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜