Support ActionDispatch::Http::UploadedFile again#585
Support ActionDispatch::Http::UploadedFile again#585jnunemaker merged 1 commit intojnunemaker:masterfrom
Conversation
Using the `path` method instead of passing the file directly to `File.basename` to get the filename ensures that `ActionDispatch::Http::UploadedFile` can be used again. Related issue: jnunemaker#584
|
So this works with regular files and action dispatch uploads? Is path right? File.basename returns the filename, not a full path, but path makes me think it is more than just the filename. Any thoughts either way on that? Just curious. |
|
Hi, it's still using All I changed is passing the file's Example: irb(main):003:0> f = File.open("lib/httparty.rb")
=> #<File:lib/httparty.rb>
irb(main):004:0> f.path
=> "lib/httparty.rb"
irb(main):005:0> File.basename(f)
=> "httparty.rb"
irb(main):006:0> File.basename(f.path)
=> "httparty.rb"versus irb(main):002:0> file = ActionDispatch::Http::UploadedFile.new(tempfile: File.open("config/routes.rb"))
#<ActionDispatch::Http::UploadedFile:0x00007fe148b67bd8 @tempfile=#<File:config/routes.rb>, @original_filename=nil, @content_type=nil, @headers=nil>
irb(main):003:0> file.path
"config/routes.rb"
irb(main):004:0> File.basename(file.path)
"routes.rb"
irb(main):005:0> File.basename(file)
TypeError: no implicit conversion of ActionDispatch::Http::UploadedFile into String
from (irb):5:in `basename'
from (irb):5
from /Users/mcls/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands/console.rb:65:in `start'
from /Users/mcls/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/mcls/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/mcls/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/mcls/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/railties-5.0.6/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>' |
I overlooked that somehow. 😆 My bad. |
|
This is out in 0.16.2. Thank you so much for caring and fixing this. |
Using the
pathmethod instead of passing the file directly toFile.basenameensures thatActionDispatch::Http::UploadedFilecan be used again.Related issue: #584
Note: ActionDispatch isn't a dependency of the library, so I opted not to write a test specifically for it. I tried to account for it in the existing tests by checking that e.g.
to_strisn't called on the file object, but that didn't work sinceFile.basenameis implemented in C.