Commit 74d02a29089732f690ac959b1a7ef8810ab2d4ec

Authored by Rodrigo Souto
1 parent 131144ba

file_presenter: display unknown type when file has no content_type

(ActionItem3049)
lib/file_presenter.rb
... ... @@ -51,7 +51,12 @@ class FilePresenter
51 51 end
52 52  
53 53 def short_description
54   - _("File (%s)") % content_type.sub(/^application\//, '').sub(/^x-/, '').sub(/^image\//, '')
  54 + file_type = if content_type.present?
  55 + content_type.sub(/^application\//, '').sub(/^x-/, '').sub(/^image\//, '')
  56 + else
  57 + _('Unknown')
  58 + end
  59 + _("File (%s)") % file_type
55 60 end
56 61  
57 62 # Define the css classes to style the page fragment with the file related
... ...
test/unit/file_presenter_test.rb
... ... @@ -62,4 +62,16 @@ class FilePresenterTest < ActiveSupport::TestCase
62 62 f = FilePresenter.for(UploadedFile.new)
63 63 assert f.kind_of?(UploadedFile)
64 64 end
  65 +
  66 + should 'not crash with uploaded_file short description without content_type' do
  67 + f = FilePresenter.for(UploadedFile.new)
  68 + assert_nothing_raised do
  69 + f.short_description
  70 + end
  71 + end
  72 +
  73 + should 'show unknown type when file doesn\'t have a content_type' do
  74 + f = FilePresenter.for(UploadedFile.new)
  75 + assert_match /Unknown/, f.short_description
  76 + end
65 77 end
... ...