From 74d02a29089732f690ac959b1a7ef8810ab2d4ec Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 17 Mar 2014 23:15:52 +0000 Subject: [PATCH] file_presenter: display unknown type when file has no content_type --- lib/file_presenter.rb | 7 ++++++- test/unit/file_presenter_test.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/file_presenter.rb b/lib/file_presenter.rb index 08c4d43..f0e926d 100644 --- a/lib/file_presenter.rb +++ b/lib/file_presenter.rb @@ -51,7 +51,12 @@ class FilePresenter end def short_description - _("File (%s)") % content_type.sub(/^application\//, '').sub(/^x-/, '').sub(/^image\//, '') + file_type = if content_type.present? + content_type.sub(/^application\//, '').sub(/^x-/, '').sub(/^image\//, '') + else + _('Unknown') + end + _("File (%s)") % file_type end # Define the css classes to style the page fragment with the file related diff --git a/test/unit/file_presenter_test.rb b/test/unit/file_presenter_test.rb index 9b0d9f7..917885f 100644 --- a/test/unit/file_presenter_test.rb +++ b/test/unit/file_presenter_test.rb @@ -62,4 +62,16 @@ class FilePresenterTest < ActiveSupport::TestCase f = FilePresenter.for(UploadedFile.new) assert f.kind_of?(UploadedFile) end + + should 'not crash with uploaded_file short description without content_type' do + f = FilePresenter.for(UploadedFile.new) + assert_nothing_raised do + f.short_description + end + end + + should 'show unknown type when file doesn\'t have a content_type' do + f = FilePresenter.for(UploadedFile.new) + assert_match /Unknown/, f.short_description + end end -- libgit2 0.21.2