Class: NotifyUser::BaseNotification
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- NotifyUser::BaseNotification
- Includes:
- AASM, ActionView::Helpers::TextHelper
- Defined in:
- app/models/notify_user/base_notification.rb
Constant Summary collapse
- @@channels =
{ action_mailer: {description: 'Email notifications'} }
- @@views =
{ mobile_sdk: { template_path: Proc.new {|n| "notify_user/#{n.class.name.underscore}/mobile_sdk/notification" } } }
Class Method Summary collapse
-
.channel(name, options = {}) ⇒ Object
Configure a channel.
-
.deliver_channels(notification_id) ⇒ Object
Deliver a single notification across each channel.
-
.deliver_channels_aggregated(notifications) ⇒ Object
Deliver multiple notifications across each channel as an aggregate message.
-
.deliver_notification_channel(notification_id, channel_name) ⇒ Object
Deliver a single notification to a specific channel.
-
.deliver_notifications_channel(notifications, channel_name) ⇒ Object
Deliver a aggregated notifications to a specific channel.
-
.for_target(target) ⇒ Object
Sending.
- .notify_aggregated(notification_id) ⇒ Object
-
.notify_aggregated_channel(notification_id, channel_name) ⇒ Object
notifies a single channel for aggregation.
- .pending_aggregation_with(notification) ⇒ Object
- .unsubscribed_from_channel?(user, type) ⇒ Boolean
Instance Method Summary collapse
-
#aggregate_per ⇒ Object
Aggregation.
- #aggregation_pending? ⇒ Boolean
-
#channels ⇒ Object
Channels.
-
#deliver! ⇒ Object
self.class.delay.deliver_channels(self.id) end end.
-
#description ⇒ Object
Notification description.
- #generate_unsubscribe_hash ⇒ Object
- #message ⇒ Object
- #mobile_message(length = 115) ⇒ Object
-
#notify ⇒ Object
Send any Emails/SMS/APNS.
- #notify! ⇒ Object
- #params ⇒ Object
-
#to(user) ⇒ Object
Public Interface.
- #with(*args) ⇒ Object
Class Method Details
.channel(name, options = {}) ⇒ Object
Configure a channel
139 140 141 |
# File 'app/models/notify_user/base_notification.rb', line 139 def self.channel(name, ={}) channels[name] = end |
.deliver_channels(notification_id) ⇒ Object
Deliver a single notification across each channel.
185 186 187 188 189 190 191 192 193 194 |
# File 'app/models/notify_user/base_notification.rb', line 185 def self.deliver_channels(notification_id) notification = self.where(id: notification_id).first return unless notification self.channels.each do |channel_name, | unless unsubscribed_from_channel?(notification.target, channel_name) channel = (channel_name.to_s + "_channel").camelize.constantize channel.deliver(notification, ) end end end |
.deliver_channels_aggregated(notifications) ⇒ Object
Deliver multiple notifications across each channel as an aggregate message.
197 198 199 200 201 202 203 204 |
# File 'app/models/notify_user/base_notification.rb', line 197 def self.deliver_channels_aggregated(notifications) self.channels.each do |channel_name, | if [:aggregate_per] != false && !unsubscribed_from_channel?(notifications.first.target, channel_name) channel = (channel_name.to_s + "_channel").camelize.constantize channel.deliver_aggregated(notifications, ) end end end |
.deliver_notification_channel(notification_id, channel_name) ⇒ Object
Deliver a single notification to a specific channel.
209 210 211 212 213 214 215 216 217 218 |
# File 'app/models/notify_user/base_notification.rb', line 209 def self.deliver_notification_channel(notification_id, channel_name) notification = self.where(id: notification_id).first return unless notification = channels[channel_name.to_sym] channel = (channel_name.to_s + "_channel").camelize.constantize unless self.unsubscribed_from_channel?(notification.target, channel_name) channel.deliver(notification, ) end end |
.deliver_notifications_channel(notifications, channel_name) ⇒ Object
Deliver a aggregated notifications to a specific channel.
221 222 223 224 225 226 227 228 229 |
# File 'app/models/notify_user/base_notification.rb', line 221 def self.deliver_notifications_channel(notifications, channel_name) = channels[channel_name.to_sym] channel = (channel_name.to_s + "_channel").camelize.constantize #check if user unsubsribed from channel type unless self.unsubscribed_from_channel?(notifications.first.target, channel_name) channel.deliver_aggregated(notifications, ) end end |
.for_target(target) ⇒ Object
Sending
150 151 152 153 |
# File 'app/models/notify_user/base_notification.rb', line 150 def self.for_target(target) where(target_id: target.id) .where(target_type: target.class.base_class) end |
.notify_aggregated(notification_id) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'app/models/notify_user/base_notification.rb', line 252 def self.notify_aggregated(notification_id) notification = self.find(notification_id) # Raise an exception if not found. # Find any pending notifications with the same type and target, which can all be sent in one message. notifications = self.pending_aggregation_with(notification) notifications.map(&:mark_as_sent) notifications.map(&:save) return if notifications.empty? if notifications.length == 1 # Despite waiting for more to aggregate, we only got one in the end. self.deliver_channels(notifications.first.id) else # We got several notifications while waiting, send them aggregated. self.deliver_channels_aggregated(notifications) end end |
.notify_aggregated_channel(notification_id, channel_name) ⇒ Object
notifies a single channel for aggregation
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'app/models/notify_user/base_notification.rb', line 232 def self.notify_aggregated_channel(notification_id, channel_name) notification = self.find(notification_id) # Raise an exception if not found. # Find any pending notifications with the same type and target, which can all be sent in one message. notifications = self.pending_aggregation_with(notification) notifications.map(&:mark_as_sent) notifications.map(&:save) return if notifications.empty? if notifications.length == 1 # Despite waiting for more to aggregate, we only got one in the end. self.deliver_notification_channel(notifications.first.id, channel_name) else # We got several notifications while waiting, send them aggregated. self.deliver_notifications_channel(notifications, channel_name) end end |
.pending_aggregation_with(notification) ⇒ Object
155 156 157 158 159 |
# File 'app/models/notify_user/base_notification.rb', line 155 def self.pending_aggregation_with(notification) where(type: notification.type) .for_target(notification.target) .where(state: :pending) end |
.unsubscribed_from_channel?(user, type) ⇒ Boolean
284 285 286 287 288 289 |
# File 'app/models/notify_user/base_notification.rb', line 284 def self.unsubscribed_from_channel?(user, type) #return true if user has unsubscribed return true unless NotifyUser::Unsubscribe.has_unsubscribed_from(user, type).empty? return false end |
Instance Method Details
#aggregate_per ⇒ Object
Aggregation
145 |
# File 'app/models/notify_user/base_notification.rb', line 145 class_attribute :aggregate_per |
#aggregation_pending? ⇒ Boolean
161 162 163 164 165 |
# File 'app/models/notify_user/base_notification.rb', line 161 def aggregation_pending? # A notification of the same type, that would have an aggregation job associated with it, # already exists. return (self.class.pending_aggregation_with(self).where('id != ?', id).count > 0) end |
#channels ⇒ Object
Channels
124 |
# File 'app/models/notify_user/base_notification.rb', line 124 mattr_accessor :channels |
#deliver! ⇒ Object
self.class.delay.deliver_channels(self.id)
end
end
176 177 178 179 180 181 182 |
# File 'app/models/notify_user/base_notification.rb', line 176 def deliver! unless user_has_unsubscribed? self.mark_as_sent self.save self.class.deliver_channels(self.id) end end |
#description ⇒ Object
Notification description
119 |
# File 'app/models/notify_user/base_notification.rb', line 119 class_attribute :description |
#generate_unsubscribe_hash ⇒ Object
113 114 115 116 |
# File 'app/models/notify_user/base_notification.rb', line 113 def generate_unsubscribe_hash #check if a hash already exists for that user otherwise create a new one return NotifyUser::UserHash.where(target_id: self.target.id).where(target_type: self.target.class.base_class).where(type: self.type).where(active: true).first || NotifyUser::UserHash.create(target: self.target, type: self.type, active: true) end |
#message ⇒ Object
61 62 63 64 65 66 |
# File 'app/models/notify_user/base_notification.rb', line 61 def ActionView::Base.new( Rails.configuration.paths["app/views"]).render( :template => self.class.views[:mobile_sdk][:template_path].call(self), :formats => [:html], :locals => { :params => self.params}, :layout => false) end |
#mobile_message(length = 115) ⇒ Object
68 69 70 71 72 73 |
# File 'app/models/notify_user/base_notification.rb', line 68 def (length=115) truncate(ActionView::Base.new( Rails.configuration.paths["app/views"]).render( :template => self.class.views[:mobile_sdk][:template_path].call(self), :formats => [:html], :locals => { :params => self.params}, :layout => false), :length => length) end |
#notify ⇒ Object
Send any Emails/SMS/APNS
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/models/notify_user/base_notification.rb', line 94 def notify if save #if aggregation is false bypass aggregation completely self.channels.each do |channel_name, | if([:aggregate_per] == false) self.class.delay.deliver_notification_channel(self.id, channel_name) else if not aggregation_pending? self.class.delay_for([:aggregate_per] || self.aggregate_per).notify_aggregated_channel(self.id, channel_name) end end end end end |
#notify! ⇒ Object
86 87 88 89 90 91 |
# File 'app/models/notify_user/base_notification.rb', line 86 def notify! save # Bang version of 'notify' ignores aggregation self.deliver! end |
#params ⇒ Object
53 54 55 56 57 58 59 |
# File 'app/models/notify_user/base_notification.rb', line 53 def params if super.nil? {} else super.with_indifferent_access end end |
#to(user) ⇒ Object
Public Interface
76 77 78 79 |
# File 'app/models/notify_user/base_notification.rb', line 76 def to(user) self.target = user self end |
#with(*args) ⇒ Object
81 82 83 84 |
# File 'app/models/notify_user/base_notification.rb', line 81 def with(*args) self.params = args.reduce({}, :update) self end |