Module: ActivityNotification::NotificationApi
- Extended by:
- ActiveSupport::Concern
- Included in:
- ORM::ActiveRecord::Notification, ORM::Dynamoid::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.
-
.destroy_all_of(target, options = {}) ⇒ Array<Notification>
Destroys all notifications of the target matching the filter criteria.
-
.earliest ⇒ Notification
Returns earliest notification instance.
-
.earliest! ⇒ Notification
Returns earliest notification instance.
-
.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.
-
.latest ⇒ Notification
Returns latest notification instance.
-
.latest! ⇒ Notification
Returns latest notification instance.
-
.notify(target_type, notifiable, options = {}) ⇒ Array<Notificaion>
(also: notify_now)
Generates notifications to configured targets with notifiable model.
-
.notify_all(targets, notifiable, options = {}) ⇒ Array<Notificaion>
(also: notify_all_now)
Generates notifications to specified targets.
-
.notify_all_later(targets, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to specified targets later by ActiveJob queue.
-
.notify_later(target_type, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to configured targets with notifiable model later by ActiveJob queue.
-
.notify_later_to(target, notifiable, options = {}) ⇒ Notification
Generates notifications to one target later by ActiveJob queue.
-
.notify_to(target, notifiable, options = {}) ⇒ Notification
(also: notify_now_to)
Generates notifications to one target.
-
.open_all_of(target, options = {}) ⇒ Array<Notification>
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.
-
.set_notification_mailer ⇒ Object
Defines mailer class to send notification.
-
.uniq_keys ⇒ Array<String>
Selects unique keys from query for notifications.
-
.unopened_index ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects unopened notification index.
-
.valid_group_owner(target, notifiable, key, group, group_expiry_delay) ⇒ Notificaion
Returns valid group owner within the expiration period.
Instance Method Summary collapse
-
#after_store ⇒ Object
Call after store action with stored notification.
-
#earliest_order ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as created_at: :asc.
-
#earliest_order! ⇒ ActiveRecord_AssociationRelation<Notificaion>, 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 ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as created_at: :desc.
-
#latest_order! ⇒ ActiveRecord_AssociationRelation<Notificaion>, 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 overridden 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 overridden method.
-
#prepare_to_store ⇒ Object
:nocov: Returns prepared notification object to store.
-
#printable_notifiable_name ⇒ String
Returns printable notifiable model name to show in view or email.
-
#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, ...
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
26 27 28 29 |
# File 'lib/activity_notification/apis/notification_api.rb', line 26 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.
504 505 506 |
# File 'lib/activity_notification/apis/notification_api.rb', line 504 def [:key, :group, :group_expiry_delay, :notifier, :parameters, :send_email, :send_later, :pass_full_options].freeze end |
.destroy_all_of(target, options = {}) ⇒ Array<Notification>
Destroys all notifications of the target matching the filter criteria.
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/activity_notification/apis/notification_api.rb', line 450 def destroy_all_of(target, = {}) target_notifications = target.notifications.() # If specific IDs are provided, filter by them if [:ids].present? # :nocov: case ActivityNotification.config.orm when :mongoid target_notifications = target_notifications.where(id: { '$in' => [:ids] }) when :dynamoid target_notifications = target_notifications.where('id.in': [:ids]) else # :active_record target_notifications = target_notifications.where(id: [:ids]) end # :nocov: end # Get the notifications before destroying them for return value destroyed_notifications = target_notifications.to_a target_notifications.destroy_all destroyed_notifications end |
.earliest ⇒ Notification
Returns earliest notification instance.
167 168 169 |
# File 'lib/activity_notification/apis/notification_api.rb', line 167 def self.earliest earliest_order.first end |
.earliest! ⇒ Notification
Returns earliest notification instance. This method is to be overridden in implementation for each ORM.
181 182 183 |
# File 'lib/activity_notification/apis/notification_api.rb', line 181 def self.earliest! earliest end |
.filtered_by_key ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Selects filtered notifications by key.
86 |
# File 'lib/activity_notification/apis/notification_api.rb', line 86 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.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/activity_notification/apis/notification_api.rb', line 112 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?(:later_than) filtered_notifications = filtered_notifications.later_than(Time.iso8601([:later_than])) end if .has_key?(:earlier_than) filtered_notifications = filtered_notifications.earlier_than(Time.iso8601([:earlier_than])) 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.
70 |
# File 'lib/activity_notification/apis/notification_api.rb', line 70 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.
78 |
# File 'lib/activity_notification/apis/notification_api.rb', line 78 scope :filtered_by_type, ->(notifiable_type) { where(notifiable_type: notifiable_type) } |
.generate_notification(target, notifiable, options = {}) ⇒ Object
Generates a notification
394 395 396 397 398 399 400 |
# File 'lib/activity_notification/apis/notification_api.rb', line 394 def generate_notification(target, notifiable, = {}) key = [:key] || notifiable.default_notification_key if target.subscribes_to_notification?(key) # Store notification 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.
476 477 478 |
# File 'lib/activity_notification/apis/notification_api.rb', line 476 def group_member_exists?(notifications) notifications.present? and group_members_of_owner_ids_only(notifications.map(&:id)).exists? end |
.latest ⇒ Notification
Returns latest notification instance.
161 162 163 |
# File 'lib/activity_notification/apis/notification_api.rb', line 161 def self.latest latest_order.first end |
.latest! ⇒ Notification
Returns latest notification instance. This method is to be overridden in implementation for each ORM.
174 175 176 |
# File 'lib/activity_notification/apis/notification_api.rb', line 174 def self.latest! latest end |
.notify(target_type, notifiable, options = {}) ⇒ Array<Notificaion> Also known as: notify_now
Generates notifications to configured targets with notifiable model.
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/activity_notification/apis/notification_api.rb', line 226 def notify(target_type, notifiable, = {}) if [:notify_later] notify_later(target_type, notifiable, ) else targets = notifiable.notification_targets(target_type, [:pass_full_options] ? : [:key]) unless targets.blank? notify_all(targets, notifiable, ) end end end |
.notify_all(targets, notifiable, options = {}) ⇒ Array<Notificaion> Also known as: notify_all_now
Generates notifications to specified targets.
289 290 291 292 293 294 295 |
# File 'lib/activity_notification/apis/notification_api.rb', line 289 def notify_all(targets, notifiable, = {}) if [:notify_later] notify_all_later(targets, notifiable, ) else targets.map { |target| notify_to(target, notifiable, ) } end end |
.notify_all_later(targets, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to specified targets later by ActiveJob queue.
316 317 318 319 |
# File 'lib/activity_notification/apis/notification_api.rb', line 316 def notify_all_later(targets, notifiable, = {}) .delete(:notify_later) ActivityNotification::NotifyAllJob.perform_later(targets, notifiable, ) end |
.notify_later(target_type, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to configured targets with notifiable model later by ActiveJob queue.
264 265 266 267 268 |
# File 'lib/activity_notification/apis/notification_api.rb', line 264 def notify_later(target_type, notifiable, = {}) target_type = target_type.to_s if target_type.is_a? Symbol .delete(:notify_later) ActivityNotification::NotifyJob.perform_later(target_type, notifiable, ) end |
.notify_later_to(target, notifiable, options = {}) ⇒ Notification
Generates notifications to one target later by ActiveJob queue.
381 382 383 384 |
# File 'lib/activity_notification/apis/notification_api.rb', line 381 def notify_later_to(target, notifiable, = {}) .delete(:notify_later) ActivityNotification::NotifyToJob.perform_later(target, notifiable, ) end |
.notify_to(target, notifiable, options = {}) ⇒ Notification Also known as: notify_now_to
Generates notifications to one target.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/activity_notification/apis/notification_api.rb', line 340 def notify_to(target, notifiable, = {}) if [:notify_later] notify_later_to(target, notifiable, ) else 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 end |
.open_all_of(target, options = {}) ⇒ Array<Notification>
Opens all notifications of the target.
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/activity_notification/apis/notification_api.rb', line 416 def open_all_of(target, = {}) opened_at = [:opened_at] || Time.current target_unopened_notifications = target.notifications.unopened_only.() # If specific IDs are provided, filter by them if [:ids].present? # :nocov: case ActivityNotification.config.orm when :mongoid target_unopened_notifications = target_unopened_notifications.where(id: { '$in' => [:ids] }) when :dynamoid target_unopened_notifications = target_unopened_notifications.where('id.in': [:ids]) else # :active_record target_unopened_notifications = target_unopened_notifications.where(id: [:ids]) end # :nocov: end opened_notifications = target_unopened_notifications.to_a.map { |n| n.opened_at = opened_at; n } target_unopened_notifications.update_all(opened_at: opened_at) opened_notifications 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
59 60 61 62 |
# File 'lib/activity_notification/apis/notification_api.rb', line 59 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.
489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/activity_notification/apis/notification_api.rb', line 489 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 ? @@notification_mailer.send_batch_notification_email(target, notifications, batch_key, ).deliver_later : @@notification_mailer.send_batch_notification_email(target, notifications, batch_key, ).deliver_now end end |
.set_notification_mailer ⇒ Object
Defines mailer class to send notification
509 510 511 |
# File 'lib/activity_notification/apis/notification_api.rb', line 509 def set_notification_mailer @@notification_mailer = ActivityNotification.config.mailer.constantize end |
.uniq_keys ⇒ Array<String>
Selects unique keys from query for notifications.
187 188 189 190 191 192 193 |
# File 'lib/activity_notification/apis/notification_api.rb', line 187 def self.uniq_keys ## select method cannot be chained with order by other columns like created_at # select(:key).distinct.pluck(:key) ## distinct method cannot keep original sort # distinct(:key) pluck(:key).uniq 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
42 43 44 45 |
# File 'lib/activity_notification/apis/notification_api.rb', line 42 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 } |
.valid_group_owner(target, notifiable, key, group, group_expiry_delay) ⇒ Notificaion
Returns valid group owner within the expiration period
521 522 523 524 525 526 527 528 529 530 |
# File 'lib/activity_notification/apis/notification_api.rb', line 521 def valid_group_owner(target, notifiable, key, group, group_expiry_delay) return nil if group.blank? # Bundle notification group by target, notifiable_type, group and key # Different notifiable.id can be made in a same group group_owner_notifications = filtered_by_target(target).filtered_by_type(notifiable.to_class_name).filtered_by_key(key) .filtered_by_group(group).group_owners_only.unopened_only group_expiry_delay.present? ? group_owner_notifications.within_expiration_only(group_expiry_delay).earliest : group_owner_notifications.earliest end |
Instance Method Details
#after_store ⇒ Object
Call after store action with stored notification
559 560 |
# File 'lib/activity_notification/apis/notification_api.rb', line 559 def after_store end |
#earliest_order ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as created_at: :asc.
146 |
# File 'lib/activity_notification/apis/notification_api.rb', line 146 scope :earliest_order, -> { order(created_at: :asc) } |
#earliest_order! ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as created_at: :asc. This method is to be overridden in implementation for each ORM.
157 |
# File 'lib/activity_notification/apis/notification_api.rb', line 157 scope :earliest_order!, -> { earliest_order } |
#email_subscribed? ⇒ Boolean
Returns if the target subscribes this notification email.
753 754 755 |
# File 'lib/activity_notification/apis/notification_api.rb', line 753 def email_subscribed? target.subscribes_to_notification_email?(key) end |
#group_member? ⇒ Boolean
Returns if the notification is group member belonging to owner.
646 647 648 |
# File 'lib/activity_notification/apis/notification_api.rb', line 646 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.
675 676 677 |
# File 'lib/activity_notification/apis/notification_api.rb', line 675 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.
655 656 657 |
# File 'lib/activity_notification/apis/notification_api.rb', line 655 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.
695 696 697 |
# File 'lib/activity_notification/apis/notification_api.rb', line 695 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.
666 667 668 |
# File 'lib/activity_notification/apis/notification_api.rb', line 666 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.
684 685 686 |
# File 'lib/activity_notification/apis/notification_api.rb', line 684 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.
705 706 707 708 |
# File 'lib/activity_notification/apis/notification_api.rb', line 705 def group_notifier_count(limit = ActivityNotification.config.opened_index_limit) notification = group_member? && group_owner.present? ? group_owner : self notification.notifier.present? ? group_member_notifier_count(limit) + 1 : 0 end |
#group_owner? ⇒ Boolean
Returns if the notification is group owner.
639 640 641 |
# File 'lib/activity_notification/apis/notification_api.rb', line 639 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.
714 715 716 717 |
# File 'lib/activity_notification/apis/notification_api.rb', line 714 def latest_group_member notification = group_member? && group_owner.present? ? group_owner : self notification.group_member_exists? ? notification.group_members.latest : self end |
#latest_order ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as created_at: :desc.
142 |
# File 'lib/activity_notification/apis/notification_api.rb', line 142 scope :latest_order, -> { order(created_at: :desc) } |
#latest_order! ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as created_at: :desc. This method is to be overridden in implementation for each ORM.
152 |
# File 'lib/activity_notification/apis/notification_api.rb', line 152 scope :latest_order!, ->(reverse = false) { reverse ? earliest_order : latest_order } |
#notifiable_path ⇒ String
Returns notifiable_path to move after opening notification with notifiable.notifiable_path.
734 735 736 737 |
# File 'lib/activity_notification/apis/notification_api.rb', line 734 def notifiable_path notifiable.blank? and raise ActivityNotification::NotifiableNotFoundError.new("Couldn't find associated notifiable (#{notifiable_type}) of #{self.class.name} with 'id'=#{id}") notifiable.notifiable_path(target_type, key) end |
#open!(options = {}) ⇒ Integer
Opens the notification.
612 613 614 615 616 617 618 619 620 |
# File 'lib/activity_notification/apis/notification_api.rb', line 612 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 [:skip_validation] ? update_attribute(:opened_at, opened_at) : update(opened_at: opened_at) unopened_member_count + 1 end |
#opened? ⇒ Boolean
Returns if the notification is opened.
632 633 634 |
# File 'lib/activity_notification/apis/notification_api.rb', line 632 def opened? opened_at.present? end |
#optional_target_names ⇒ Array<Symbol>
Returns optional_target names of the notification from configured field or overridden method.
772 773 774 |
# File 'lib/activity_notification/apis/notification_api.rb', line 772 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.
760 761 762 |
# File 'lib/activity_notification/apis/notification_api.rb', line 760 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 overridden method.
766 767 768 |
# File 'lib/activity_notification/apis/notification_api.rb', line 766 def optional_targets notifiable.optional_targets(target.to_resources_name, key) end |
#prepare_to_store ⇒ Object
:nocov: Returns prepared notification object to store
554 555 556 |
# File 'lib/activity_notification/apis/notification_api.rb', line 554 def prepare_to_store self end |
#printable_notifiable_name ⇒ String
Returns printable notifiable model name to show in view or email.
741 742 743 |
# File 'lib/activity_notification/apis/notification_api.rb', line 741 def printable_notifiable_name notifiable.printable_notifiable_name(target, key) end |
#publish_to_optional_targets(options = {}) ⇒ Hash
Publishes notification to the optional targets.
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/activity_notification/apis/notification_api.rb', line 584 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) begin optional_target.notify(self, [optional_target_name] || {}) [optional_target_name, true] rescue => e Rails.logger.error(e) if ActivityNotification.config.rescue_optional_target_errors [optional_target_name, e] else raise e end end else [optional_target_name, false] end }.to_h end |
#remove_from_group ⇒ Notificaion
Remove from notification group and make a new group owner.
722 723 724 725 726 727 728 729 |
# File 'lib/activity_notification/apis/notification_api.rb', line 722 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, ...
Sends notification email to the target.
569 570 571 572 573 574 575 576 577 578 |
# File 'lib/activity_notification/apis/notification_api.rb', line 569 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 ? @@notification_mailer.send_notification_email(self, ).deliver_later : @@notification_mailer.send_notification_email(self, ).deliver_now end end |
#subscribed? ⇒ Boolean
Returns if the target subscribes this notification.
747 748 749 |
# File 'lib/activity_notification/apis/notification_api.rb', line 747 def subscribed? target.subscribes_to_notification?(key) end |
#unopened? ⇒ Boolean
Returns if the notification is unopened.
625 626 627 |
# File 'lib/activity_notification/apis/notification_api.rb', line 625 def unopened? !opened? end |