tsurupin/portfolio

View on GitHub
app/uploaders/author_image_uploader.rb

Summary

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

class AuthorImageUploader < 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/author/#{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