From 620cd0f56e51f46e943f5a6a5b0cd915eba108a6 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Tue, 14 Jul 2026 18:23:00 +0100 Subject: [PATCH] Avoid showing very long pieces of text in notifications I am unsure about this one. I am relying on `RichText#truncate_html`, to avoid having to put together my own truncation facility. My problem is that I find that method to be too coarse-grained as it works by removing entire HTML elements: - If the text starts with a small paragraph, then a long one, it may only show the first. - If it starts with a long paragraph, it may not show anything, or it will show the whole paragraph. I think this is confusing. Note that can't just use Rails's own `String#truncate` as this is a `RichText`, not a `String`, and then we'd have to worry about truncating the HTML. Having said that: `#truncate_html` is right there, and it is what we use for diary entries. So I guess let's reuse it, leaving it for a future contribution to rework it into something better, if there's inclination. --- .../notifications/_changeset_comment.html.erb | 2 +- .../notifications/_diary_comment.html.erb | 2 +- .../notifications/_note_comment.html.erb | 2 +- .../_notification_quote.html.erb | 6 ++++- .../changeset_comment_view_test.rb | 23 +++++++++++++++++++ .../notifications/diary_comment_view_test.rb | 22 ++++++++++++++++++ .../notifications/note_comment_view_test.rb | 22 ++++++++++++++++++ test/fixtures/lorem_ipsum.txt | 11 +++++++++ test/test_helper.rb | 4 ++++ 9 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/lorem_ipsum.txt diff --git a/app/views/notifications/_changeset_comment.html.erb b/app/views/notifications/_changeset_comment.html.erb index d441ad1c3..2d5ab9093 100644 --- a/app/views/notifications/_changeset_comment.html.erb +++ b/app/views/notifications/_changeset_comment.html.erb @@ -36,5 +36,5 @@ ) %> <% end %>

- <%= render "notifications/notification_quote", :body => record.body.to_html %> + <%= render "notifications/notification_quote", :body => record.body %> <% end %> diff --git a/app/views/notifications/_diary_comment.html.erb b/app/views/notifications/_diary_comment.html.erb index 30795b62f..8af11db06 100644 --- a/app/views/notifications/_diary_comment.html.erb +++ b/app/views/notifications/_diary_comment.html.erb @@ -31,5 +31,5 @@ ) ) %>

- <%= render "notifications/notification_quote", :body => record.body.to_html %> + <%= render "notifications/notification_quote", :body => record.body %> <% end %> diff --git a/app/views/notifications/_note_comment.html.erb b/app/views/notifications/_note_comment.html.erb index 7cb969c75..2e6a14b6b 100644 --- a/app/views/notifications/_note_comment.html.erb +++ b/app/views/notifications/_note_comment.html.erb @@ -26,6 +26,6 @@ ) %>

<% if record.body.present? %> - <%= render "notifications/notification_quote", :body => record.body.to_html %> + <%= render "notifications/notification_quote", :body => record.body %> <% end %> <% end %> diff --git a/app/views/notifications/_notification_quote.html.erb b/app/views/notifications/_notification_quote.html.erb index 8498a4c35..f4cd1724b 100644 --- a/app/views/notifications/_notification_quote.html.erb +++ b/app/views/notifications/_notification_quote.html.erb @@ -1,5 +1,9 @@ <%# locals: (body: nil) %>
- <%= body %> + <% truncated_entry = body.truncate_html(1000) %> + <%= truncated_entry[:html] %> + <% if truncated_entry[:truncated] %> +

+ <% end %>
diff --git a/test/controllers/notifications/changeset_comment_view_test.rb b/test/controllers/notifications/changeset_comment_view_test.rb index 8118da841..58a5ce889 100644 --- a/test/controllers/notifications/changeset_comment_view_test.rb +++ b/test/controllers/notifications/changeset_comment_view_test.rb @@ -51,5 +51,28 @@ module Notifications assert_dom "blockquote", "Insightful comment" end end + + def test_render_with_long_text + comment_author = build_stubbed( + :user, + :display_name => "Helpful Commenter" + ) + changeset = build_stubbed(:changeset) + changeset_comment = build_stubbed( + :changeset_comment, + :author => comment_author, + :changeset => changeset, + :body => read_fixture_file("lorem_ipsum.txt") + ) + + notification = build_stubbed(:notification, :record => changeset_comment) + + render( + "notifications/notification", + :notification => notification + ) + + assert_dom ".web-notification blockquote p", :count => 3 + end end end diff --git a/test/controllers/notifications/diary_comment_view_test.rb b/test/controllers/notifications/diary_comment_view_test.rb index d947db746..6ff5fea26 100644 --- a/test/controllers/notifications/diary_comment_view_test.rb +++ b/test/controllers/notifications/diary_comment_view_test.rb @@ -19,5 +19,27 @@ module Notifications assert_dom "blockquote", diary_comment.body end end + + def test_render_with_long_text + comment_user = build_stubbed( + :user, + :display_name => "Helpful Commenter" + ) + diary_entry = build_stubbed(:diary_entry) + diary_comment = build_stubbed( + :diary_comment, + :user => comment_user, + :diary_entry => diary_entry, + :body => read_fixture_file("lorem_ipsum.txt") + ) + notification = build_stubbed(:notification, :record => diary_comment) + + render( + "notifications/notification", + :notification => notification + ) + + assert_dom ".web-notification blockquote p", :count => 3 + end end end diff --git a/test/controllers/notifications/note_comment_view_test.rb b/test/controllers/notifications/note_comment_view_test.rb index edee15d19..9c674ff50 100644 --- a/test/controllers/notifications/note_comment_view_test.rb +++ b/test/controllers/notifications/note_comment_view_test.rb @@ -24,6 +24,28 @@ module Notifications end end + def test_render_commented_with_long_text + comment_author = build_stubbed( + :user, + :display_name => "Helpful Commenter" + ) + note = build_stubbed(:note) + note_comment = build_stubbed( + :note_comment, + :author => comment_author, + :note => note, + :body => read_fixture_file("lorem_ipsum.txt") + ) + notification = build_stubbed(:notification, :record => note_comment) + + render( + "notifications/notification", + :notification => notification + ) + + assert_dom ".web-notification blockquote p", :count => 3 + end + def test_render_closed_with_comment comment_author = build_stubbed( :user, diff --git a/test/fixtures/lorem_ipsum.txt b/test/fixtures/lorem_ipsum.txt new file mode 100644 index 000000000..56b070a11 --- /dev/null +++ b/test/fixtures/lorem_ipsum.txt @@ -0,0 +1,11 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ullamcorper id mauris non consectetur. + +Nam ut sollicitudin est. Nulla quis facilisis neque. Praesent pellentesque euismod mauris, nec volutpat erat convallis in. Vestibulum eleifend nulla id diam faucibus, id finibus lectus molestie. Suspendisse potenti. Quisque in dapibus purus. Vivamus viverra vel diam eu vehicula. Mauris fermentum dui sit amet sapien efficitur, vitae molestie diam commodo. Sed vel auctor leo, eu scelerisque enim. Pellentesque tincidunt euismod dignissim. Aliquam sed odio quis nibh feugiat pellentesque non ac nisi. + +Nullam gravida, felis sed porttitor faucibus, massa libero efficitur dolor, at gravida est turpis nec diam. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Mauris gravida tincidunt tortor, in pulvinar nisl viverra in. Morbi dignissim quis dui quis elementum. Maecenas suscipit ex in finibus egestas. Curabitur elementum scelerisque elit, eu auctor turpis feugiat mollis. Quisque in malesuada mi, nec vulputate mi. Nunc imperdiet auctor eros at consequat. Aenean eleifend nibh a euismod dictum. Fusce mauris nisl, mollis porttitor aliquam vitae, sodales eget enim. Aliquam erat volutpat. Quisque eu velit mi. Aliquam convallis mattis ligula convallis convallis. + +Aliquam quis ex eget purus bibendum egestas. Quisque sit amet sapien accumsan, mattis mauris eu, bibendum turpis. Vestibulum libero est, tincidunt efficitur turpis vel, viverra interdum orci. Aenean vitae vehicula mi. Aliquam ullamcorper non ante a congue. Phasellus at molestie libero. Sed vulputate lobortis porttitor. Curabitur mattis sapien in viverra sodales. Vestibulum pretium non lectus eget dapibus. In et elit mattis, imperdiet augue in, interdum erat. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus porttitor quis lorem ut varius. Aliquam posuere ultrices nunc non interdum. Nulla id eros nibh. + +Sed mauris libero, iaculis vitae volutpat nec, mollis eget augue. Vivamus ligula est, blandit pulvinar sagittis ac, efficitur ut justo. Integer id ipsum mi. Suspendisse potenti. Mauris et est justo. Praesent posuere augue vitae porta iaculis. Cras pharetra sed justo molestie fermentum. Cras imperdiet lorem eu pretium sagittis. Praesent sit amet vulputate ligula. + +Praesent ligula orci, facilisis ut ante quis, molestie sagittis dolor. Nunc sagittis arcu at leo mollis varius. Donec pellentesque cursus lectus a ullamcorper. Duis id lorem nec turpis scelerisque feugiat. Etiam vel pretium purus, ut mollis odio. In placerat ullamcorper ligula. Nullam lobortis lorem quam, ut pellentesque mauris cursus nec. Pellentesque ipsum ex, aliquam ac iaculis eu, blandit vitae sapien. Pellentesque tincidunt ultrices vulputate. Phasellus vulputate elit eros, sed cursus velit semper eget. Duis vel elit luctus, maximus ipsum eget, dapibus nisi. Sed imperdiet ex nibh, vitae viverra dui elementum vel. Curabitur dapibus ultrices augue, sed congue mauris aliquam at. diff --git a/test/test_helper.rb b/test/test_helper.rb index e2c15b604..e1c1ee56c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -423,5 +423,9 @@ module ActiveSupport def parse_html(html) Rails::Dom::Testing.html_document_fragment.parse(html) end + + def read_fixture_file(relpath) + Rails.root.join("test", "fixtures", relpath).read + end end end -- 2.47.3