From a5de4863fa7bd22a1ac0a35fc8a9b77982e784fc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 17 Apr 2025 11:39:05 +0200 Subject: [PATCH] Allow for multiple notifications to be read cc @kevinmcconnell --- app/controllers/cards/readings_controller.rb | 4 ++-- app/views/cards/readings/create.turbo_stream.erb | 4 ++-- test/controllers/cards/readings_controller_test.rb | 12 +++++++++++- test/fixtures/notifications.yml | 7 +++++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/controllers/cards/readings_controller.rb b/app/controllers/cards/readings_controller.rb index dbb379b15..2d8b51ac6 100644 --- a/app/controllers/cards/readings_controller.rb +++ b/app/controllers/cards/readings_controller.rb @@ -2,7 +2,7 @@ class Cards::ReadingsController < ApplicationController include CardScoped def create - Current.user.notifications.unread.where(card: @card).read_all - @notifications = Current.user.notifications.unread.ordered.limit(20) + @notifications = Current.user.notifications.where(card: @card) + @notifications.each(&:read) end end diff --git a/app/views/cards/readings/create.turbo_stream.erb b/app/views/cards/readings/create.turbo_stream.erb index 8f758752e..5d7fe7801 100644 --- a/app/views/cards/readings/create.turbo_stream.erb +++ b/app/views/cards/readings/create.turbo_stream.erb @@ -1,3 +1,3 @@ -<%= turbo_stream.update "notifications" do %> - <%= render collection: @notifications, partial: "notifications/notification" %> +<% @notifications.each do |notification| %> + <%= turbo_stream.remove notification %> <% end %> diff --git a/test/controllers/cards/readings_controller_test.rb b/test/controllers/cards/readings_controller_test.rb index c169517e2..a880e46c7 100644 --- a/test/controllers/cards/readings_controller_test.rb +++ b/test/controllers/cards/readings_controller_test.rb @@ -5,11 +5,21 @@ class Cards::ReadingsControllerTest < ActionDispatch::IntegrationTest sign_in_as :kevin end - test "index" do + test "read one notification on card visit" do assert_changes -> { notifications(:logo_published_kevin).reload.read? }, from: false, to: true do post card_reading_path(cards(:logo)), as: :turbo_stream end assert_response :success end + + test "read multiple notifications on card visit" do + assert_changes -> { notifications(:logo_published_kevin).reload.read? }, from: false, to: true do + assert_changes -> { notifications(:logo_assignment_kevin).reload.read? }, from: false, to: true do + post card_reading_path(cards(:logo)), as: :turbo_stream + end + end + + assert_response :success + end end diff --git a/test/fixtures/notifications.yml b/test/fixtures/notifications.yml index d6db9d360..7579f8eb6 100644 --- a/test/fixtures/notifications.yml +++ b/test/fixtures/notifications.yml @@ -5,6 +5,13 @@ logo_published_kevin: resource: logo (Card) created_at: <%= 1.week.ago %> +logo_assignment_kevin: + user: kevin + event: logo_assignment_km + card: logo + resource: logo (Card) + created_at: <%= 1.week.ago %> + layout_commented_kevin: user: kevin event: layout_commented