Permalink
Browse files

Add original_filename to the FileUploadCache::Tempfile, which is pull…

…ed from the FileUploadCache::CachedFile object. This field will also be on the Rack file upload object too.
  • Loading branch information...
1 parent 16d8f70 commit d0f4686bef7cc0319a91cb33df96ec6cd7842251 @kenmazaika committed Apr 11, 2012
@@ -1,9 +1,12 @@
module FileUploadCache
class Tempfile < ::Tempfile
+ attr_accessor :original_filename
# mode should be :binmode or :text
- def self.for(data, mode = :binmode, &block)
- file = self.new("tempfile")
+ def self.for(data, filename, mode = :binmode, &block)
+ file = self.new(filename)
+ file.original_filename = filename
+
begin
file.binmode if mode == :binmode
file.write(data)
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
s.summary = "Insert FileUploadCache summary."
s.description = "Insert FileUploadCache description."
s.files = Dir["{app,lib,config}/**/*"] + ["Rakefile", "Gemfile"]
- s.version = "1.0.2"
+ s.version = "1.0.3"
s.authors = ['Ken Mazaika']
s.add_dependency 'uuid'
s.add_dependency 'rails'
@@ -21,7 +21,7 @@ def cached_file_for(field)
if( ! self.send("#{field}_cache_id").blank? && original.blank? )
cached_file = CachedFile.find(self.send("#{field}_cache_id"))
- FileUploadCache::Tempfile.for(cached_file.read) do |tf|
+ FileUploadCache::Tempfile.for(cached_file.read, cached_file.original_filename) do |tf|
self.send("#{field}=", tf)
self.send("cached_#{field}=", cached_file)
end
@@ -6,15 +6,18 @@ class TempfileTest < MiniTest::Unit::TestCase
def test_for_binmode
file_data = "OMGOMG"
- FileUploadCache::Tempfile.for(file_data) do |temp_file|
+ FileUploadCache::Tempfile.for(file_data, "omg.txt") do |temp_file|
assert_equal file_data, temp_file.read
+ assert_equal "omg.txt", temp_file.original_filename
end
end
def test_for_text_mode
- file_data = "OMG"
- FileUploadCache::Tempfile.for(file_data, :text) do |temp_file|
+ file_data = "OMGOMG"
+
+ FileUploadCache::Tempfile.for(file_data, "omg.txt", :text) do |temp_file|
assert_equal file_data, temp_file.read
+ assert_equal "omg.txt", temp_file.original_filename
end
end
end

0 comments on commit d0f4686

Please sign in to comment.