Search code examples
ruby-on-rails-3ruby-on-rails-3.2carrierwavegridfsfog

Carrierwave dynamic storage type via model attributes


I want to save photographs in 'grid_fs' and other types of files into 'fog', like:

class DocumentUploader < CarrierWave::Uploader::Base
  def storage
    if model.is_photograph == true
      :grid_fs
    else
      :fog
    end 
  end
end

Is this possible?

I want to dynamically assign the storage type from model attributes, and I've tried this, but the model and its attributes are not accessible in a class method.

Thanks!


Solution

  • The quick answer is "no". Unlike store_dir, validate_integrity et al, storage isn't sent through add_config, and doesn't get any of the instance-level magic that would make this easy.

    It might be possible, but anything I could come up with almost certainly be a fragile hack. You're better off looking at other ways to support the feature (multiple uploaders?) or changing the specification.