aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-03-06 18:34:20 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-03-06 18:34:20 -0300
commit8d5c742df4d172c084a84013009b052eeb9551a6 (patch)
treeb4a436551348be5622245337be9c0b707bfc6d85 /actionpack
parentd1a4faf61f64e53797d13b7c2f150b94be94e916 (diff)
downloadrails-8d5c742df4d172c084a84013009b052eeb9551a6.tar.gz
rails-8d5c742df4d172c084a84013009b052eeb9551a6.tar.bz2
rails-8d5c742df4d172c084a84013009b052eeb9551a6.zip
Refactor AD::UploadedFile, and raise sooner if tempfile is not present
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/http/upload.rb14
-rw-r--r--actionpack/test/dispatch/uploaded_file_test.rb2
2 files changed, 7 insertions, 9 deletions
diff --git a/actionpack/lib/action_dispatch/http/upload.rb b/actionpack/lib/action_dispatch/http/upload.rb
index 5ab99d1061..c4a915d1ad 100644
--- a/actionpack/lib/action_dispatch/http/upload.rb
+++ b/actionpack/lib/action_dispatch/http/upload.rb
@@ -4,11 +4,12 @@ module ActionDispatch
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 @@ module ActionDispatch
[: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
diff --git a/actionpack/test/dispatch/uploaded_file_test.rb b/actionpack/test/dispatch/uploaded_file_test.rb
index 0b95291e18..1e3f720fa7 100644
--- a/actionpack/test/dispatch/uploaded_file_test.rb
+++ b/actionpack/test/dispatch/uploaded_file_test.rb
@@ -12,7 +12,7 @@ module ActionDispatch
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