From 9a7e14e2e952ed8c160f32308eb512329a067c8e Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Wed, 13 May 2026 11:00:50 +0100 Subject: [PATCH] Paginate notifications --- app/controllers/notifications_controller.rb | 6 ++++- app/models/user_notifications.rb | 9 ++++---- app/views/notifications/_page.html.erb | 10 +++++++++ app/views/notifications/index.html.erb | 9 ++------ config/locales/en.yml | 5 +++++ test/system/web_notifications_test.rb | 25 +++++++++++++++++++++ 6 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 app/views/notifications/_page.html.erb diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 602841383..11e630c40 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class NotificationsController < ApplicationController + include PaginationMethods + layout :site_layout before_action :authorize_web @@ -11,6 +13,8 @@ class NotificationsController < ApplicationController before_action :check_database_readable def index - @notifications = UserNotifications.new(current_user) + records = UserNotifications.new(current_user).notification_records + @notifications = get_page_items(records) + @params = params.permit end end diff --git a/app/models/user_notifications.rb b/app/models/user_notifications.rb index fac6656eb..28a6ddd44 100644 --- a/app/models/user_notifications.rb +++ b/app/models/user_notifications.rb @@ -165,17 +165,18 @@ class UserNotifications NoteCommentNotifier::Notification ].freeze + def self.wrap(notification_records) + notification_records.map { |record| Notification.from(record) } + end + def initialize(user) @user = user end - def each(&) + def notification_records(&) @user .notifications .where(:type => LISTABLE_NOTIFICATIONS) - .newest_first - .map { |instance| Notification.from(instance) } - .each(&) end def empty? diff --git a/app/views/notifications/_page.html.erb b/app/views/notifications/_page.html.erb new file mode 100644 index 000000000..b07a5c29c --- /dev/null +++ b/app/views/notifications/_page.html.erb @@ -0,0 +1,10 @@ +<%# locals: (notifications:, params:) %> + + + <% UserNotifications.wrap(notifications.items).each do |notification| %> + <%= render :partial => notification.to_partial_path, :object => notification, :as => :notification %> +
+ <% end %> + + <%= render "pagination", :paginator => notifications, :params => params %> +
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index 1240a1b62..a403d35f7 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -8,13 +8,8 @@

<% end %> -<% if @notifications.empty? %> +<% if @notifications.items.empty? %>

<%= t(".no_notifications") %>

<% else %> -
- <% @notifications.each do |notification| %> - <%= render :partial => notification.to_partial_path, :object => notification, :as => :notification %> -
- <% end %> -
+ <%= render "page", :notifications => @notifications, :params => @params %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index ba0bf2c80..f1a414f42 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3130,6 +3130,11 @@ en: newer: Newer Entries oldest: Oldest Entries newest: Newest Entries + notifications: + older: Older Notifications + newer: Newer Notifications + oldest: Oldest Notifications + newest: Newest Notifications issues: older: Older Issues newer: Newer Issues diff --git a/test/system/web_notifications_test.rb b/test/system/web_notifications_test.rb index 187a4c8a4..48719523d 100644 --- a/test/system/web_notifications_test.rb +++ b/test/system/web_notifications_test.rb @@ -32,6 +32,31 @@ class WebNotificationsTest < ApplicationSystemTestCase assert_text "User Commenter left a comment on changeset" end + test "pagination" do + changeset_author = create(:user) + commenter = create(:user, :display_name => "Commenter") + 1.upto(30).each do |i| + setup_changeset_comment( + :changeset_author => changeset_author, + :commenter => commenter, + :comment_attrs => { + :body => "This is comment number #{i}" + } + ) + end + + sign_in_as(changeset_author) + + click_on changeset_author.display_name + click_on "My Notifications" + + assert_text "This is comment number 30" + assert_no_text "This is comment number 10" + click_on "Older Notifications" + assert_no_text "This is comment number 30" + assert_text "This is comment number 10" + end + private def setup_changeset_comment(changeset_author:, commenter:, comment_attrs: {}) -- 2.47.3