From f73fc87991924a8ddd83675f63030ea7d6439fa9 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 5 Mar 2023 13:47:46 +0100 Subject: [PATCH] Simplify `notify` and `denotify` methods --- app/models/subscription.rb | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 1eb4fd9e..0424c3da 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -36,23 +36,20 @@ class Subscription < ApplicationRecord end def notify(source, target) - if source.nil? or target.nil? - return nil + return nil if source.nil? || target.nil? + + notifications = Subscription.where(answer: target).where.not(user: target.user).map do |s| + { target_id: source.id, target_type: Comment, recipient_id: s.user_id, new: true, type: Notification::Commented } end - Subscription.where(answer: target).each do |subs| - next unless not subs.user == source.user - Notification.notify subs.user, source - end + Notification.insert_all!(notifications) end def denotify(source, target) - if source.nil? or target.nil? - return nil - end - Subscription.where(answer: target).each do |subs| - Notification.denotify subs.user, source - end + return nil if source.nil? or target.nil? + + subs = Subscription.where(answer: target) + Notification.where(target:, recipient: subs.map(&:user)).delete_all end end end