Skip to content
Browse files

Merge pull request #5309 from carlosantoniodasilva/ad-uploaded-file

Refactor ActionDispatch::UploadedFile
  • Loading branch information...
2 parents d1a4faf + 8d5c742 commit d467351149e80ff203c8d238542f324666421e23 @tenderlove tenderlove committed Mar 6, 2012
Showing with 7 additions and 9 deletions.
  1. +6 −8 actionpack/lib/action_dispatch/http/upload.rb
  2. +1 −1 actionpack/test/dispatch/uploaded_file_test.rb
View
14 actionpack/lib/action_dispatch/http/upload.rb
@@ -4,11 +4,12 @@ class UploadedFile
attr_accessor :original_filename, :content_type, :tempfile, :headers
def initialize(hash)
+ @tempfile = hash[:tempfile]
+ raise(ArgumentError, ':tempfile is required') unless @tempfile
+
@original_filename = encode_filename(hash[:filename])
@content_type = hash[:type]
@headers = hash[:head]
- @tempfile = hash[:tempfile]
- raise(ArgumentError, ':tempfile is required') unless @tempfile
end
def read(*args)
@@ -19,15 +20,12 @@ def read(*args)
[:open, :path, :rewind, :size].each do |method|
class_eval "def #{method}; @tempfile.#{method}; end"
end
-
+
private
+
def encode_filename(filename)
# Encode the filename in the utf8 encoding, unless it is nil
- if filename
- filename.force_encoding("UTF-8").encode!
- else
- filename
- end
+ filename.force_encoding("UTF-8").encode! if filename
end
end
View
2 actionpack/test/dispatch/uploaded_file_test.rb
@@ -12,7 +12,7 @@ def test_original_filename
uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
assert_equal 'foo', uf.original_filename
end
-
+
def test_filename_should_be_in_utf_8
uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
assert_equal "UTF-8", uf.original_filename.encoding.to_s

0 comments on commit d467351

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