Class: ApplicationUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
CarrierWave::MiniMagick
Defined in:
app/uploaders/application_uploader.rb

Overview

typed: ignore frozen_string_literal: true

Instance Method Summary collapse

Instance Method Details

#extension_whitelistObject

Add a white list of extensions which are allowed to be uploaded. For images you might use something like this:



42
43
44
# File 'app/uploaders/application_uploader.rb', line 42

def extension_whitelist
  %w[jpg jpeg png svg heic]
end

#quality(percentage) ⇒ Object

Override the filename of the uploaded files: Avoid using model.id or version_name here, see uploader/store.rb for details. def filename

"something.jpg" if original_filename

end



52
53
54
55
56
57
58
# File 'app/uploaders/application_uploader.rb', line 52

def quality(percentage)
  manipulate! do |img|
    img.quality(percentage.to_s)
    img = yield(img) if block_given?
    img
  end
end

#store_dirObject

Override the directory where uploaded files will be stored. This is a sensible default for uploaders that are meant to be mounted:



16
17
18
# File 'app/uploaders/application_uploader.rb', line 16

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

#validate_dimensionsObject



62
63
64
65
66
67
68
# File 'app/uploaders/application_uploader.rb', line 62

def validate_dimensions
  manipulate! do |img|
    raise CarrierWave::ProcessingError, 'dimensions too large' if img.dimensions.any? { |i| i > 10_000 }

    img
  end
end