From 056d853cc87c686df0cb4e0f0bb125aa13a1c796 Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Wed, 15 Jan 2025 11:42:35 +0000 Subject: [PATCH] Don't notify while bubbles are still drafted --- app/helpers/notifications_helper.rb | 11 ++++++++++- app/models/notifier.rb | 10 ++++++++-- test/models/notifier_test.rb | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 62474229b..96e17ff43 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -14,8 +14,8 @@ module NotificationsHelper case notification.event.action when "assigned" then "#{name} assigned to you" - when "published" then "Added by #{name}" when "popped" then "Popped by by #{name}" + when "published" then notification_bubble_created_message(notification) else name end end @@ -24,4 +24,13 @@ module NotificationsHelper link_to notification.resource, id: dom_id(notification), class: "notification border-radius", data: { turbo_frame: "_top" }, & end + + private + def notification_bubble_created_message(notification) + if notification.bubble.assigned_to?(notification.user) + "#{notification.creator.name} assigned to you" + else + "Added by #{notification.creator.name}" + end + end end diff --git a/app/models/notifier.rb b/app/models/notifier.rb index cb64ec77c..86c07b54d 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -10,8 +10,10 @@ class Notifier end def generate - recipients.map do |recipient| - Notification.create! user: recipient, event: event, bubble: bubble, resource: resource + if should_notify? + recipients.map do |recipient| + Notification.create! user: recipient, event: event, bubble: bubble, resource: resource + end end end @@ -20,6 +22,10 @@ class Notifier @event = event end + def should_notify? + bubble.published? + end + def recipients bubble.bucket.users.without(creator) end diff --git a/test/models/notifier_test.rb b/test/models/notifier_test.rb index cb85bf551..36cbfca5d 100644 --- a/test/models/notifier_test.rb +++ b/test/models/notifier_test.rb @@ -12,4 +12,18 @@ class NotifierTest < ActiveSupport::TestCase end end end + + test "generate creates notifications for the event recipients" do + assert_difference -> { Notification.count }, +2 do + Notifier.for(events(:logo_published)).generate + end + end + + test "generate does not create notifications if the bubble is not published" do + bubbles(:logo).drafted! + + assert_no_difference -> { Notification.count } do + Notifier.for(events(:logo_published)).generate + end + end end