Project

General

Profile

Feature #44460 » 0003-Display-the-name-of-the-type-instead-of-the-MIME-typ.patch

Go MAEDA, 2026-09-21 17:24

View differences:

app/helpers/attachments_helper.rb
88 88
    api.created_on attachment.created_on
89 89
  end
90 90

  
91
  # Names that are displayed instead of MIME types. A name should be added
92
  # only for a type that is frequently used but whose MIME type is very long
93
  # and hard to read. The names are not translated, so they should consist
94
  # of the name of the application or the format followed by nouns.
95
  #
96
  # * A template is named by appending "Template" to the corresponding type
97
  # * For Microsoft Office, a macro-enabled type is named by appending "(+macro)"
98
  #
99
  # The keys must be in lowercase because the lookup is case-insensitive.
100
  CONTENT_TYPE_NAMES = {
101
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'Microsoft Word Document',
102
    'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => 'Microsoft Word Document Template',
103
    'application/vnd.ms-word.document.macroenabled.12' => 'Microsoft Word Document (+macro)',
104
    'application/vnd.ms-word.template.macroenabled.12' => 'Microsoft Word Document Template (+macro)',
105
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'Microsoft Excel Workbook',
106
    'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => 'Microsoft Excel Workbook Template',
107
    'application/vnd.ms-excel.sheet.macroenabled.12' => 'Microsoft Excel Workbook (+macro)',
108
    'application/vnd.ms-excel.template.macroenabled.12' => 'Microsoft Excel Workbook Template (+macro)',
109
    'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'Microsoft PowerPoint Presentation',
110
    'application/vnd.openxmlformats-officedocument.presentationml.template' => 'Microsoft PowerPoint Presentation Template',
111
    'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'Microsoft PowerPoint Slide Show',
112
    'application/vnd.ms-powerpoint.presentation.macroenabled.12' => 'Microsoft PowerPoint Presentation (+macro)',
113
    'application/vnd.ms-powerpoint.template.macroenabled.12' => 'Microsoft PowerPoint Presentation Template (+macro)',
114
    'application/vnd.ms-powerpoint.slideshow.macroenabled.12' => 'Microsoft PowerPoint Slide Show (+macro)',
115
    'application/vnd.oasis.opendocument.text' => 'OpenDocument Text',
116
    'application/vnd.oasis.opendocument.text-template' => 'OpenDocument Text Template',
117
    'application/vnd.oasis.opendocument.spreadsheet' => 'OpenDocument Spreadsheet',
118
    'application/vnd.oasis.opendocument.spreadsheet-template' => 'OpenDocument Spreadsheet Template',
119
    'application/vnd.oasis.opendocument.presentation' => 'OpenDocument Presentation',
120
    'application/vnd.oasis.opendocument.presentation-template' => 'OpenDocument Presentation Template',
121
    'application/vnd.oasis.opendocument.graphics' => 'OpenDocument Graphics',
122
    'application/vnd.oasis.opendocument.graphics-template' => 'OpenDocument Graphics Template'
123
  }.freeze
124

  
125
  # Returns a span tag that contains the content type of the attachment, or
126
  # nil if the content type is blank.
127
  #
128
  # For the types listed in CONTENT_TYPE_NAMES, the name of the type is
129
  # displayed and the MIME type is available in the title attribute.
130
  def attachment_content_type_tag(attachment)
131
    content_type = attachment.content_type
132
    return if content_type.blank?
133

  
134
    name = CONTENT_TYPE_NAMES[content_type.downcase]
135
    content_tag(:span, name || content_type, :class => 'content-type', :title => (content_type if name))
136
  end
137

  
91 138
  def render_file_content(attachment, content)
92 139
    if attachment.is_markdown?
93 140
      render :partial => 'common/markup', :locals => {:markup_text_formatting => 'common_mark', :markup_text => content}
app/views/layouts/_file.html.erb
8 8

  
9 9
<div class="attachments">
10 10
<p><% if @attachment.description.present? %><%= @attachment.description %><br /><% end %>
11
   <% content_type = content_tag(:span, @attachment.content_type, :class => 'content-type') if @attachment.content_type.present? %>
12
   <span class="author"><%= safe_join([link_to_user(@attachment.author), format_time(@attachment.created_on), content_type].compact, ', ') %></span></p>
11
   <span class="author"><%= safe_join([link_to_user(@attachment.author), format_time(@attachment.created_on), attachment_content_type_tag(@attachment)].compact, ', ') %></span></p>
13 12
</div>
14 13
<div class="filecontent-container">
15 14
  <%= yield %>
test/functional/attachments_controller_test.rb
151 151
    assert_select 'div.attachments span.author', :text => /, application\/x-ruby\z/
152 152
  end
153 153

  
154
  def test_show_should_display_name_instead_of_long_content_type
155
    {
156
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'Microsoft Word Document',
157
      'application/vnd.oasis.opendocument.spreadsheet' => 'OpenDocument Spreadsheet',
158
      # Content types sent by browsers may contain uppercase letters
159
      'application/vnd.ms-excel.sheet.macroEnabled.12' => 'Microsoft Excel Workbook (+macro)'
160
    }.each do |content_type, name|
161
      Attachment.find(4).update_column(:content_type, content_type)
162

  
163
      get(:show, :params => {:id => 4})
164
      assert_response :success
165
      assert_select 'div.attachments span.content-type[title=?]', content_type, :text => name
166
    end
167
  end
168

  
169
  def test_show_should_display_other_content_types_as_is
170
    get(:show, :params => {:id => 4})
171
    assert_response :success
172
    assert_select 'div.attachments span.content-type', :text => 'application/x-ruby'
173
    assert_select 'div.attachments span.content-type[title]', 0
174
  end
175

  
154 176
  def test_show_should_not_display_content_type_if_blank
155 177
    get(:show, :params => {:id => 22})
156 178
    assert_response :success
(5-5/7)