Skip to content

Commit c4528de

Browse files
committed
delegate to the @tempfile instance variable
1 parent 1e50fae commit c4528de

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

actionpack/lib/action_dispatch/http/upload.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22

33
module ActionDispatch
44
module Http
5-
class UploadedFile < Tempfile
5+
class UploadedFile
66
attr_accessor :original_filename, :content_type, :tempfile, :headers
77

88
def initialize(hash)
99
@original_filename = hash[:filename]
1010
@content_type = hash[:type]
1111
@headers = hash[:head]
12-
13-
# To the untrained eye, this may appear as insanity. Given the alternatives,
14-
# such as busting the method cache on every request or breaking backwards
15-
# compatibility with is_a?(Tempfile), this solution is the best available
16-
# option.
17-
#
18-
# TODO: Deprecate is_a?(Tempfile) and define a real API for this parameter
19-
tempfile = hash[:tempfile]
20-
tempfile.instance_variables.each do |ivar|
21-
instance_variable_set(ivar, tempfile.instance_variable_get(ivar))
22-
end
12+
@tempfile = hash[:tempfile]
2313
end
2414

25-
alias local_path path
15+
def method_missing(name, *args, &block)
16+
@tempfile.send(name, *args, &block)
17+
end
2618
end
2719

2820
module Upload

actionpack/test/dispatch/uploaded_file_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,23 @@ def test_tempfile
2121
uf = Http::UploadedFile.new(:tempfile => 'foo')
2222
assert_equal 'foo', uf.tempfile
2323
end
24+
25+
def test_delegates_to_tempfile
26+
tf = Class.new { def tenderlove; 'thunderhorse' end }
27+
uf = Http::UploadedFile.new(:tempfile => tf.new)
28+
assert_equal 'thunderhorse', uf.tenderlove
29+
end
30+
31+
def test_delegates_to_tempfile_with_params
32+
tf = Class.new { def tenderlove *args; args end }
33+
uf = Http::UploadedFile.new(:tempfile => tf.new)
34+
assert_equal %w{ thunder horse }, uf.tenderlove(*%w{ thunder horse })
35+
end
36+
37+
def test_delegates_to_tempfile_with_block
38+
tf = Class.new { def tenderlove; yield end }
39+
uf = Http::UploadedFile.new(:tempfile => tf.new)
40+
assert_equal('thunderhorse', uf.tenderlove { 'thunderhorse' })
41+
end
2442
end
2543
end

0 commit comments

Comments
 (0)