Module: ActivityNotification::NotificationApi
- Extended by:
- ActiveSupport::Concern
- Includes:
- CascadingNotificationApi
- 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.
Instance Method Summary collapse
-
#after_store ⇒ Object
Call after store action with stored notification.
-
#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 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.
Methods included from CascadingNotificationApi
#cascade_in_progress?, #cascade_notify, #validate_cascade_config
Instance Method Details
#after_store ⇒ Object
Call after store action with stored notification
632 633 |
# File 'lib/activity_notification/apis/notification_api.rb', line 632 def after_store end |
#email_subscribed? ⇒ Boolean
Returns if the target subscribes this notification email.
826 827 828 |
# File 'lib/activity_notification/apis/notification_api.rb', line 826 def email_subscribed? target.subscribes_to_notification_email?(key) end |
#group_member? ⇒ Boolean
Returns if the notification is group member belonging to owner.
719 720 721 |
# File 'lib/activity_notification/apis/notification_api.rb', line 719 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.
748 749 750 |
# File 'lib/activity_notification/apis/notification_api.rb', line 748 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.
728 729 730 |
# File 'lib/activity_notification/apis/notification_api.rb', line 728 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.
768 769 770 |
# File 'lib/activity_notification/apis/notification_api.rb', line 768 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.
739 740 741 |
# File 'lib/activity_notification/apis/notification_api.rb', line 739 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.
757 758 759 |
# File 'lib/activity_notification/apis/notification_api.rb', line 757 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.
778 779 780 781 |
# File 'lib/activity_notification/apis/notification_api.rb', line 778 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.
712 713 714 |
# File 'lib/activity_notification/apis/notification_api.rb', line 712 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.
787 788 789 790 |
# File 'lib/activity_notification/apis/notification_api.rb', line 787 def latest_group_member notification = group_member? && group_owner.present? ? 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.
807 808 809 810 |
# File 'lib/activity_notification/apis/notification_api.rb', line 807 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.
685 686 687 688 689 690 691 692 693 |
# File 'lib/activity_notification/apis/notification_api.rb', line 685 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.
705 706 707 |
# File 'lib/activity_notification/apis/notification_api.rb', line 705 def opened? opened_at.present? end |
#optional_target_names ⇒ Array<Symbol>
Returns optional_target names of the notification from configured field or overridden method.
845 846 847 |
# File 'lib/activity_notification/apis/notification_api.rb', line 845 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.
833 834 835 |
# File 'lib/activity_notification/apis/notification_api.rb', line 833 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.
839 840 841 |
# File 'lib/activity_notification/apis/notification_api.rb', line 839 def optional_targets notifiable.optional_targets(target.to_resources_name, key) end |
#prepare_to_store ⇒ Object
:nocov: Returns prepared notification object to store
627 628 629 |
# File 'lib/activity_notification/apis/notification_api.rb', line 627 def prepare_to_store self end |
#printable_notifiable_name ⇒ String
Returns printable notifiable model name to show in view or email.
814 815 816 |
# File 'lib/activity_notification/apis/notification_api.rb', line 814 def printable_notifiable_name notifiable.printable_notifiable_name(target, key) end |
#publish_to_optional_targets(options = {}) ⇒ Hash
Publishes notification to the optional targets.
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 |
# File 'lib/activity_notification/apis/notification_api.rb', line 657 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.
795 796 797 798 799 800 801 802 |
# File 'lib/activity_notification/apis/notification_api.rb', line 795 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.
642 643 644 645 646 647 648 649 650 651 |
# File 'lib/activity_notification/apis/notification_api.rb', line 642 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.
820 821 822 |
# File 'lib/activity_notification/apis/notification_api.rb', line 820 def subscribed? target.subscribes_to_notification?(key) end |
#unopened? ⇒ Boolean
Returns if the notification is unopened.
698 699 700 |
# File 'lib/activity_notification/apis/notification_api.rb', line 698 def unopened? !opened? end |