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
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
Instance Method Summary collapse
-
#aggregate_per ⇒ Object
Aggregation.
- #aggregation_pending? ⇒ Boolean
-
#channels ⇒ Object
Channels.
- #count_for_target ⇒ Object
-
#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
142 143 144 145 146 |
# File 'app/models/notify_user/base_notification.rb', line 142 def self.channel(name, ={}) channels_clone = self.channels.clone channels_clone[name] = self.channels = channels_clone end |
.deliver_channels(notification_id) ⇒ Object
Deliver a single notification across each channel.
190 191 192 193 194 195 196 197 198 199 |
# File 'app/models/notify_user/base_notification.rb', line 190 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.
202 203 204 205 206 207 208 209 |
# File 'app/models/notify_user/base_notification.rb', line 202 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.
214 215 216 217 218 219 220 221 222 223 |
# File 'app/models/notify_user/base_notification.rb', line 214 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.
226 227 228 229 230 231 232 233 234 |
# File 'app/models/notify_user/base_notification.rb', line 226 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
155 156 157 158 |
# File 'app/models/notify_user/base_notification.rb', line 155 def self.for_target(target) where(target_id: target.id) .where(target_type: target.class.base_class) end |
.notify_aggregated(notification_id) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'app/models/notify_user/base_notification.rb', line 257 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
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'app/models/notify_user/base_notification.rb', line 237 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
160 161 162 163 164 |
# File 'app/models/notify_user/base_notification.rb', line 160 def self.pending_aggregation_with(notification) where(type: notification.type) .for_target(notification.target) .where(state: :pending) end |
Instance Method Details
#aggregate_per ⇒ Object
Aggregation
150 |
# File 'app/models/notify_user/base_notification.rb', line 150 class_attribute :aggregate_per |
#aggregation_pending? ⇒ Boolean
166 167 168 169 170 |
# File 'app/models/notify_user/base_notification.rb', line 166 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
127 |
# File 'app/models/notify_user/base_notification.rb', line 127 class_attribute :channels |
#count_for_target ⇒ Object
61 62 63 |
# File 'app/models/notify_user/base_notification.rb', line 61 def count_for_target NotifyUser::BaseNotification.for_target(target).where('state IN (?)', ["sent", "pending"]).count end |
#deliver! ⇒ Object
self.class.delay.deliver_channels(self.id)
end
end
181 182 183 184 185 186 187 |
# File 'app/models/notify_user/base_notification.rb', line 181 def deliver! unless user_has_unsubscribed? self.mark_as_sent self.save self.class.deliver_channels(self.id) end end |
#description ⇒ Object
Notification description
123 |
# File 'app/models/notify_user/base_notification.rb', line 123 class_attribute :description |
#generate_unsubscribe_hash ⇒ Object
117 118 119 120 |
# File 'app/models/notify_user/base_notification.rb', line 117 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
65 66 67 68 69 70 |
# File 'app/models/notify_user/base_notification.rb', line 65 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
72 73 74 75 76 77 |
# File 'app/models/notify_user/base_notification.rb', line 72 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/notify_user/base_notification.rb', line 98 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
90 91 92 93 94 95 |
# File 'app/models/notify_user/base_notification.rb', line 90 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
80 81 82 83 |
# File 'app/models/notify_user/base_notification.rb', line 80 def to(user) self.target = user self end |
#with(*args) ⇒ Object
85 86 87 88 |
# File 'app/models/notify_user/base_notification.rb', line 85 def with(*args) self.params = args.reduce({}, :update) self end |