scanE
scanE

Reputation: 342

skip carrierwave Integirty and Processing validation

I have white listed some of the extensions in the carrierwave uploader class

def extension_white_list
    %w(doc docx)
end

In some cases I would like to skip the Integrity validation while saving a record. But as per their documentation validates_integrity_of validation exist by default.

https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Validate-uploads-with-Active-Record

can anyone please tell me how to skip such validation ?

Upvotes: 3

Views: 1940

Answers (2)

benchwarmer
benchwarmer

Reputation: 2774

in uploaders/file_uploader.rb

def extension_white_list
  if model.do_i_need_validation?
    %w(doc docx)
  else
   file.extension
  end
end

and define this instance method in the model

def do_i_need_validation?
  condition? ? true : false
end

Just replace the content of the method suitable to your app

Upvotes: 3

13k
13k

Reputation: 299

I couldn't find anything about this in any of carrierwave's documentation, but reading its source code, one can pass specific uploader options in the mount_uploader call:

mount_uploader :field, MyUploader, options

Validations configuration do exist in uploader options, so you can, for example, disable all validations using:

mount_uploader :field, MyUploader, validate_download: false, validate_integrity: false, validate_processing: false

Note that when doing this the errors are silently ignored, so saves will succeed. This could be unexpected behavior. You can check if the operation actually had any errors using the model helpers <field>_processing_error, <field>_integrity_error and <field>_download_error:

class Article < ActiveRecord::Base
  mount_uploader :image, ImageUploader, validate_integrity: false
end

article = Article.find(1)
article.update_attributes!(title: "New article title", image: open("/path/to/invalid_image.jpg")) # => this will actually succeed
article.image_integrity_error # => returns the error message from carrierwave

Upvotes: 1

Related Questions