Module: ActivityNotification::Target
- Extended by:
- ActiveSupport::Concern
- Includes:
- Association, Common
- Defined in:
- lib/activity_notification/models/concerns/target.rb
Overview
Target implementation included in target model to notify, like users or administrators.
Class Method Summary collapse
-
.all_notifications(options = {}) ⇒ Array<Notificaion>
Gets all notifications for this target type.
-
.available_as_target? ⇒ Boolean
Checks if the model includes target and target methods are available.
-
.notification_index_map(options = {}) ⇒ Hash<Target, Notificaion>
Gets all notifications for this target type grouped by targets.
-
.resolve_current_devise_target(current_resource) ⇒ Object
Resolves current authenticated target by devise authentication from current resource signed in with Devise.
-
.send_batch_unopened_notification_email(options = {}) ⇒ Hash<Object, Mail::Message|ActionMailer::DeliveryJob>
Send batch notification email to this type targets with unopened notifications.
-
.set_target_class_defaults ⇒ NilClass
Sets default values to target class fields.
-
.subscription_enabled? ⇒ Boolean
(also: notification_subscription_enabled?)
Returns if subscription management is allowed for this target type.
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.
-
#notifications ⇒ Array<Notificaion>, Mongoid::Criteria<Notificaion>
Has many notification instances of this target.
-
#open_all_notifications(options = {}) ⇒ Integer
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_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_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.
Methods included from Association
belongs_to_composite_xdb_record, belongs_to_polymorphic_xdb_record, filtered_by_association, has_many_composite_xdb_records, has_many_polymorphic_xdb_records, has_many_records, #update
Methods included from Common
#printable_name, #printable_type, #resolve_value, #to_class_name, #to_resource_name, #to_resources_name
Class Method Details
.all_notifications(options = {}) ⇒ Array<Notificaion>
Gets all notifications for this target type.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/activity_notification/models/concerns/target.rb', line 66 def all_notifications( = {}) reverse = [:reverse] || false with_group_members = [:with_group_members] || false as_latest_group_member = [:as_latest_group_member] || false target_notifications = Notification.filtered_by_target_type(self.name) .all_index!(reverse, with_group_members) .() .with_target case [:filtered_by_status] when :opened, 'opened' target_notifications = target_notifications.opened_only! when :unopened, 'unopened' target_notifications = target_notifications.unopened_only end target_notifications = target_notifications.limit([:limit]) if [:limit].present? as_latest_group_member ? target_notifications.latest_order!(reverse).map{ |n| n.latest_group_member } : target_notifications.latest_order!(reverse).to_a end |
.available_as_target? ⇒ Boolean
Checks if the model includes target and target methods are available.
33 34 35 |
# File 'lib/activity_notification/models/concerns/target.rb', line 33 def available_as_target? true end |
.notification_index_map(options = {}) ⇒ Hash<Target, Notificaion>
Gets all notifications for this target type grouped by targets.
106 107 108 |
# File 'lib/activity_notification/models/concerns/target.rb', line 106 def notification_index_map( = {}) all_notifications().group_by(&:target) end |
.resolve_current_devise_target(current_resource) ⇒ Object
Resolves current authenticated target by devise authentication from current resource signed in with Devise. This method is able to be overridden.
144 145 146 |
# File 'lib/activity_notification/models/concerns/target.rb', line 144 def resolve_current_devise_target(current_resource) _notification_current_devise_target.call(current_resource) end |
.send_batch_unopened_notification_email(options = {}) ⇒ Hash<Object, Mail::Message|ActionMailer::DeliveryJob>
Send batch notification email to this type targets with unopened notifications.
131 132 133 134 135 136 137 |
# File 'lib/activity_notification/models/concerns/target.rb', line 131 def send_batch_unopened_notification_email( = {}) unopened_notification_index_map = notification_index_map(.merge(filtered_by_status: :unopened)) = .select { |k, _| [:send_later, :fallback, :batch_key].include?(k) } unopened_notification_index_map.map { |target, notifications| [target, Notification.send_batch_notification_email(target, notifications, )] }.to_h end |
.set_target_class_defaults ⇒ NilClass
Sets default values to target class fields.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/activity_notification/models/concerns/target.rb', line 39 def set_target_class_defaults self._notification_email = nil self._notification_email_allowed = ActivityNotification.config.email_enabled self._batch_notification_email_allowed = ActivityNotification.config.email_enabled self._notification_subscription_allowed = ActivityNotification.config.subscription_enabled self._notification_action_cable_allowed = ActivityNotification.config.action_cable_enabled self._notification_action_cable_with_devise = ActivityNotification.config.action_cable_with_devise self._notification_devise_resource = ->(model) { model } self._notification_current_devise_target = ->(current_resource) { current_resource } self._printable_notification_target_name = :printable_name nil end |
.subscription_enabled? ⇒ Boolean Also known as: notification_subscription_enabled?
Returns if subscription management is allowed for this target type.
150 151 152 |
# File 'lib/activity_notification/models/concerns/target.rb', line 150 def subscription_enabled? _notification_subscription_allowed ? true : false end |
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.
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/activity_notification/models/concerns/target.rb', line 239 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.
179 180 181 |
# File 'lib/activity_notification/models/concerns/target.rb', line 179 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.
160 161 162 |
# File 'lib/activity_notification/models/concerns/target.rb', line 160 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.
199 200 201 |
# File 'lib/activity_notification/models/concerns/target.rb', line 199 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.
218 219 220 |
# File 'lib/activity_notification/models/concerns/target.rb', line 218 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.
206 207 208 |
# File 'lib/activity_notification/models/concerns/target.rb', line 206 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.
230 231 232 |
# File 'lib/activity_notification/models/concerns/target.rb', line 230 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.
170 171 172 |
# File 'lib/activity_notification/models/concerns/target.rb', line 170 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.
309 310 311 312 313 |
# File 'lib/activity_notification/models/concerns/target.rb', line 309 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.
439 440 441 442 443 |
# File 'lib/activity_notification/models/concerns/target.rb', line 439 def notification_index_with_attributes( = {}) arrange_notification_index(method(:unopened_notification_index_with_attributes), method(:opened_notification_index_with_attributes), ) end |
#notifications ⇒ Array<Notificaion>, Mongoid::Criteria<Notificaion>
Has many notification instances of this target.
13 14 15 16 |
# File 'lib/activity_notification/models/concerns/target.rb', line 13 has_many_records :notifications, class_name: "::ActivityNotification::Notification", as: :target, dependent: :delete_all |
#open_all_notifications(options = {}) ⇒ Integer
Opens all notifications of this target. This method calls NotificationApi#open_all_of internally with self target instance.
410 411 412 |
# File 'lib/activity_notification/models/concerns/target.rb', line 410 def open_all_notifications( = {}) Notification.open_all_of(self, ) end |
#opened_notification_index(options = {}) ⇒ Array<Notificaion>
Returns opened notification index of the target.
353 354 355 |
# File 'lib/activity_notification/models/concerns/target.rb', line 353 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.
483 484 485 |
# File 'lib/activity_notification/models/concerns/target.rb', line 483 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.
252 253 254 |
# File 'lib/activity_notification/models/concerns/target.rb', line 252 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.
394 395 396 |
# File 'lib/activity_notification/models/concerns/target.rb', line 394 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.
373 374 375 |
# File 'lib/activity_notification/models/concerns/target.rb', line 373 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.
507 508 509 510 511 512 |
# File 'lib/activity_notification/models/concerns/target.rb', line 507 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.
493 494 495 496 497 |
# File 'lib/activity_notification/models/concerns/target.rb', line 493 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.
520 521 522 |
# File 'lib/activity_notification/models/concerns/target.rb', line 520 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_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.
530 531 532 |
# File 'lib/activity_notification/models/concerns/target.rb', line 530 def subscribes_to_notification_email?(key, subscribe_as_default = ActivityNotification.config.subscribe_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_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.
542 543 544 |
# File 'lib/activity_notification/models/concerns/target.rb', line 542 def subscribes_to_optional_target?(key, optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_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.
188 189 190 |
# File 'lib/activity_notification/models/concerns/target.rb', line 188 def subscription_allowed?(key) resolve_value(_notification_subscription_allowed, key) end |
#unopened_notification_count(options = {}) ⇒ Integer
Returns count of unopened notifications of the target.
268 269 270 271 |
# File 'lib/activity_notification/models/concerns/target.rb', line 268 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.
332 333 334 |
# File 'lib/activity_notification/models/concerns/target.rb', line 332 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.
462 463 464 |
# File 'lib/activity_notification/models/concerns/target.rb', line 462 def unopened_notification_index_with_attributes( = {}) include_attributes(_unopened_notification_index()).to_a end |