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_channel(notification_id, channel_name) ⇒ Object
Prepares 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
returns the global unread notification count for a user.
-
#deliver ⇒ Object
Aggregates appropriately.
-
#deliver! ⇒ Object
Sends immediately and without aggregation.
-
#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
141 142 143 144 145 |
# File 'app/models/notify_user/base_notification.rb', line 141 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.
199 200 201 202 203 |
# File 'app/models/notify_user/base_notification.rb', line 199 def self.deliver_channels(notification_id) self.channels.each do |channel_name, | self.deliver_notification_channel(notification_id, channel_name) end end |
.deliver_channels_aggregated(notifications) ⇒ Object
Deliver multiple notifications across each channel as an aggregate message.
206 207 208 209 210 211 212 213 |
# File 'app/models/notify_user/base_notification.rb', line 206 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.
218 219 220 221 222 223 224 225 226 227 |
# File 'app/models/notify_user/base_notification.rb', line 218 def self.deliver_notification_channel(notification_id, channel_name) notification = self.find(notification_id) # Raise an exception if not found. = 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.
230 231 232 233 234 235 236 237 238 |
# File 'app/models/notify_user/base_notification.rb', line 230 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
154 155 156 157 |
# File 'app/models/notify_user/base_notification.rb', line 154 def self.for_target(target) where(target_id: target.id) .where(target_type: target.class.base_class) end |
.notify_aggregated_channel(notification_id, channel_name) ⇒ Object
Prepares a single channel for aggregation
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'app/models/notify_user/base_notification.rb', line 241 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
159 160 161 162 163 |
# File 'app/models/notify_user/base_notification.rb', line 159 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
149 |
# File 'app/models/notify_user/base_notification.rb', line 149 class_attribute :aggregate_per |
#aggregation_pending? ⇒ Boolean
165 166 167 168 169 |
# File 'app/models/notify_user/base_notification.rb', line 165 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
returns the global unread notification count for a user
73 74 75 |
# File 'app/models/notify_user/base_notification.rb', line 73 def count_for_target NotifyUser::BaseNotification.for_target(target).where('state IN (?)', ["sent", "pending"]).count end |
#deliver ⇒ Object
Aggregates appropriately
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/models/notify_user/base_notification.rb', line 172 def deliver if pending? and not user_has_unsubscribed? self.mark_as_sent! # 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 # only notifies channels if no pending aggreagte notifications 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 |
#deliver! ⇒ Object
Sends immediately and without aggregation
191 192 193 194 195 196 |
# File 'app/models/notify_user/base_notification.rb', line 191 def deliver! if pending_no_aggregation? and not user_has_unsubscribed? self.mark_as_sent! 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
77 78 79 80 81 82 83 84 |
# File 'app/models/notify_user/base_notification.rb', line 77 def string = 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}) return ::CGI.unescapeHTML("#{string}") end |
#mobile_message(length = 115) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'app/models/notify_user/base_notification.rb', line 86 def (length=115) string = 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}), :length => length) return ::CGI.unescapeHTML("#{string}") end |
#notify ⇒ Object
Send any Emails/SMS/APNS
112 113 114 115 |
# File 'app/models/notify_user/base_notification.rb', line 112 def notify # Sends with aggregation if enabled save end |
#notify! ⇒ Object
106 107 108 109 |
# File 'app/models/notify_user/base_notification.rb', line 106 def notify! # Bang version of 'notify' ignores aggregation dont_aggregate! end |
#params ⇒ Object
64 65 66 67 68 69 70 |
# File 'app/models/notify_user/base_notification.rb', line 64 def params if super.nil? {} else super.with_indifferent_access end end |
#to(user) ⇒ Object
Public Interface
96 97 98 99 |
# File 'app/models/notify_user/base_notification.rb', line 96 def to(user) self.target = user self end |
#with(*args) ⇒ Object
101 102 103 104 |
# File 'app/models/notify_user/base_notification.rb', line 101 def with(*args) self.params = args.reduce({}, :update) self end |