mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
delegate to the @tempfile instance variable
This commit is contained in:
@@ -2,27 +2,19 @@ require 'active_support/core_ext/object/blank'
|
||||
|
||||
module ActionDispatch
|
||||
module Http
|
||||
class UploadedFile < Tempfile
|
||||
class UploadedFile
|
||||
attr_accessor :original_filename, :content_type, :tempfile, :headers
|
||||
|
||||
def initialize(hash)
|
||||
@original_filename = hash[:filename]
|
||||
@content_type = hash[:type]
|
||||
@headers = hash[:head]
|
||||
|
||||
# To the untrained eye, this may appear as insanity. Given the alternatives,
|
||||
# such as busting the method cache on every request or breaking backwards
|
||||
# compatibility with is_a?(Tempfile), this solution is the best available
|
||||
# option.
|
||||
#
|
||||
# TODO: Deprecate is_a?(Tempfile) and define a real API for this parameter
|
||||
tempfile = hash[:tempfile]
|
||||
tempfile.instance_variables.each do |ivar|
|
||||
instance_variable_set(ivar, tempfile.instance_variable_get(ivar))
|
||||
end
|
||||
@tempfile = hash[:tempfile]
|
||||
end
|
||||
|
||||
alias local_path path
|
||||
def method_missing(name, *args, &block)
|
||||
@tempfile.send(name, *args, &block)
|
||||
end
|
||||
end
|
||||
|
||||
module Upload
|
||||
|
||||
@@ -21,5 +21,23 @@ module ActionDispatch
|
||||
uf = Http::UploadedFile.new(:tempfile => 'foo')
|
||||
assert_equal 'foo', uf.tempfile
|
||||
end
|
||||
|
||||
def test_delegates_to_tempfile
|
||||
tf = Class.new { def tenderlove; 'thunderhorse' end }
|
||||
uf = Http::UploadedFile.new(:tempfile => tf.new)
|
||||
assert_equal 'thunderhorse', uf.tenderlove
|
||||
end
|
||||
|
||||
def test_delegates_to_tempfile_with_params
|
||||
tf = Class.new { def tenderlove *args; args end }
|
||||
uf = Http::UploadedFile.new(:tempfile => tf.new)
|
||||
assert_equal %w{ thunder horse }, uf.tenderlove(*%w{ thunder horse })
|
||||
end
|
||||
|
||||
def test_delegates_to_tempfile_with_block
|
||||
tf = Class.new { def tenderlove; yield end }
|
||||
uf = Http::UploadedFile.new(:tempfile => tf.new)
|
||||
assert_equal('thunderhorse', uf.tenderlove { 'thunderhorse' })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user