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.
-
.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
-
#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.
-
#send_notification_email(options = {}) ⇒ Mail::Message|ActionMailer::DeliveryJob
Sends notification email to the target.
-
#unopened? ⇒ Boolean
Returns if the notification is unopened.
Class Method Details
.available_options ⇒ Array<Notificaion>
Returns available options for kinds of notify methods.
136 137 138 |
# File 'lib/activity_notification/apis/notification_api.rb', line 136 def [:key, :group, :parameters, :notifier, :send_email, :send_later].freeze 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.
108 109 110 |
# File 'lib/activity_notification/apis/notification_api.rb', line 108 def group_member_exists?(notifications) notifications.present? and where(group_owner_id: notifications.map(&:id)).exists? end |
.notify(target_type, notifiable, options = {}) ⇒ Array<Notificaion>
Generates notifications to configured targets with notifiable model.
34 35 36 37 38 39 |
# File 'lib/activity_notification/apis/notification_api.rb', line 34 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.
56 57 58 |
# File 'lib/activity_notification/apis/notification_api.rb', line 56 def notify_all(targets, notifiable, = {}) targets.map { |target| target.notify_to(notifiable, ) } end |
.notify_to(target, notifiable, options = {}) ⇒ Notification
Generates notifications to one target.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/activity_notification/apis/notification_api.rb', line 75 def notify_to(target, notifiable, = {}) send_email = .has_key?(:send_email) ? [:send_email] : true send_later = .has_key?(:send_later) ? [:send_later] : true # Store notification notification = store_notification(target, notifiable, ) # Send notification email notification.send_notification_email({ send_later: send_later }) if send_email # Return created notification notification end |
.open_all_of(target, options = {}) ⇒ Integer
Add filter option
Opens all notifications of the target.
98 99 100 101 |
# File 'lib/activity_notification/apis/notification_api.rb', line 98 def open_all_of(target, = {}) opened_at = [:opened_at] || DateTime.now 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.
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/activity_notification/apis/notification_api.rb', line 121 def send_batch_notification_email(target, notifications, = {}) return if notifications.blank? if target.batch_notification_email_allowed?(notifications.first.notifiable_type, notifications.first.key) send_later = .has_key?(:send_later) ? [:send_later] : true if send_later Mailer.send_batch_notification_email(target, notifications, ).deliver_later else Mailer.send_batch_notification_email(target, notifications, ).deliver_now end end end |
Instance Method Details
#group_member? ⇒ Boolean
Returns if the notification is group member belonging to owner.
219 220 221 |
# File 'lib/activity_notification/apis/notification_api.rb', line 219 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.
248 249 250 |
# File 'lib/activity_notification/apis/notification_api.rb', line 248 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.
228 229 230 |
# File 'lib/activity_notification/apis/notification_api.rb', line 228 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.
268 269 270 |
# File 'lib/activity_notification/apis/notification_api.rb', line 268 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.
239 240 241 |
# File 'lib/activity_notification/apis/notification_api.rb', line 239 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.
257 258 259 |
# File 'lib/activity_notification/apis/notification_api.rb', line 257 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.
278 279 280 281 |
# File 'lib/activity_notification/apis/notification_api.rb', line 278 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.
212 213 214 |
# File 'lib/activity_notification/apis/notification_api.rb', line 212 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.
287 288 289 290 |
# File 'lib/activity_notification/apis/notification_api.rb', line 287 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.
295 296 297 298 |
# File 'lib/activity_notification/apis/notification_api.rb', line 295 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.
188 189 190 191 192 193 |
# File 'lib/activity_notification/apis/notification_api.rb', line 188 def open!( = {}) opened_at = [:opened_at] || DateTime.now 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.
205 206 207 |
# File 'lib/activity_notification/apis/notification_api.rb', line 205 def opened? opened_at.present? end |
#send_notification_email(options = {}) ⇒ Mail::Message|ActionMailer::DeliveryJob
Sends notification email to the target.
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/activity_notification/apis/notification_api.rb', line 170 def send_notification_email( = {}) if target.notification_email_allowed?(notifiable, key) and notifiable.notification_email_allowed?(target, key) send_later = .has_key?(:send_later) ? [:send_later] : true if send_later Mailer.send_notification_email(self, ).deliver_later else Mailer.send_notification_email(self, ).deliver_now end end end |
#unopened? ⇒ Boolean
Returns if the notification is unopened.
198 199 200 |
# File 'lib/activity_notification/apis/notification_api.rb', line 198 def unopened? !opened? end |