Module: ActivityNotification::NotificationApi
- Extended by:
- ActiveSupport::Concern
- Included in:
- ORM::ActiveRecord::Notification, ORM::Mongoid::Notification
- Defined in:
- lib/activity_notification/apis/notification_api.rb
Overview
Defines API for notification included in Notification model.
Class Method Summary collapse
-
.all_index! ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects all notification index.
-
.available_options ⇒ Array<Notificaion>
Returns available options for kinds of notify methods.
-
.filtered_by_key ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects filtered notifications by key.
-
.filtered_by_options ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects filtered notifications by notifiable_type, group or key with filter options.
-
.filtered_by_target_type ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects filtered notifications by target_type.
-
.filtered_by_type ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects filtered notifications by notifiable_type.
-
.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.
-
.opened_index ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects unopened notification index.
-
.send_batch_notification_email(target, notifications, options = {}) ⇒ Mail::Message, ActionMailer::DeliveryJob|NilClass
Sends batch notification email to the target.
-
.unopened_index ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects unopened notification index.
Instance Method Summary collapse
-
#earliest_order ⇒ Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as created_at: :asc.
-
#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.
-
#latest_order ⇒ Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as created_at: :desc.
-
#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
.all_index! ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects all notification index. ActivityNotification::Notification.all_index! is defined same as ActivityNotification::Notification.group_owners_only.latest_order
21 22 23 24 |
# File 'lib/activity_notification/apis/notification_api.rb', line 21 scope :all_index!, ->(reverse = false, with_group_members = false) { target_index = with_group_members ? self : group_owners_only reverse ? target_index.earliest_order : target_index.latest_order } |
.available_options ⇒ Array<Notificaion>
Returns available options for kinds of notify methods.
296 297 298 |
# File 'lib/activity_notification/apis/notification_api.rb', line 296 def [:key, :group, :parameters, :notifier, :send_email, :send_later].freeze end |
.filtered_by_key ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects filtered notifications by key.
81 |
# File 'lib/activity_notification/apis/notification_api.rb', line 81 scope :filtered_by_key, ->(key) { where(key: key) } |
.filtered_by_options ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects filtered notifications by notifiable_type, group or key with filter options.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/activity_notification/apis/notification_api.rb', line 105 scope :filtered_by_options, ->( = {}) { = ActivityNotification.cast_to_indifferent_hash() filtered_notifications = all if .has_key?(:filtered_by_type) filtered_notifications = filtered_notifications.filtered_by_type([:filtered_by_type]) end if .has_key?(:filtered_by_group) filtered_notifications = filtered_notifications.filtered_by_group([:filtered_by_group]) end if .has_key?(:filtered_by_group_type) && .has_key?(:filtered_by_group_id) filtered_notifications = filtered_notifications .where(group_type: [:filtered_by_group_type], group_id: [:filtered_by_group_id]) end if .has_key?(:filtered_by_key) filtered_notifications = filtered_notifications.filtered_by_key([:filtered_by_key]) end if .has_key?(:custom_filter) filtered_notifications = filtered_notifications.where([:custom_filter]) end filtered_notifications } |
.filtered_by_target_type ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects filtered notifications by target_type.
65 |
# File 'lib/activity_notification/apis/notification_api.rb', line 65 scope :filtered_by_target_type, ->(target_type) { where(target_type: target_type) } |
.filtered_by_type ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects filtered notifications by notifiable_type.
73 |
# File 'lib/activity_notification/apis/notification_api.rb', line 73 scope :filtered_by_type, ->(notifiable_type) { where(notifiable_type: notifiable_type) } |
.generate_notification(target, notifiable, options = {}) ⇒ Object
Generates a notification
235 236 237 238 239 240 241 |
# File 'lib/activity_notification/apis/notification_api.rb', line 235 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.
268 269 270 |
# File 'lib/activity_notification/apis/notification_api.rb', line 268 def group_member_exists?(notifications) notifications.present? && group_members_of_owner_ids_only(notifications.map(&:id)).exists? end |
.notify(target_type, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to configured targets with notifiable model.
162 163 164 165 166 167 |
# File 'lib/activity_notification/apis/notification_api.rb', line 162 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.
187 188 189 |
# File 'lib/activity_notification/apis/notification_api.rb', line 187 def notify_all(targets, notifiable, = {}) targets.map { |target| target.notify_to(notifiable, ) } end |
.notify_to(target, notifiable, options = {}) ⇒ Notification
Generates notifications to one target.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/activity_notification/apis/notification_api.rb', line 209 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.
255 256 257 258 259 260 261 |
# File 'lib/activity_notification/apis/notification_api.rb', line 255 def open_all_of(target, = {}) opened_at = [:opened_at] || Time.current target_unopened_notifications = target.notifications.unopened_only.() unopened_notification_count = target_unopened_notifications.count target_unopened_notifications.update_all(opened_at: opened_at) unopened_notification_count end |
.opened_index ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects unopened notification index. ActivityNotification::Notification.opened_index(limit) is defined same as ActivityNotification::Notification.opened_only(limit).group_owners_only.latest_order
54 55 56 57 |
# File 'lib/activity_notification/apis/notification_api.rb', line 54 scope :opened_index, ->(limit, reverse = false, with_group_members = false) { target_index = with_group_members ? opened_only(limit) : opened_only(limit).group_owners_only reverse ? target_index.earliest_order : target_index.latest_order } |
.send_batch_notification_email(target, notifications, options = {}) ⇒ Mail::Message, ActionMailer::DeliveryJob|NilClass
Sends batch notification email to the target.
281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/activity_notification/apis/notification_api.rb', line 281 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 |
.unopened_index ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects unopened notification index. ActivityNotification::Notification.unopened_index is defined same as ActivityNotification::Notification.unopened_only.group_owners_only.latest_order
37 38 39 40 |
# File 'lib/activity_notification/apis/notification_api.rb', line 37 scope :unopened_index, ->(reverse = false, with_group_members = false) { target_index = with_group_members ? unopened_only : unopened_only.group_owners_only reverse ? target_index.earliest_order : target_index.latest_order } |
Instance Method Details
#earliest_order ⇒ Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as created_at: :asc.
133 |
# File 'lib/activity_notification/apis/notification_api.rb', line 133 scope :earliest_order, -> { order(created_at: :asc) } |
#email_subscribed? ⇒ Boolean
Returns if the target subscribes this notification email.
499 500 501 |
# File 'lib/activity_notification/apis/notification_api.rb', line 499 def email_subscribed? target.subscribes_to_notification_email?(key) end |
#group_member? ⇒ Boolean
Returns if the notification is group member belonging to owner.
398 399 400 |
# File 'lib/activity_notification/apis/notification_api.rb', line 398 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.
427 428 429 |
# File 'lib/activity_notification/apis/notification_api.rb', line 427 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.
407 408 409 |
# File 'lib/activity_notification/apis/notification_api.rb', line 407 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.
447 448 449 |
# File 'lib/activity_notification/apis/notification_api.rb', line 447 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.
418 419 420 |
# File 'lib/activity_notification/apis/notification_api.rb', line 418 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.
436 437 438 |
# File 'lib/activity_notification/apis/notification_api.rb', line 436 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.
457 458 459 460 |
# File 'lib/activity_notification/apis/notification_api.rb', line 457 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.
391 392 393 |
# File 'lib/activity_notification/apis/notification_api.rb', line 391 def group_owner? !group_member? 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.
466 467 468 469 |
# File 'lib/activity_notification/apis/notification_api.rb', line 466 def latest_group_member notification = group_member? ? group_owner : self notification.group_member_exists? ? notification.group_members.latest : self end |
#latest_order ⇒ Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as created_at: :desc.
129 |
# File 'lib/activity_notification/apis/notification_api.rb', line 129 scope :latest_order, -> { order(created_at: :desc) } |
#notifiable_path ⇒ String
Returns notifiable_path to move after opening notification with notifiable.notifiable_path.
486 487 488 489 |
# File 'lib/activity_notification/apis/notification_api.rb', line 486 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.
364 365 366 367 368 369 370 371 372 |
# File 'lib/activity_notification/apis/notification_api.rb', line 364 def open!( = {}) opened? and return 0 opened_at = [:opened_at] || Time.current with_members = .has_key?(:with_members) ? [:with_members] : true unopened_member_count = with_members ? group_members.unopened_only.count : 0 group_members.update_all(opened_at: opened_at) if with_members update(opened_at: opened_at) unopened_member_count + 1 end |
#opened? ⇒ Boolean
Returns if the notification is opened.
384 385 386 |
# File 'lib/activity_notification/apis/notification_api.rb', line 384 def opened? opened_at.present? end |
#optional_target_names ⇒ Array<Symbol>
Returns optional_target names of the notification from configured field or overriden method.
518 519 520 |
# File 'lib/activity_notification/apis/notification_api.rb', line 518 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.
506 507 508 |
# File 'lib/activity_notification/apis/notification_api.rb', line 506 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.
512 513 514 |
# File 'lib/activity_notification/apis/notification_api.rb', line 512 def optional_targets notifiable.optional_targets(target.to_resources_name, key) end |
#publish_to_optional_targets(options = {}) ⇒ Hash
Publishes notification to the optional targets.
346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/activity_notification/apis/notification_api.rb', line 346 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.
474 475 476 477 478 479 480 481 |
# File 'lib/activity_notification/apis/notification_api.rb', line 474 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.id) end new_group_owner end |
#send_notification_email(options = {}) ⇒ Mail::Message, ActionMailer::DeliveryJob
Sends notification email to the target.
331 332 333 334 335 336 337 338 339 340 |
# File 'lib/activity_notification/apis/notification_api.rb', line 331 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.
493 494 495 |
# File 'lib/activity_notification/apis/notification_api.rb', line 493 def subscribed? target.subscribes_to_notification?(key) end |
#unopened? ⇒ Boolean
Returns if the notification is unopened.
377 378 379 |
# File 'lib/activity_notification/apis/notification_api.rb', line 377 def unopened? !opened? end |