Skip to content
Browse files

Work with merb tempfile

  • Loading branch information...
1 parent acc6500 commit 3836c313d60772ff2ae334083acd083dfdcadf68 @redinger redinger committed May 7, 2008
Showing with 41 additions and 3 deletions.
  1. +10 −3 lib/technoweenie/attachment_fu.rb
  2. +13 −0 test/base_attachment_tests.rb
  3. +10 −0 test/extra_attachment_test.rb
  4. +8 −0 test/test_helper.rb
View
13 lib/technoweenie/attachment_fu.rb
@@ -290,9 +290,16 @@ def uploaded_data() nil; end
#
# TODO: Allow it to work with Merb tempfiles too.
def uploaded_data=(file_data)
- return nil if file_data.nil? || file_data.size == 0
- self.content_type = file_data.content_type
- self.filename = file_data.original_filename if respond_to?(:filename)
+ if file_data.respond_to?(:content_type)
+ return nil if file_data.size == 0
+ self.content_type = file_data.content_type
+ self.filename = file_data.original_filename if respond_to?(:filename)
+ else
+ return nil if file_data.nil? || file_data['size'] == 0
+ self.content_type = file_data['content_type']
+ self.filename = file_data['filename']
+ file_data = file_data['tempfile']
+ end
if file_data.is_a?(StringIO)
file_data.rewind
self.temp_data = file_data.read
View
13 test/base_attachment_tests.rb
@@ -12,6 +12,19 @@ def test_should_create_file_from_uploaded_file
end
end
+ def test_should_create_file_from_merb_temp_file
+ assert_created do
+ attachment = upload_merb_file :filename => '/files/foo.txt'
+ assert_valid attachment
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
+ assert attachment.image?
+ assert !attachment.size.zero?
+ #assert_equal 3, attachment.size
+ assert_nil attachment.width
+ assert_nil attachment.height
+ end
+ end
+
def test_reassign_attribute_data
assert_created 1 do
attachment = upload_file :filename => '/files/rails.png'
View
10 test/extra_attachment_test.rb
@@ -24,6 +24,16 @@ def test_should_create_file_from_uploaded_file
end
end
+ def test_should_create_file_from_merb_temp_file
+ assert_created do
+ attachment = upload_merb_file :filename => '/files/foo.txt'
+ assert_valid attachment
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
+ assert attachment.image?
+ assert !attachment.size.zero?
+ end
+ end
+
def test_should_create_image_from_uploaded_file_with_custom_content_type
assert_created do
attachment = upload_file :content_type => 'foo/bar', :filename => '/files/rails.png'
View
8 test/test_helper.rb
@@ -87,6 +87,14 @@ def upload_file(options = {})
end
end
+ def upload_merb_file(options = {})
+ use_temp_file options[:filename] do |file|
+ att = attachment_model.create :uploaded_data => {"size" => file.size, "content_type" => options[:content_type] || 'image/png', "filename" => file, 'tempfile' => fixture_file_upload(file, options[:content_type] || 'image/png')}
+ att.reload unless att.new_record?
+ return att
+ end
+ end
+
def use_temp_file(fixture_filename)
temp_path = File.join('/tmp', File.basename(fixture_filename))
FileUtils.mkdir_p File.join(fixture_path, 'tmp')

0 comments on commit 3836c31

Please sign in to comment.
Something went wrong with that request. Please try again.