Skip to content

Commit 7be47b8

Browse files
committed
Show attachments in HTML
1 parent 83eac10 commit 7be47b8

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

app/views/messages/_message.html.erb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,63 @@
2323
<div class="px-6 py-4">
2424
<pre class="whitespace-pre-wrap font-mono text-sm text-gray-800 dark:text-gray-300 leading-relaxed"><%= message.body %></pre>
2525
</div>
26+
27+
<% if message.attachments.attached? %>
28+
<div class="border-t border-gray-200 dark:border-gray-700 px-6 py-4 bg-gray-50 dark:bg-gray-900">
29+
<h3 class="text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">Attachments (<%= message.attachments.count %>)</h3>
30+
<div class="space-y-4">
31+
<% message.attachments.each do |attachment| %>
32+
<% if attachment.content_type&.start_with?('image/') %>
33+
<div class="space-y-2">
34+
<div class="flex items-center gap-3 text-sm">
35+
<svg class="w-4 h-4 text-gray-400 dark:text-gray-500 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
36+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path>
37+
</svg>
38+
<%= link_to attachment.filename.to_s, rails_blob_path(attachment, disposition: "attachment"), class: "text-red-600 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300 font-medium" %>
39+
<span class="text-gray-500 dark:text-gray-400">(<%= number_to_human_size(attachment.byte_size) %>)</span>
40+
</div>
41+
<%= image_tag rails_blob_path(attachment), class: "max-w-full h-auto rounded border border-gray-300 dark:border-gray-600", alt: attachment.filename.to_s %>
42+
</div>
43+
<% elsif attachment.content_type&.start_with?('text/') %>
44+
<div class="space-y-2">
45+
<div class="flex items-center gap-3 text-sm">
46+
<svg class="w-4 h-4 text-gray-400 dark:text-gray-500 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
47+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
48+
</svg>
49+
<%= link_to attachment.filename.to_s, rails_blob_path(attachment, disposition: "attachment"), class: "text-red-600 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300 font-medium" %>
50+
<span class="text-gray-500 dark:text-gray-400">(<%= number_to_human_size(attachment.byte_size) %>, <%= attachment.content_type %>)</span>
51+
</div>
52+
<% if attachment.byte_size < 100_000 %>
53+
<pre class="whitespace-pre-wrap font-mono text-sm text-gray-800 dark:text-gray-300 bg-gray-100 dark:bg-gray-800 p-4 rounded border border-gray-300 dark:border-gray-600 overflow-x-auto"><%= attachment.download %></pre>
54+
<% end %>
55+
</div>
56+
<% elsif attachment.content_type == 'application/pdf' %>
57+
<div class="flex items-center gap-3 text-sm">
58+
<svg class="w-4 h-4 text-red-500 dark:text-red-400 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
59+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path>
60+
</svg>
61+
<%= link_to attachment.filename.to_s, rails_blob_path(attachment, disposition: "attachment"), class: "text-red-600 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300 font-medium" %>
62+
<span class="text-gray-500 dark:text-gray-400">(<%= number_to_human_size(attachment.byte_size) %>, PDF)</span>
63+
</div>
64+
<% elsif attachment.content_type&.match?(/zip|tar|gzip|compress|bzip|lha/) %>
65+
<div class="flex items-center gap-3 text-sm">
66+
<svg class="w-4 h-4 text-gray-400 dark:text-gray-500 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
67+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 19a2 2 0 01-2-2V7a2 2 0 012-2h4l2 2h4a2 2 0 012 2v1M5 19h14a2 2 0 002-2v-5a2 2 0 00-2-2H9a2 2 0 00-2 2v5a2 2 0 01-2 2z"></path>
68+
</svg>
69+
<%= link_to attachment.filename.to_s, rails_blob_path(attachment, disposition: "attachment"), class: "text-red-600 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300 font-medium" %>
70+
<span class="text-gray-500 dark:text-gray-400">(<%= number_to_human_size(attachment.byte_size) %>, Archive)</span>
71+
</div>
72+
<% else %>
73+
<div class="flex items-center gap-3 text-sm">
74+
<svg class="w-4 h-4 text-gray-400 dark:text-gray-500 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
75+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"></path>
76+
</svg>
77+
<%= link_to attachment.filename.to_s, rails_blob_path(attachment, disposition: "attachment"), class: "text-red-600 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300 font-medium" %>
78+
<span class="text-gray-500 dark:text-gray-400">(<%= number_to_human_size(attachment.byte_size) %><% if attachment.content_type %>, <%= attachment.content_type %><% end %>)</span>
79+
</div>
80+
<% end %>
81+
<% end %>
82+
</div>
83+
</div>
84+
<% end %>
2685
</div>

test/system/messages_test.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22

33
class MessagesTest < ApplicationSystemTestCase
44
setup do
5-
@message = messages(:message1)
5+
@message1 = messages(:message1)
6+
@message2 = messages(:message2)
7+
end
8+
9+
test 'visiting ruby-dev index, and showing a message with an attachment' do
10+
visit '/ruby-dev'
11+
assert_content @message2.subject
12+
13+
click_link @message2.subject
14+
assert_content @message2.body
15+
16+
click_link @message2.attachments_attachments.first.blob.filename.to_s
617
end
718

819
test "visiting the index" do

0 commit comments

Comments
 (0)