Module: ActivityNotification::Target
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/activity_notification/models/concerns/target.rb
Overview
Target implementation included in target model to notify, like users or administrators.
Instance Method Summary collapse
-
#authenticated_with_devise?(current_resource) ⇒ Boolean
Returns if current resource signed in with Devise is authenticated for the notification.
-
#batch_notification_email_allowed?(key) ⇒ Boolean
Returns if sending batch notification email is allowed for the target from configured field or overridden method.
-
#has_unopened_notifications?(options = {}) ⇒ Boolean
Returns if the target has unopened notifications.
-
#mailer_to ⇒ String
Returns target email address for email notification.
-
#notification_action_cable_allowed?(notifiable = nil, key = nil) ⇒ Boolean
Returns if publishing WebSocket using ActionCable is allowed for the target from configured field or overridden method.
-
#notification_action_cable_channel_class_name ⇒ String
Returns notification ActionCable channel class name from action_cable_with_devise? configuration.
-
#notification_action_cable_with_devise? ⇒ Boolean
Returns if publishing WebSocket using ActionCable is allowed only for the authenticated target with Devise from configured field or overridden method.
-
#notification_devise_resource ⇒ Object
Returns Devise resource model associated with this target.
-
#notification_email_allowed?(notifiable, key) ⇒ Boolean
Returns if sending notification email is allowed for the target from configured field or overridden method.
-
#notification_index(options = {}) ⇒ Array<Notificaion>
Returns automatically arranged notification index of the target.
-
#notification_index_with_attributes(options = {}) ⇒ Array<Notificaion>
Gets automatically arranged notification index of the target with included attributes like target, notifiable, group and notifier.
-
#open_all_notifications(options = {}) ⇒ Array<Notification>
Opens all notifications of this target.
-
#opened_notification_index(options = {}) ⇒ Array<Notificaion>
Returns opened notification index of the target.
-
#opened_notification_index_with_attributes(options = {}) ⇒ Array<Notificaion>
Gets opened notification index of the target with including attributes like target, notifiable, group and notifier.
-
#printable_target_name ⇒ String
Returns printable target model name to show in view or email.
-
#receive_notification_later_of(notifiable, options = {}) ⇒ Notification
Generates notifications to this target later by ActiveJob queue.
-
#receive_notification_of(notifiable, options = {}) ⇒ Notification
(also: #receive_notification_now_of)
Generates notifications to this target.
-
#send_batch_notification_email(notifications, options = {}) ⇒ Mail::Message|ActionMailer::DeliveryJob|NilClass
Sends batch notification email to the target.
-
#send_notification_email(notification, options = {}) ⇒ Mail::Message|ActionMailer::DeliveryJob
Sends notification email to the target.
-
#subscribes_to_notification?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default) ⇒ Boolean
Returns if the target subscribes to the notification.
-
#subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_to_email_as_default) ⇒ Boolean
(also: #subscribes_to_email?)
Returns if the target subscribes to the notification email.
-
#subscribes_to_optional_target?(key, optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_to_optional_targets_as_default) ⇒ Boolean
Returns if the target subscribes to the specified optional target.
-
#subscription_allowed?(key) ⇒ Boolean
(also: #notification_subscription_allowed?)
Returns if subscription management is allowed for the target from configured field or overridden method.
-
#unopened_notification_count(options = {}) ⇒ Integer
Returns count of unopened notifications of the target.
-
#unopened_notification_index(options = {}) ⇒ Array<Notificaion>
Returns unopened notification index of the target.
-
#unopened_notification_index_with_attributes(options = {}) ⇒ Array<Notificaion>
Gets unopened notification index of the target with included attributes like target, notifiable, group and notifier.
Instance Method Details
#authenticated_with_devise?(current_resource) ⇒ Boolean
Returns if current resource signed in with Devise is authenticated for the notification. This method is able to be overridden.
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/activity_notification/models/concerns/target.rb', line 235 def authenticated_with_devise?(current_resource) devise_resource = notification_devise_resource unless current_resource.blank? or current_resource.is_a? devise_resource.class raise TypeError, "Different type of current resource #{current_resource.class} "\ "with devise resource #{devise_resource.class} has been passed to #{self.class}##{__method__}. "\ "You have to override #{self.class}##{__method__} method or set devise_resource in acts_as_target." end current_resource.present? && current_resource == devise_resource end |
#batch_notification_email_allowed?(key) ⇒ Boolean
Returns if sending batch notification email is allowed for the target from configured field or overridden method. This method is able to be overridden.
185 186 187 |
# File 'lib/activity_notification/models/concerns/target.rb', line 185 def batch_notification_email_allowed?(key) resolve_value(_batch_notification_email_allowed, key) end |
#has_unopened_notifications?(options = {}) ⇒ Boolean
Returns if the target has unopened notifications.
284 285 286 |
# File 'lib/activity_notification/models/concerns/target.rb', line 284 def has_unopened_notifications?( = {}) _unopened_notification_index().exists? end |
#mailer_to ⇒ String
Returns target email address for email notification. This method is able to be overridden.
166 167 168 |
# File 'lib/activity_notification/models/concerns/target.rb', line 166 def mailer_to resolve_value(_notification_email) end |
#notification_action_cable_allowed?(notifiable = nil, key = nil) ⇒ Boolean
Returns if publishing WebSocket using ActionCable is allowed for the target from configured field or overridden method. This method is able to be overridden.
205 206 207 |
# File 'lib/activity_notification/models/concerns/target.rb', line 205 def notification_action_cable_allowed?(notifiable = nil, key = nil) resolve_value(_notification_action_cable_allowed, notifiable, key) end |
#notification_action_cable_channel_class_name ⇒ String
Returns notification ActionCable channel class name from action_cable_with_devise? configuration.
219 220 221 |
# File 'lib/activity_notification/models/concerns/target.rb', line 219 def notification_action_cable_channel_class_name notification_action_cable_with_devise? ? "ActivityNotification::NotificationWithDeviseChannel" : "ActivityNotification::NotificationChannel" end |
#notification_action_cable_with_devise? ⇒ Boolean
Returns if publishing WebSocket using ActionCable is allowed only for the authenticated target with Devise from configured field or overridden method.
212 213 214 |
# File 'lib/activity_notification/models/concerns/target.rb', line 212 def notification_action_cable_with_devise? resolve_value(_notification_action_cable_with_devise) end |
#notification_devise_resource ⇒ Object
Returns Devise resource model associated with this target.
226 227 228 |
# File 'lib/activity_notification/models/concerns/target.rb', line 226 def notification_devise_resource resolve_value(_notification_devise_resource) end |
#notification_email_allowed?(notifiable, key) ⇒ Boolean
Returns if sending notification email is allowed for the target from configured field or overridden method. This method is able to be overridden.
176 177 178 |
# File 'lib/activity_notification/models/concerns/target.rb', line 176 def notification_email_allowed?(notifiable, key) resolve_value(_notification_email_allowed, notifiable, key) end |
#notification_index(options = {}) ⇒ Array<Notificaion>
Is this conbimned array the best solution?
Returns automatically arranged notification index of the target. This method is the typical way to get notification index from controller and view. When the target has unopened notifications, it returns unopened notifications first. Additionaly, it returns opened notifications unless unopened index size overs the limit.
311 312 313 314 315 |
# File 'lib/activity_notification/models/concerns/target.rb', line 311 def notification_index( = {}) arrange_notification_index(method(:unopened_notification_index), method(:opened_notification_index), ) end |
#notification_index_with_attributes(options = {}) ⇒ Array<Notificaion>
Is this switching the best solution?
Gets automatically arranged notification index of the target with included attributes like target, notifiable, group and notifier. This method is the typical way to get notifications index from controller of view. When the target have unopened notifications, it returns unopened notifications first. Additionaly, it returns opened notifications unless unopened index size overs the limit.
449 450 451 452 453 |
# File 'lib/activity_notification/models/concerns/target.rb', line 449 def notification_index_with_attributes( = {}) arrange_notification_index(method(:unopened_notification_index_with_attributes), method(:opened_notification_index_with_attributes), ) end |
#open_all_notifications(options = {}) ⇒ Array<Notification>
Opens all notifications of this target. This method calls NotificationApi#open_all_of internally with self target instance.
418 419 420 |
# File 'lib/activity_notification/models/concerns/target.rb', line 418 def open_all_notifications( = {}) Notification.open_all_of(self, ) end |
#opened_notification_index(options = {}) ⇒ Array<Notificaion>
Returns opened notification index of the target.
359 360 361 |
# File 'lib/activity_notification/models/concerns/target.rb', line 359 def opened_notification_index( = {}) arrange_single_notification_index(method(:_opened_notification_index), ) end |
#opened_notification_index_with_attributes(options = {}) ⇒ Array<Notificaion>
Gets opened notification index of the target with including attributes like target, notifiable, group and notifier.
497 498 499 |
# File 'lib/activity_notification/models/concerns/target.rb', line 497 def opened_notification_index_with_attributes( = {}) include_attributes(_opened_notification_index()).to_a end |
#printable_target_name ⇒ String
Returns printable target model name to show in view or email.
248 249 250 |
# File 'lib/activity_notification/models/concerns/target.rb', line 248 def printable_target_name resolve_value(_printable_notification_target_name) end |
#receive_notification_later_of(notifiable, options = {}) ⇒ Notification
Generates notifications to this target later by ActiveJob queue. This method calls NotificationApi#notify_later_to internally with self target instance.
400 401 402 |
# File 'lib/activity_notification/models/concerns/target.rb', line 400 def receive_notification_later_of(notifiable, = {}) Notification.notify_later_to(self, notifiable, ) end |
#receive_notification_of(notifiable, options = {}) ⇒ Notification Also known as: receive_notification_now_of
Generates notifications to this target. This method calls NotificationApi#notify_to internally with self target instance.
379 380 381 |
# File 'lib/activity_notification/models/concerns/target.rb', line 379 def receive_notification_of(notifiable, = {}) Notification.notify_to(self, notifiable, ) end |
#send_batch_notification_email(notifications, options = {}) ⇒ Mail::Message|ActionMailer::DeliveryJob|NilClass
Sends batch notification email to the target.
521 522 523 524 525 526 |
# File 'lib/activity_notification/models/concerns/target.rb', line 521 def send_batch_notification_email(notifications, = {}) return if notifications.blank? if notifications.map{ |n| n.target }.uniq == [self] Notification.send_batch_notification_email(self, notifications, ) end end |
#send_notification_email(notification, options = {}) ⇒ Mail::Message|ActionMailer::DeliveryJob
Sends notification email to the target.
507 508 509 510 511 |
# File 'lib/activity_notification/models/concerns/target.rb', line 507 def send_notification_email(notification, = {}) if notification.target == self notification.send_notification_email() end end |
#subscribes_to_notification?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default) ⇒ Boolean
Returns if the target subscribes to the notification. It also returns true when the subscription management is not allowed for the target.
534 535 536 |
# File 'lib/activity_notification/models/concerns/target.rb', line 534 def subscribes_to_notification?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default) !subscription_allowed?(key) || _subscribes_to_notification?(key, subscribe_as_default) end |
#subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_to_email_as_default) ⇒ Boolean Also known as: subscribes_to_email?
Returns if the target subscribes to the notification email. It also returns true when the subscription management is not allowed for the target.
544 545 546 |
# File 'lib/activity_notification/models/concerns/target.rb', line 544 def subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_to_email_as_default) !subscription_allowed?(key) || _subscribes_to_notification_email?(key, subscribe_as_default) end |
#subscribes_to_optional_target?(key, optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_to_optional_targets_as_default) ⇒ Boolean
Returns if the target subscribes to the specified optional target. It also returns true when the subscription management is not allowed for the target.
556 557 558 |
# File 'lib/activity_notification/models/concerns/target.rb', line 556 def subscribes_to_optional_target?(key, optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_to_optional_targets_as_default) !subscription_allowed?(key) || _subscribes_to_optional_target?(key, optional_target_name, subscribe_as_default) end |
#subscription_allowed?(key) ⇒ Boolean Also known as: notification_subscription_allowed?
Returns if subscription management is allowed for the target from configured field or overridden method. This method is able to be overridden.
194 195 196 |
# File 'lib/activity_notification/models/concerns/target.rb', line 194 def subscription_allowed?(key) resolve_value(_notification_subscription_allowed, key) end |
#unopened_notification_count(options = {}) ⇒ Integer
Returns count of unopened notifications of the target.
266 267 268 269 |
# File 'lib/activity_notification/models/concerns/target.rb', line 266 def unopened_notification_count( = {}) target_notifications = _unopened_notification_index() target_notifications.present? ? target_notifications.count : 0 end |
#unopened_notification_index(options = {}) ⇒ Array<Notificaion>
Returns unopened notification index of the target.
336 337 338 |
# File 'lib/activity_notification/models/concerns/target.rb', line 336 def unopened_notification_index( = {}) arrange_single_notification_index(method(:_unopened_notification_index), ) end |
#unopened_notification_index_with_attributes(options = {}) ⇒ Array<Notificaion>
Gets unopened notification index of the target with included attributes like target, notifiable, group and notifier.
474 475 476 |
# File 'lib/activity_notification/models/concerns/target.rb', line 474 def unopened_notification_index_with_attributes( = {}) include_attributes(_unopened_notification_index()).to_a end |