* VRFS-217; necessary changes to test and make artifact model work with carrierwave

This commit is contained in:
Seth Call 2013-02-12 22:40:01 -06:00
parent 1b1ac040a6
commit 713b4b6f4b
2 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,59 @@
# encoding: utf-8
class ArtifactUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
# storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"artifacts/#{model.product}/#{model.version}"
end
def md5
@md5 ||= ::Digest::MD5.file(current_path).hexdigest
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(exe msi dmg)
end
# 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
end

33
spec/uses_temp_files.rb Normal file
View File

@ -0,0 +1,33 @@
#http://gabebw.wordpress.com/2011/03/21/temp-files-in-rspec/
# this will make a folder jam-ruby/spec/tmp if used in an rspec test, and delete it after
# our .gitignore would also keep spec/tmp out, if somehow it did not get deleted.
module UsesTempFiles
def self.included(example_group)
example_group.extend(self)
end
def in_directory_with_file(file)
before do
@pwd = Dir.pwd
@tmp_dir = File.join(File.dirname(__FILE__), 'tmp')
FileUtils.mkdir_p(@tmp_dir)
Dir.chdir(@tmp_dir)
FileUtils.mkdir_p(File.dirname(file))
FileUtils.touch(file)
end
define_method(:content_for_file) do |content|
f = File.new(File.join(@tmp_dir, file), 'a+')
f.write(content)
f.flush # VERY IMPORTANT
f.close
end
after do
Dir.chdir(@pwd)
FileUtils.rm_rf(@tmp_dir)
end
end
end