From 89fe7d00aa3d2ba4191363c23889bf59de776b68 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Mon, 28 Sep 2026 11:25:37 +0100 Subject: [PATCH] Don't break when a notification record goes missing --- app/helpers/notifications_helper.rb | 14 +++++++++--- app/notifiers/application_notifier.rb | 5 +++++ app/notifiers/gpx_import_failure_notifier.rb | 6 +++++ .../notifications/_missing_record.html.erb | 12 ++++++++++ .../notifications/_notification.html.erb | 17 +++++++++----- .../_notification_header.html.erb | 4 ++-- config/locales/en.yml | 2 ++ .../gpx_import_success_view_test.rb | 22 +++++++++++++++++++ .../notifications/new_follower_view_test.rb | 19 ++++++++++++++++ 9 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 app/views/notifications/_missing_record.html.erb diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 426923946..a6e177ebe 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -2,9 +2,17 @@ module NotificationsHelper def partial_path_for_notification(notification) - # Turn "ChangesetCommentNotifier::Notification" into "ChangesetComment" - event_type_name = notification.class.name.sub("Notifier::Notification", "") + event_type_name = notification_event_type_name(notification) + + "notifications/#{event_type_name}" + end - "notifications/#{event_type_name.underscore}" + def notification_event_type_name(notification) + # Turn "ChangesetCommentNotifier::Notification" into "ChangesetComment" + notification + .class + .name + .sub("Notifier::Notification", "") + .underscore end end diff --git a/app/notifiers/application_notifier.rb b/app/notifiers/application_notifier.rb index 427711288..640b6907e 100644 --- a/app/notifiers/application_notifier.rb +++ b/app/notifiers/application_notifier.rb @@ -1,4 +1,9 @@ # frozen_string_literal: true class ApplicationNotifier < Noticed::Event + notification_methods do + def record_required? + true + end + end end diff --git a/app/notifiers/gpx_import_failure_notifier.rb b/app/notifiers/gpx_import_failure_notifier.rb index 0b6cbe101..b460c2184 100644 --- a/app/notifiers/gpx_import_failure_notifier.rb +++ b/app/notifiers/gpx_import_failure_notifier.rb @@ -6,4 +6,10 @@ class GpxImportFailureNotifier < ApplicationNotifier config.method = "gpx_failure" config.if = -> { recipient.notification_preferences.gpx_import_failure.include?("email") } end + + notification_methods do + def record_required? + false + end + end end diff --git a/app/views/notifications/_missing_record.html.erb b/app/views/notifications/_missing_record.html.erb new file mode 100644 index 000000000..0c721cde7 --- /dev/null +++ b/app/views/notifications/_missing_record.html.erb @@ -0,0 +1,12 @@ +<%# locals: (notification:) %> + +<%= render( + "notifications/notification_header", + :title => t("headline", :scope => ["notifications", notification_event_type_name(notification)]), + :timestamp => notification.created_at, + :missing_record => true + ) %> + +<%= render("notifications/notification_body") do %> +

<%= t("notifications.missing_record.body") %>

+<% end %> diff --git a/app/views/notifications/_notification.html.erb b/app/views/notifications/_notification.html.erb index 4282d868a..93550850d 100644 --- a/app/views/notifications/_notification.html.erb +++ b/app/views/notifications/_notification.html.erb @@ -1,9 +1,16 @@ <%# locals: (notification:) %>
- <%= render( - partial_path_for_notification(notification), - :notification => notification, - :record => notification.record - ) %> + <% if notification.record || !notification.record_required? %> + <%= render( + partial_path_for_notification(notification), + :notification => notification, + :record => notification.record + ) %> + <% else %> + <%= render( + "notifications/missing_record", + :notification => notification + ) %> + <% end %>
diff --git a/app/views/notifications/_notification_header.html.erb b/app/views/notifications/_notification_header.html.erb index fceca6432..3e694e95d 100644 --- a/app/views/notifications/_notification_header.html.erb +++ b/app/views/notifications/_notification_header.html.erb @@ -1,6 +1,6 @@ -<%# locals: (title:, uri: nil, timestamp:) %> +<%# locals: (title:, uri: nil, timestamp:, missing_record: false) %> -
+
missing_record) %>">

<%= link_to_if(uri, title, uri) %>

<%= friendly_date_ago(timestamp) %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index 37cfd9ed0..b493390ac 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -674,6 +674,8 @@ en: nearby_changesets: "nearby user changesets" nearby_diaries: "nearby user diary entries" notifications: + missing_record: + body: Unfortunately this is not available any more index: title: Notifications no_notifications: You have no notifications at the moment diff --git a/test/controllers/notifications/gpx_import_success_view_test.rb b/test/controllers/notifications/gpx_import_success_view_test.rb index 36aa31f59..a72f5166d 100644 --- a/test/controllers/notifications/gpx_import_success_view_test.rb +++ b/test/controllers/notifications/gpx_import_success_view_test.rb @@ -32,5 +32,27 @@ module Notifications assert_dom "dd", "5" end end + + def test_render_deleted + notification = build_stubbed( + :notification, + :record => nil, + :notifier_class => GpxImportSuccessNotifier, + :notifier_params => { + :possible_points => 5 + } + ) + + render( + "notifications/notification", + :notification => notification + ) + + assert_dom ".web-notification" do + assert_dom "h2", "GPS trace imported successfully" + assert_dom "time", "less than 1 minute ago" + assert_dom "p", "Unfortunately this is not available any more" + end + end end end diff --git a/test/controllers/notifications/new_follower_view_test.rb b/test/controllers/notifications/new_follower_view_test.rb index 2a7a0e588..7db4bb895 100644 --- a/test/controllers/notifications/new_follower_view_test.rb +++ b/test/controllers/notifications/new_follower_view_test.rb @@ -25,5 +25,24 @@ module Notifications assert_dom "p", "User Follower started following you. You can follow them back if you wish." end end + + def test_render_unfollowed + notification = build_stubbed( + :notification, + :record => nil, + :notifier_class => NewFollowerNotifier + ) + + render( + "notifications/notification", + :notification => notification + ) + + assert_dom ".web-notification" do + assert_dom "h2", "New follower" + assert_dom "time", "less than 1 minute ago" + assert_dom "p", "Unfortunately this is not available any more" + end + end end end -- 2.47.3