Show a file in a message's rich text by its name, whatever its variants

Showing an image's preview when its variant had already been made tied the cached message to state
that changes without touching it: a variant made after the message was cached would never show. It also
cost a query per embedded image, since Action Text loads the embeds without their variant records.
Nothing in the app makes those variants, so the partial no longer looks at them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Bj8KnxpTf9sj2Ysa8aLAVa
This commit is contained in:
Marcello Costagliola
2026-10-06 16:02:35 +02:00
parent 15f56134e7
commit c48083dcfe
2 changed files with 12 additions and 13 deletions
+3 -10
View File
@@ -1,14 +1,7 @@
<%# Files are posted as a message's own attachment, which gets its preview when it's posted; the composer adds none to
the rich text. One that's there anyway shows a preview only if it was already made, rather than linking to a URL
that makes it, on any view, for as long as it fails. %>
<% preview = blob.variant(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) if blob.variable? %>
<% preview = nil unless preview&.processed? %>
<figure class="attachment attachment--<%= preview ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if preview %>
<%= image_tag preview, alt: blob.try(:alt) %>
<% end %>
the rich text. One that's there anyway is shown by its name, with no preview: Action Text's partial links to a URL
that makes one on view, and nothing else in the app would make it. %>
<figure class="attachment attachment--file attachment--<%= blob.filename.extension %>">
<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
+9 -3
View File
@@ -1,6 +1,8 @@
require "test_helper"
class MessagesHelperTest < ActionView::TestCase
include ActiveRecord::Assertions::QueryAssertions
test "message_presentation neutralizes unsafe URI schemes in links" do
message = Message.create! room: rooms(:pets), body: '<div><a href="javascript:alert(1)">x</a></div>', client_message_id: "0015", creator: users(:jason)
@@ -26,19 +28,23 @@ class MessagesHelperTest < ActionView::TestCase
end
test "message_presentation shows an image in the rich text as a file, rather than making its preview on view" do
presentation = view.message_presentation(message_with_file_in_rich_text("moon.jpg", "image/jpeg"))
message = Message.with_attachment_details.find(message_with_file_in_rich_text("moon.jpg", "image/jpeg").id)
presentation = nil
assert_no_queries_match(/active_storage_variant_records/) { presentation = view.message_presentation(message) }
assert_no_match %r{/representations/}, presentation
assert_match %r{<span class="attachment__name">moon\.jpg</span>}, presentation
end
test "message_presentation shows an image in the rich text whose preview was already made" do
test "message_presentation shows an image in the rich text as a file even when its preview was made" do
message = message_with_file_in_rich_text("moon.jpg", "image/jpeg")
message.body.embeds.first.blob.variant(resize_to_limit: [ 1024, 768 ]).processed
presentation = view.message_presentation(message.reload)
assert_match %r{<img[^>]+src="[^"]*/representations/[^"]*moon\.jpg"}, presentation
assert_no_match %r{/representations/}, presentation
assert_match %r{<span class="attachment__name">moon\.jpg</span>}, presentation
end
test "message_presentation shows a video in the rich text as a file" do