Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came across this, and it looks like an example of how to solve this problem: <a href="https://gist.github.com/995663">https://gist.github.com/995663</a>. </p> <p>The uploader first gets loaded when you call <code>mount_uploader</code>, at which point things like <code>if image?</code> or <code>elsif video?</code> won't work, because there is no file to upload defined yet. You'll need the methods to be called when the class is instantiated instead. </p> <p>What the link I gave above does, is rewrite the <code>process</code> method, so that it takes a list of file extensions, and processes only if your file matches one of those extensions</p> <pre><code># create a new "process_extensions" method. It is like "process", except # it takes an array of extensions as the first parameter, and registers # a trampoline method which checks the extension before invocation def self.process_extensions(*args) extensions = args.shift args.each do |arg| if arg.is_a?(Hash) arg.each do |method, args| processors.push([:process_trampoline, [extensions, method, args]]) end else processors.push([:process_trampoline, [extensions, arg, []]]) end end end # our trampoline method which only performs processing if the extension matches def process_trampoline(extensions, method, args) extension = File.extname(original_filename).downcase extension = extension[1..-1] if extension[0,1] == '.' self.send(method, *args) if extensions.include?(extension) end </code></pre> <p>You can then use this to call what used to be process</p> <pre><code>IMAGE_EXTENSIONS = %w(jpg jpeg gif png) DOCUMENT_EXTENSIONS = %(exe pdf doc docm xls) def extension_white_list IMAGE_EXTENSIONS + DOCUMENT_EXTENSIONS end process_extensions IMAGE_EXTENSIONS, :resize_to_fit =&gt; [1024, 768] </code></pre> <p>For versions, there's a page on the carrierwave wiki that allows you to conditionally process versions, if you're on >0.5.4. <a href="https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing">https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing</a>. You'll have to change the version code to look like this:</p> <pre><code>version :big, :if =&gt; :image? do process :resize_to_limit =&gt; [160, 100] end protected def image?(new_file) new_file.content_type.include? 'image' end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload