From 42a39b224c895bdf6b9734c3c9e913e45d5c3c09 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Fri, 21 Nov 2025 15:49:14 +0100 Subject: [PATCH 1/3] Cap the max number of unread notifications to 500 Simpler than adding 2 levels of pagination here --- app/controllers/notifications_controller.rb | 4 ++- app/models/notification.rb | 1 + app/views/notifications/index.html.erb | 26 ++----------------- .../index/_read_notifications.html.erb | 7 +++++ .../index/_unread_notifications.html.erb | 23 ++++++++++++++++ 5 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 app/views/notifications/index/_read_notifications.html.erb create mode 100644 app/views/notifications/index/_unread_notifications.html.erb diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 8adb04b3c..76bd62974 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -1,6 +1,8 @@ class NotificationsController < ApplicationController + MAX_UNREAD_NOTIFICATIONS = 500 + def index - @unread = Current.user.notifications.unread.ordered.preloaded unless current_page_param + @unread = Current.user.notifications.unread.ordered.preloaded.limit(MAX_UNREAD_NOTIFICATIONS) unless current_page_param set_page_and_extract_portion_from Current.user.notifications.read.ordered.preloaded respond_to do |format| diff --git a/app/models/notification.rb b/app/models/notification.rb index 4b69dd6e6..5cd3802a4 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -9,6 +9,7 @@ class Notification < ApplicationRecord scope :unread, -> { where(read_at: nil) } scope :read, -> { where.not(read_at: nil) } scope :ordered, -> { order(read_at: :desc, created_at: :desc) } + scope :with_unread_first, -> { order(Arel.sql("CASE WHEN read_at IS NULL THEN 0 ELSE 1 END")) } after_create_commit :broadcast_unread after_destroy_commit :broadcast_read diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index eae50830b..70fee45b8 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -13,30 +13,8 @@
-
- <% if @unread.any? %> -
-

New for you

- <%= button_to "Mark all as read", bulk_reading_path, class: "btn txt-small", form: { data: { turbo: false } }, data: { action: "badge#clear" } %> -
- <% else %> -
- Nothing new for you. -
- <% end %> - -
- <%= render partial: "notifications/index/notification", collection: @unread, cached: true %> -
-
- -
-

Previously seen

- -
- <%= render partial: "notifications/index/notification", collection: @page.records, cached: true %> -
-
+ <%= render "notifications/index/unread_notifications", unread: @unread if @unread %> + <%= render "notifications/index/read_notifications", page: @page %>
<%= notifications_next_page_link(@page) if @page.records.any? %> diff --git a/app/views/notifications/index/_read_notifications.html.erb b/app/views/notifications/index/_read_notifications.html.erb new file mode 100644 index 000000000..c5e282237 --- /dev/null +++ b/app/views/notifications/index/_read_notifications.html.erb @@ -0,0 +1,7 @@ +
+

Previously seen

+ +
+ <%= render partial: "notifications/index/notification", collection: page.records, cached: true %> +
+
diff --git a/app/views/notifications/index/_unread_notifications.html.erb b/app/views/notifications/index/_unread_notifications.html.erb new file mode 100644 index 000000000..4fbfdba7d --- /dev/null +++ b/app/views/notifications/index/_unread_notifications.html.erb @@ -0,0 +1,23 @@ +
+ <% if unread.any? %> +
+

New for you

+ <%= button_to "Mark all as read", bulk_reading_path, class: "btn txt-small", form: { data: { turbo: false } }, data: { action: "badge#clear" } %> +
+ + <% total_unread_count = Current.user.notifications.unread.count %> + <% if Current.user.notifications.unread.count > NotificationsController::MAX_UNREAD_NOTIFICATIONS %> + + <% end %> + <% else %> +
+ Nothing new for you. +
+ <% end %> + +
+ <%= render partial: "notifications/index/notification", collection: unread, cached: true %> +
+
From c0920f046fb28a00cb915d9cf249fd2e9d94b94e Mon Sep 17 00:00:00 2001 From: Jason Zimdars Date: Fri, 21 Nov 2025 09:29:21 -0600 Subject: [PATCH 2/3] Move to bottom, use utility classes --- .../index/_unread_notifications.html.erb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/views/notifications/index/_unread_notifications.html.erb b/app/views/notifications/index/_unread_notifications.html.erb index 4fbfdba7d..9a9898788 100644 --- a/app/views/notifications/index/_unread_notifications.html.erb +++ b/app/views/notifications/index/_unread_notifications.html.erb @@ -4,13 +4,6 @@

New for you

<%= button_to "Mark all as read", bulk_reading_path, class: "btn txt-small", form: { data: { turbo: false } }, data: { action: "badge#clear" } %> - - <% total_unread_count = Current.user.notifications.unread.count %> - <% if Current.user.notifications.unread.count > NotificationsController::MAX_UNREAD_NOTIFICATIONS %> - - <% end %> <% else %>
Nothing new for you. @@ -20,4 +13,13 @@
<%= render partial: "notifications/index/notification", collection: unread, cached: true %>
+ + <% if unread.any? %> + <% total_unread_count = Current.user.notifications.unread.count %> + <% if Current.user.notifications.unread.count > NotificationsController::MAX_UNREAD_NOTIFICATIONS %> +
+ Showing the <%= NotificationsController::MAX_UNREAD_NOTIFICATIONS %> most recent (<%= total_unread_count - NotificationsController::MAX_UNREAD_NOTIFICATIONS %> are hidden) +
+ <% end %> + <% end %> From 95b555869bf2d7d3d0079da4cde72975abf06cc3 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Fri, 21 Nov 2025 16:31:06 +0100 Subject: [PATCH 3/3] not used --- app/models/notification.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index 5cd3802a4..4b69dd6e6 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -9,7 +9,6 @@ class Notification < ApplicationRecord scope :unread, -> { where(read_at: nil) } scope :read, -> { where.not(read_at: nil) } scope :ordered, -> { order(read_at: :desc, created_at: :desc) } - scope :with_unread_first, -> { order(Arel.sql("CASE WHEN read_at IS NULL THEN 0 ELSE 1 END")) } after_create_commit :broadcast_unread after_destroy_commit :broadcast_read