ruby CarrierWave初始化文件,用于在装载和生产中使用夹具进行测试并支持S3。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby CarrierWave初始化文件,用于在装载和生产中使用夹具进行测试并支持S3。相关的知识,希望对你有一定的参考价值。

# NullStorage provider for CarrierWave for use in tests.  Doesn't actually
# upload or store files but allows test to pass as if files were stored and
# the use of fixtures.
class NullStorage
  attr_reader :uploader

  def initialize(uploader)
    @uploader = uploader
  end

  def identifier
    uploader.filename
  end

  def store!(_file)
    true
  end

  def retrieve!(_identifier)
    true
  end
end

CarrierWave.configure do |config|
  # Make the tmp dir work on Heroku
  config.cache_dir = "#{Rails.root}/tmp/uploads"

  if Rails.env.production? || Rails.env.staging?
    config.storage :fog
    config.fog_credentials = {
      provider:               "AWS",
      # When precompiling assets Fog will be initialized and needs to be
      # initialized (even though it will never touch S3), provide some values
      # to prevent Fog gem from crashing during initialize wihtout actually
      # giving away the keys.
      aws_access_key_id:      ENV["S3_KEY"] || "S3_KEY",
      aws_secret_access_key:  ENV["S3_SECRET"] || "S3_SECRET"
    }

    config.fog_directory  = ENV["S3_BUCKET"]
    # App requires explicit protocol to be specified for uploaded assets
    # Do not change to "//..." like we are doing with the other asset hosts
    config.asset_host     = "https://#{ENV["S3_CDN_HOST"]}"
    config.fog_attributes = { "Cache-Control" => "max-age=315576000" }

  elsif Rails.env.development?
    config.storage :file
    config.asset_host = "http://#{ENV["S3_CDN_HOST"] || ENV["HOST"]}"

  elsif Rails.env.test?
    config.storage NullStorage
    # Required to prevent FactoryGirl from giving an infuriating exception
    # ArgumentError: wrong exec option
    # It also speeds up tests so it's a good idea
    config.enable_processing = false
  end
end

# https://gist.github.com/1058477
module CarrierWave::MiniMagick
  def quality(percentage)
    manipulate! do |img|
      img.quality(percentage.to_s)
      img = yield(img) if block_given?
      img
    end
  end
end

以上是关于ruby CarrierWave初始化文件,用于在装载和生产中使用夹具进行测试并支持S3。的主要内容,如果未能解决你的问题,请参考以下文章

ruby 生成随机文件名并保存旧的已保存文件名(CarrierWave FTP)

Ruby on Rails - Heroku上的Carrierwave错误

ruby 在Rails中从CarrierWave过渡到Dragonfly

ruby CarrierWave

ruby 使用CarrierWave延迟图像处理

ruby 更新s3上的carrierwave图像