Module: ActivityNotification::NotificationApi
- Extended by:
- ActiveSupport::Concern
- Included in:
- Notification
- Defined in:
- lib/activity_notification/apis/notification_api.rb
Overview
Defines API for notification included in Notification model.
Class Method Summary collapse
-
.available_options ⇒ Array<Notificaion>
Returns available options for kinds of notify methods.
-
.generate_notification(target, notifiable, options = {}) ⇒ Object
Generates a notification.
-
.group_member_exists?(notifications) ⇒ Boolean
Returns if group member of the notifications exists.
-
.notify(target_type, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to configured targets with notifiable model.
-
.notify_all(targets, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to specified targets.
-
.notify_to(target, notifiable, options = {}) ⇒ Notification
Generates notifications to one target.
-
.open_all_of(target, options = {}) ⇒ Integer
Opens all notifications of the target.
-
.send_batch_notification_email(target, notifications, options = {}) ⇒ Mail::Message, ActionMailer::DeliveryJob|NilClass
Sends batch notification email to the target.
Instance Method Summary collapse
-
#email_subscribed? ⇒ Boolean
Returns if the target subscribes this notification email.
-
#group_member? ⇒ Boolean
Returns if the notification is group member belonging to owner.
-
#group_member_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer
Returns count of group members of the notification.
-
#group_member_exists?(limit = ActivityNotification.config.opened_index_limit) ⇒ Boolean
Returns if group member of the notification exists.
-
#group_member_notifier_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer
Returns count of group member notifiers of the notification not including group owner notifier.
-
#group_member_notifier_exists?(limit = ActivityNotification.config.opened_index_limit) ⇒ Boolean
Returns if group member notifier except group owner notifier exists.
-
#group_notification_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer
Returns count of group notifications including owner and members.
-
#group_notifier_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer
Returns count of group member notifiers including group owner notifier.
-
#group_owner? ⇒ Boolean
Returns if the notification is group owner.
-
#latest_group_member ⇒ Notificaion
Returns the latest group member notification instance of this notification.
-
#notifiable_path ⇒ String
Returns notifiable_path to move after opening notification with notifiable.notifiable_path.
-
#open!(options = {}) ⇒ Integer
Opens the notification.
-
#opened? ⇒ Boolean
Returns if the notification is opened.
-
#optional_target_names ⇒ Array<Symbol>
Returns optional_target names of the notification from configured field or overriden method.
-
#optional_target_subscribed?(optional_target_name) ⇒ Boolean
Returns if the target subscribes this notification email.
-
#optional_targets ⇒ Array<ActivityNotification::OptionalTarget::Base>
Returns optional_targets of the notification from configured field or overriden method.
-
#publish_to_optional_targets(options = {}) ⇒ Hash
Publishes notification to the optional targets.
-
#remove_from_group ⇒ Notificaion
Remove from notification group and make a new group owner.
-
#send_notification_email(options = {}) ⇒ Mail::Message, ActionMailer::DeliveryJob
Sends notification email to the target.
-
#subscribed? ⇒ Boolean
Returns if the target subscribes this notification.
-
#unopened? ⇒ Boolean
Returns if the notification is unopened.
Class Method Details
.available_options ⇒ Array<Notificaion>
Returns available options for kinds of notify methods.
168 169 170 |
# File 'lib/activity_notification/apis/notification_api.rb', line 168 def [:key, :group, :parameters, :notifier, :send_email, :send_later].freeze end |
.generate_notification(target, notifiable, options = {}) ⇒ Object
Generates a notification
110 111 112 113 114 115 116 |
# File 'lib/activity_notification/apis/notification_api.rb', line 110 def generate_notification(target, notifiable, = {}) key = [:key] || notifiable.default_notification_key if target.subscribes_to_notification?(key) # Store notification store_notification(target, notifiable, key, ) end end |
.group_member_exists?(notifications) ⇒ Boolean
Returns if group member of the notifications exists. This method is designed to be called from controllers or views to avoid N+1.
140 141 142 |
# File 'lib/activity_notification/apis/notification_api.rb', line 140 def group_member_exists?(notifications) notifications.present? && where(group_owner_id: notifications.map(&:id)).exists? end |
.notify(target_type, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to configured targets with notifiable model.
37 38 39 40 41 42 |
# File 'lib/activity_notification/apis/notification_api.rb', line 37 def notify(target_type, notifiable, = {}) targets = notifiable.notification_targets(target_type, [:key]) unless targets.blank? notify_all(targets, notifiable, ) end end |
.notify_all(targets, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to specified targets.
62 63 64 |
# File 'lib/activity_notification/apis/notification_api.rb', line 62 def notify_all(targets, notifiable, = {}) targets.map { |target| target.notify_to(notifiable, ) } end |
.notify_to(target, notifiable, options = {}) ⇒ Notification
Generates notifications to one target.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/activity_notification/apis/notification_api.rb', line 84 def notify_to(target, notifiable, = {}) send_email = .has_key?(:send_email) ? [:send_email] : true send_later = .has_key?(:send_later) ? [:send_later] : true publish_optional_targets = .has_key?(:publish_optional_targets) ? [:publish_optional_targets] : true # Generate notification notification = generate_notification(target, notifiable, ) # Send notification email if notification.present? && send_email notification.send_notification_email({ send_later: send_later }) end # Publish to optional targets if notification.present? && publish_optional_targets notification.publish_to_optional_targets([:optional_targets] || {}) end # Return generated notification notification end |
.open_all_of(target, options = {}) ⇒ Integer
Add filter option
Opens all notifications of the target.
130 131 132 133 |
# File 'lib/activity_notification/apis/notification_api.rb', line 130 def open_all_of(target, = {}) opened_at = [:opened_at] || Time.current target.notifications.unopened_only.().update_all(opened_at: opened_at) end |
.send_batch_notification_email(target, notifications, options = {}) ⇒ Mail::Message, ActionMailer::DeliveryJob|NilClass
Sends batch notification email to the target.
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/activity_notification/apis/notification_api.rb', line 153 def send_batch_notification_email(target, notifications, = {}) notifications.blank? and return batch_key = [:batch_key] || notifications.first.key if target.batch_notification_email_allowed?(batch_key) && target.subscribes_to_notification_email?(batch_key) send_later = .has_key?(:send_later) ? [:send_later] : true send_later ? Mailer.send_batch_notification_email(target, notifications, batch_key, ).deliver_later : Mailer.send_batch_notification_email(target, notifications, batch_key, ).deliver_now end end |
Instance Method Details
#email_subscribed? ⇒ Boolean
Returns if the target subscribes this notification email.
369 370 371 |
# File 'lib/activity_notification/apis/notification_api.rb', line 369 def email_subscribed? target.subscribes_to_notification_email?(key) end |
#group_member? ⇒ Boolean
Returns if the notification is group member belonging to owner.
268 269 270 |
# File 'lib/activity_notification/apis/notification_api.rb', line 268 def group_member? group_owner_id.present? end |
#group_member_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer
Returns count of group members of the notification. This method is designed to cache group by query result to avoid N+1 call.
297 298 299 |
# File 'lib/activity_notification/apis/notification_api.rb', line 297 def group_member_count(limit = ActivityNotification.config.opened_index_limit) (:opened_group_member_count, :unopened_group_member_count, limit) end |
#group_member_exists?(limit = ActivityNotification.config.opened_index_limit) ⇒ Boolean
Returns if group member of the notification exists. This method is designed to cache group by query result to avoid N+1 call.
277 278 279 |
# File 'lib/activity_notification/apis/notification_api.rb', line 277 def group_member_exists?(limit = ActivityNotification.config.opened_index_limit) group_member_count(limit) > 0 end |
#group_member_notifier_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer
Returns count of group member notifiers of the notification not including group owner notifier. It always returns 0 if group owner notifier is blank. It counts only the member notifier of the same type with group owner notifier. This method is designed to cache group by query result to avoid N+1 call.
317 318 319 |
# File 'lib/activity_notification/apis/notification_api.rb', line 317 def group_member_notifier_count(limit = ActivityNotification.config.opened_index_limit) (:opened_group_member_notifier_count, :unopened_group_member_notifier_count, limit) end |
#group_member_notifier_exists?(limit = ActivityNotification.config.opened_index_limit) ⇒ Boolean
Returns if group member notifier except group owner notifier exists. It always returns false if group owner notifier is blank. It counts only the member notifier of the same type with group owner notifier. This method is designed to cache group by query result to avoid N+1 call.
288 289 290 |
# File 'lib/activity_notification/apis/notification_api.rb', line 288 def group_member_notifier_exists?(limit = ActivityNotification.config.opened_index_limit) group_member_notifier_count(limit) > 0 end |
#group_notification_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer
Returns count of group notifications including owner and members. This method is designed to cache group by query result to avoid N+1 call.
306 307 308 |
# File 'lib/activity_notification/apis/notification_api.rb', line 306 def group_notification_count(limit = ActivityNotification.config.opened_index_limit) group_member_count(limit) + 1 end |
#group_notifier_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer
Returns count of group member notifiers including group owner notifier. It always returns 0 if group owner notifier is blank. This method is designed to cache group by query result to avoid N+1 call.
327 328 329 330 |
# File 'lib/activity_notification/apis/notification_api.rb', line 327 def group_notifier_count(limit = ActivityNotification.config.opened_index_limit) notification = group_member? ? group_owner : self notification.notifier.present? ? group_member_notifier_count(limit) + 1 : 0 end |
#group_owner? ⇒ Boolean
Returns if the notification is group owner.
261 262 263 |
# File 'lib/activity_notification/apis/notification_api.rb', line 261 def group_owner? group_owner_id.blank? end |
#latest_group_member ⇒ Notificaion
Returns the latest group member notification instance of this notification. If this group owner has no group members, group owner instance self will be returned.
336 337 338 339 |
# File 'lib/activity_notification/apis/notification_api.rb', line 336 def latest_group_member notification = group_member? ? group_owner : self notification.group_member_exists? ? notification.group_members.latest : self end |
#notifiable_path ⇒ String
Returns notifiable_path to move after opening notification with notifiable.notifiable_path.
356 357 358 359 |
# File 'lib/activity_notification/apis/notification_api.rb', line 356 def notifiable_path notifiable.present? or raise ActiveRecord::RecordNotFound.new("Couldn't find notifiable #{notifiable_type}") notifiable.notifiable_path(target_type, key) end |
#open!(options = {}) ⇒ Integer
Opens the notification.
237 238 239 240 241 242 |
# File 'lib/activity_notification/apis/notification_api.rb', line 237 def open!( = {}) opened_at = [:opened_at] || Time.current with_members = .has_key?(:with_members) ? [:with_members] : true update(opened_at: opened_at) with_members ? group_members.update_all(opened_at: opened_at) + 1 : 1 end |
#opened? ⇒ Boolean
Returns if the notification is opened.
254 255 256 |
# File 'lib/activity_notification/apis/notification_api.rb', line 254 def opened? opened_at.present? end |
#optional_target_names ⇒ Array<Symbol>
Returns optional_target names of the notification from configured field or overriden method.
388 389 390 |
# File 'lib/activity_notification/apis/notification_api.rb', line 388 def optional_target_names notifiable.optional_target_names(target.to_resources_name, key) end |
#optional_target_subscribed?(optional_target_name) ⇒ Boolean
Returns if the target subscribes this notification email.
376 377 378 |
# File 'lib/activity_notification/apis/notification_api.rb', line 376 def optional_target_subscribed?(optional_target_name) target.subscribes_to_optional_target?(key, optional_target_name) end |
#optional_targets ⇒ Array<ActivityNotification::OptionalTarget::Base>
Returns optional_targets of the notification from configured field or overriden method.
382 383 384 |
# File 'lib/activity_notification/apis/notification_api.rb', line 382 def optional_targets notifiable.optional_targets(target.to_resources_name, key) end |
#publish_to_optional_targets(options = {}) ⇒ Hash
Publishes notification to the optional targets.
219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/activity_notification/apis/notification_api.rb', line 219 def publish_to_optional_targets( = {}) notifiable.optional_targets(target.to_resources_name, key).map { |optional_target| optional_target_name = optional_target.to_optional_target_name if optional_target_subscribed?(optional_target_name) optional_target.notify(self, [optional_target_name] || {}) [optional_target_name, true] else [optional_target_name, false] end }.to_h end |
#remove_from_group ⇒ Notificaion
Remove from notification group and make a new group owner.
344 345 346 347 348 349 350 351 |
# File 'lib/activity_notification/apis/notification_api.rb', line 344 def remove_from_group new_group_owner = group_members.earliest if new_group_owner.present? new_group_owner.update(group_owner_id: nil) group_members.update_all(group_owner_id: new_group_owner) end new_group_owner end |
#send_notification_email(options = {}) ⇒ Mail::Message, ActionMailer::DeliveryJob
Sends notification email to the target.
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/activity_notification/apis/notification_api.rb', line 204 def send_notification_email( = {}) if target.notification_email_allowed?(notifiable, key) && notifiable.notification_email_allowed?(target, key) && email_subscribed? send_later = .has_key?(:send_later) ? [:send_later] : true send_later ? Mailer.send_notification_email(self, ).deliver_later : Mailer.send_notification_email(self, ).deliver_now end end |
#subscribed? ⇒ Boolean
Returns if the target subscribes this notification.
363 364 365 |
# File 'lib/activity_notification/apis/notification_api.rb', line 363 def subscribed? target.subscribes_to_notification?(key) end |
#unopened? ⇒ Boolean
Returns if the notification is unopened.
247 248 249 |
# File 'lib/activity_notification/apis/notification_api.rb', line 247 def unopened? !opened? end |