Skip to content

Commit bb07bd4

Browse files
Inline all text attachments (#65)
* Slightly clean up patch detection code Makes the code for detecting if an attachment is a patch or have a file extension indicating that it's a patch slightly nicer. * Inline all plain text attachments Not only patches.
1 parent 10accf0 commit bb07bd4

3 files changed

Lines changed: 30 additions & 10 deletions

File tree

app/models/attachment.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,31 @@ class Attachment < ApplicationRecord
55
after_create :mark_topic_has_attachments
66
after_destroy :update_topic_has_attachments
77

8+
# This list is in no way comprehensive. It can be extended as needed.
9+
TEXT_APPLICATION_TYPES = %w[
10+
application/json
11+
application/sql
12+
application/x-perl
13+
application/x-python
14+
application/x-ruby
15+
application/x-sh
16+
application/x-shellscript
17+
application/xml
18+
application/yaml
19+
].freeze
20+
21+
def text?
22+
return true if content_type&.start_with?("text/")
23+
return true if content_type&.end_with?("+xml", "+json")
24+
TEXT_APPLICATION_TYPES.include?(content_type)
25+
end
26+
827
def patch?
9-
file_name&.ends_with?(".patch") || file_name&.ends_with?(".diff") || patch_content?
28+
patch_extension? || patch_content?
1029
end
1130

1231
def patch_extension?
13-
file_name&.ends_with?(".patch") || file_name&.ends_with?(".diff")
32+
file_name&.ends_with?(".patch", ".diff")
1433
end
1534

1635
def decoded_body

app/views/topics/_message.html.slim

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,21 @@
6565
.message-attachments id="message-#{message.id}-attachments"
6666
h4 Attachments:
6767
- message.attachments.each do |attachment|
68-
- if attachment.patch?
68+
- if attachment.patch? || attachment.text?
6969
details.attachment
7070
summary.attachment-info
7171
span.attachment-summary-row
7272
span.filename = attachment.file_name
7373
span.content-type = attachment.content_type if attachment.content_type
7474
= link_to "Download", attachment_path(attachment), class: "attachment-download", download: attachment.file_name, data: { turbo: false }
75-
- stats = attachment.diff_line_stats
76-
- if stats[:added].positive? || stats[:removed].positive?
77-
span.patchset-stats aria-label="Patch line changes" title="Lines added and removed by this patch"
78-
span.patchset-added +#{stats[:added]}
79-
span.patchset-removed -#{stats[:removed]}
75+
- if attachment.patch?
76+
- stats = attachment.diff_line_stats
77+
- if stats[:added].positive? || stats[:removed].positive?
78+
span.patchset-stats aria-label="Patch line changes" title="Lines added and removed by this patch"
79+
span.patchset-added +#{stats[:added]}
80+
span.patchset-removed -#{stats[:removed]}
8081
pre.attachment-content
81-
code.language-diff data-controller="diff-highlight"
82+
code data-controller=("diff-highlight" if attachment.patch?)
8283
= attachment.decoded_body_utf8
8384
- else
8485
.attachment

spec/requests/topics_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def attach_verified_alias(user, email:, primary: true)
133133

134134
expect(response).to have_http_status(:success)
135135
expect(response.body).to include("Attachments:")
136-
expect(response.body).to include('class="language-diff"')
136+
expect(response.body).to include('data-controller="diff-highlight"')
137137
expect(response.body).to include("diff --git")
138138
end
139139
end

0 commit comments

Comments
 (0)