From 1b97c396454d9c3e45c37173d29b702c7d6de13d Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 26 Feb 2019 10:45:12 +0100 Subject: [PATCH] refactored notificationcenter to accept meta_data --- app/models/subscription.rb | 16 +++++++++------- app/models/user.rb | 8 ++++---- app/services/notification_center.rb | 20 ++++++++++++-------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 2787bea86..c9f2de0c4 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -257,14 +257,16 @@ class Subscription < ActiveRecord::Base def notify_subscription_extended(free_days) meta_data = {} - meta_data[:free_days] = true if free_days == true - notification = Notification.new(meta_data: meta_data) - notification.send_notification(type: :notify_member_subscription_extended, attached_object: self).to(user).deliver_later + meta_data[:free_days] = true if free_days + NotificationCenter.call type: :notify_member_subscription_extended, + receiver: user, + attached_object: self, + meta_data: meta_data - User.admins.each do |admin| - notification = Notification.new(meta_data: meta_data) - notification.send_notification(type: :notify_admin_subscription_extended, attached_object: self).to(admin).deliver_later - end + NotificationCenter.call type: :notify_admin_subscription_extended, + receiver: User.admins, + attached_object: self, + meta_data: meta_data end def set_expiration_date diff --git a/app/models/user.rb b/app/models/user.rb index 33266ad73..ca5dd6963 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -336,10 +336,10 @@ class User < ActiveRecord::Base ex_group = Group.find(changes[:group_id].first) meta_data = { ex_group_name: ex_group.name } - User.admins.each do |admin| - notification = Notification.new(meta_data: meta_data) - notification.send_notification(type: :notify_admin_user_group_changed, attached_object: self).to(admin).deliver_later - end + NotificationCenter.call type: :notify_admin_user_group_changed, + receiver: User.admins, + attached_object: self, + meta_data: meta_data NotificationCenter.call type: :notify_user_user_group_changed, receiver: self, diff --git a/app/services/notification_center.rb b/app/services/notification_center.rb index 0d2699290..32a6063c9 100644 --- a/app/services/notification_center.rb +++ b/app/services/notification_center.rb @@ -1,16 +1,20 @@ +# frozen_string_literal: true + +# send notification to one or several receiver with a type, an attached object and an optional meta data class NotificationCenter - # send notification to one or several receiver with a type and attached object - def self.call(type: nil, receiver: nil, attached_object: nil) + def self.call(type: nil, receiver: nil, attached_object: nil, meta_data: {}) if receiver.respond_to?(:each) receiver.each do |user| - Notification.new.send_notification(type: type, attached_object: attached_object) - .to(user) - .deliver_later + Notification.new(meta_data: meta_data) + .send_notification(type: type, attached_object: attached_object) + .to(user) + .deliver_later end else - Notification.new.send_notification(type: type, attached_object: attached_object) - .to(receiver) - .deliver_later + Notification.new(meta_data: meta_data) + .send_notification(type: type, attached_object: attached_object) + .to(receiver) + .deliver_later end end end