tsurupin/portfolio

View on GitHub
app/uploaders/project_image_uploader.rb

Summary

Maintainability
A
1 hr
Test Coverage
require 'carrierwave/processing/mime_types'

class ProjectImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  include CarrierWave::MimeTypes
  process :set_content_type

  process resize_to_limit: [740, 740]

  if Rails.env.production? || Rails.env.staging?
    storage :fog
  else
    storage :file
  end

  def store_dir
    "uploads/project/#{mounted_as}/#{model.id}/"
  end

  def extension_white_list
    %w(jpg gif png jpeg)
  end

  def filename
    "#{secure_token}.#{file.extension}" if original_filename.present?
  end

  protected

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) || model.instance_variable_set(var, SecureRandom.uuid)
  end
end