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

Instance Method Summary collapse

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.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :reverse (Boolean) — default: false

    If notification index will be ordered as earliest first

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :as_latest_group_member (Boolean) — default: false

    If grouped notification will be shown as the latest group member (default is shown as the earliest member)

  • :filtered_by_status (String) — default: :all

    Status for filter, :all, :opened and :unopened are available

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Array<Notificaion>)

    All notifications for this target type



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/activity_notification/models/concerns/target.rb', line 68

def all_notifications(options = {})
  reverse                = options[:reverse] || false
  with_group_members     = options[:with_group_members] || false
  as_latest_group_member = options[:as_latest_group_member] || false
  target_notifications = Notification.filtered_by_target_type(self.name)
                                     .all_index!(reverse, with_group_members)
                                     .filtered_by_options(options)
                                     .with_target
  case options[: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(options[:limit]) if options[: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.

Returns:

  • (Boolean)

    Always true



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.

Examples:

Get all notifications for for users grouped by user

@notification_index_map = User.notification_index_map
@notification_index_map.each do |user, notifications|
  # Do something for user and notifications
end

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :reverse (Boolean) — default: false

    If notification index will be ordered as earliest first

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :as_latest_group_member (Boolean) — default: false

    If grouped notification will be shown as the latest group member (default is shown as the earliest member)

  • :filtered_by_status (String) — default: :all

    Status for filter, :all, :opened and :unopened are available

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Hash<Target, Notificaion>)

    All notifications for this target type grouped by targets



110
111
112
# File 'lib/activity_notification/models/concerns/target.rb', line 110

def notification_index_map(options = {})
  all_notifications(options).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.

Parameters:

  • current_resource (Object)

    Current resource signed in with Devise

Returns:

  • (Object)

    Current authenticated target by devise authentication



150
151
152
# File 'lib/activity_notification/models/concerns/target.rb', line 150

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.

Examples:

Send batch notification email to the users with unopened notifications of specified key

User.send_batch_unopened_notification_email(filtered_by_key: 'this.key')

Send batch notification email to the users with unopened notifications of specified key in 1 hour

User.send_batch_unopened_notification_email(filtered_by_key: 'this.key', custom_filter: ["created_at >= ?", time.hour.ago])

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :reverse (Boolean) — default: false

    If notification index will be ordered as earliest first

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :as_latest_group_member (Boolean) — default: false

    If grouped notification will be shown as the latest group member (default is shown as the earliest member)

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

  • :send_later (Boolean) — default: false

    If it sends notification email asynchronously

  • :fallback (String, Symbol) — default: :batch_default

    Fallback template to use when MissingTemplate is raised

  • :batch_key (String) — default: nil

    Key of the batch notification email, a key of the first notification will be used if not specified

Returns:

  • (Hash<Object, Mail::Message|ActionMailer::DeliveryJob>)

    Hash of target and sent email message or its delivery job



137
138
139
140
141
142
143
# File 'lib/activity_notification/models/concerns/target.rb', line 137

def send_batch_unopened_notification_email(options = {})
  unopened_notification_index_map = notification_index_map(options.merge(filtered_by_status: :unopened))
  mailer_options = options.select { |k, _| [:send_later, :fallback, :batch_key].include?(k) }
  unopened_notification_index_map.map { |target, notifications|
    [target, Notification.send_batch_notification_email(target, notifications, mailer_options)]
  }.to_h
end

.set_target_class_defaultsNilClass

Sets default values to target class fields.

Returns:

  • (NilClass)

    nil



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 || ActivityNotification.config.action_cable_api_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.

Returns:

  • (Boolean)

    If subscription management is allowed for this target type



156
157
158
# File 'lib/activity_notification/models/concerns/target.rb', line 156

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.

Parameters:

  • current_resource (Object)

    Current resource signed in with Devise

Returns:

  • (Boolean)

    If current resource signed in with Devise is authenticated for the notification



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.

Parameters:

  • key (String)

    Key of the notifications

Returns:

  • (Boolean)

    If sending batch notification email is allowed for the target



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.

Parameters:

  • options (Hash) (defaults to: {})

    Options for notification index

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Boolean)

    If the target has unopened notifications



284
285
286
# File 'lib/activity_notification/models/concerns/target.rb', line 284

def has_unopened_notifications?(options = {})
  _unopened_notification_index(options).exists?
end

#mailer_toString

Returns target email address for email notification. This method is able to be overridden.

Returns:

  • (String)

    Target email address



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.

Parameters:

  • notifiable (Object) (defaults to: nil)

    Notifiable instance of the notification

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Boolean)

    If publishing WebSocket using ActionCable is allowed for the target



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_nameString

Returns notification ActionCable channel class name from action_cable_with_devise? configuration.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    If publishing WebSocket using ActionCable is allowed for the target



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_resourceObject

Returns Devise resource model associated with this target.

Returns:

  • (Object)

    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.

Parameters:

  • notifiable (Object)

    Notifiable instance of the notification

  • key (String)

    Key of the notification

Returns:

  • (Boolean)

    If sending notification email is allowed for the target



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>

TODO:

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.

Examples:

Get automatically arranged notification index of @user

@notifications = @user.notification_index

Parameters:

  • options (Hash) (defaults to: {})

    Options for notification index

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :reverse (Boolean) — default: false

    If notification index will be ordered as earliest first

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :as_latest_group_member (Boolean) — default: false

    If grouped notification will be shown as the latest group member (default is shown as the earliest member)

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Array<Notificaion>)

    Notification index of the target



311
312
313
314
315
# File 'lib/activity_notification/models/concerns/target.rb', line 311

def notification_index(options = {})
  arrange_notification_index(method(:unopened_notification_index),
                             method(:opened_notification_index),
                             options)
end

#notification_index_with_attributes(options = {}) ⇒ Array<Notificaion>

TODO:

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.

Examples:

Get automatically arranged notification index of the @user with included attributes

@notifications = @user.notification_index_with_attributes

Parameters:

  • options (Hash) (defaults to: {})

    Options for notification index

Options Hash (options):

  • :send_later (Boolean) — default: false

    If it sends notification email asynchronously

  • :fallback (String, Symbol) — default: :batch_default

    Fallback template to use when MissingTemplate is raised

  • :batch_key (String) — default: nil

    Key of the batch notification email, a key of the first notification will be used if not specified

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :reverse (Boolean) — default: false

    If notification index will be ordered as earliest first

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :as_latest_group_member (Boolean) — default: false

    If grouped notification will be shown as the latest group member (default is shown as the earliest member)

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Array<Notificaion>)

    Notification index of the target with attributes



449
450
451
452
453
# File 'lib/activity_notification/models/concerns/target.rb', line 449

def notification_index_with_attributes(options = {})
  arrange_notification_index(method(:unopened_notification_index_with_attributes),
                             method(:opened_notification_index_with_attributes),
                             options)
end

#notificationsArray<Notificaion>, Mongoid::Criteria<Notificaion>

Has many notification instances of this target.

Returns:

  • (Array<Notificaion>, Mongoid::Criteria<Notificaion>)

    Array or database query of notifications 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 = {}) ⇒ Array<Notification>

Opens all notifications of this target. This method calls NotificationApi#open_all_of internally with self target instance.

Parameters:

  • options (Hash) (defaults to: {})

    Options for opening notifications

Options Hash (options):

  • :opened_at (DateTime) — default: Time.current

    Time to set to opened_at of the notification record

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

Returns:

See Also:



418
419
420
# File 'lib/activity_notification/models/concerns/target.rb', line 418

def open_all_notifications(options = {})
  Notification.open_all_of(self, options)
end

#opened_notification_index(options = {}) ⇒ Array<Notificaion>

Returns opened notification index of the target.

Examples:

Get opened notification index of @user

@notifications = @user.opened_notification_index(10)

Parameters:

  • options (Hash) (defaults to: {})

    Options for notification index

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :reverse (Boolean) — default: false

    If notification index will be ordered as earliest first

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :as_latest_group_member (Boolean) — default: false

    If grouped notification will be shown as the latest group member (default is shown as the earliest member)

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Array<Notificaion>)

    Opened notification index of the target



359
360
361
# File 'lib/activity_notification/models/concerns/target.rb', line 359

def opened_notification_index(options = {})
  arrange_single_notification_index(method(:_opened_notification_index), options)
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.

Examples:

Get opened notification index of the @user with included attributes

@notifications = @user.opened_notification_index_with_attributes(10)

Parameters:

  • options (Hash) (defaults to: {})

    Options for notification index

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :reverse (Boolean) — default: false

    If notification index will be ordered as earliest first

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :as_latest_group_member (Boolean) — default: false

    If grouped notification will be shown as the latest group member (default is shown as the earliest member)

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Array<Notificaion>)

    Opened notification index of the target with attributes



497
498
499
# File 'lib/activity_notification/models/concerns/target.rb', line 497

def opened_notification_index_with_attributes(options = {})
  include_attributes(_opened_notification_index(options)).to_a
end

#printable_target_nameString

Returns printable target model name to show in view or email.

Returns:

  • (String)

    Printable target model name



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.

Parameters:

  • notifiable (Object)

    Notifiable instance to notify

  • options (Hash) (defaults to: {})

    Options for notifications

Options Hash (options):

  • :key (String) — default: notifiable.default_notification_key

    Key of the notification

  • :group (Object) — default: nil

    Group unit of the notifications

  • :group_expiry_delay (ActiveSupport::Duration) — default: nil

    Expiry period of a notification group

  • :notifier (Object) — default: nil

    Notifier of the notifications

  • :parameters (Hash) — default: {}

    Additional parameters of the notifications

  • :send_email (Boolean) — default: true

    Whether it sends notification email

  • :send_later (Boolean) — default: true

    Whether it sends notification email asynchronously

  • :publish_optional_targets (Boolean) — default: true

    Whether it publishes notification to optional targets

  • :optional_targets (Hash<String, Hash>) — default: {}

    Options for optional targets, keys are optional target name (:amazon_sns or :slack etc) and values are options

Returns:

See Also:



400
401
402
# File 'lib/activity_notification/models/concerns/target.rb', line 400

def receive_notification_later_of(notifiable, options = {})
  Notification.notify_later_to(self, notifiable, options)
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.

Parameters:

  • notifiable (Object)

    Notifiable instance to notify

  • options (Hash) (defaults to: {})

    Options for notifications

Options Hash (options):

  • :key (String) — default: notifiable.default_notification_key

    Key of the notification

  • :group (Object) — default: nil

    Group unit of the notifications

  • :group_expiry_delay (ActiveSupport::Duration) — default: nil

    Expiry period of a notification group

  • :notifier (Object) — default: nil

    Notifier of the notifications

  • :parameters (Hash) — default: {}

    Additional parameters of the notifications

  • :send_email (Boolean) — default: true

    Whether it sends notification email

  • :send_later (Boolean) — default: true

    Whether it sends notification email asynchronously

  • :publish_optional_targets (Boolean) — default: true

    Whether it publishes notification to optional targets

  • :optional_targets (Hash<String, Hash>) — default: {}

    Options for optional targets, keys are optional target name (:amazon_sns or :slack etc) and values are options

Returns:

See Also:



379
380
381
# File 'lib/activity_notification/models/concerns/target.rb', line 379

def receive_notification_of(notifiable, options = {})
  Notification.notify_to(self, notifiable, options)
end

#send_batch_notification_email(notifications, options = {}) ⇒ Mail::Message|ActionMailer::DeliveryJob|NilClass

Sends batch notification email to the target.

Parameters:

  • notifications (Array<Notification>)

    Target notifications to send batch notification email

  • options (Hash) (defaults to: {})

    Options for notification email

Options Hash (options):

  • :send_later (Boolean) — default: false

    If it sends notification email asynchronously

  • :fallback (String, Symbol) — default: :batch_default

    Fallback template to use when MissingTemplate is raised

  • :batch_key (String) — default: nil

    Key of the batch notification email, a key of the first notification will be used if not specified

Returns:

  • (Mail::Message|ActionMailer::DeliveryJob|NilClass)

    Email message or its delivery job, return NilClass for wrong target



521
522
523
524
525
526
# File 'lib/activity_notification/models/concerns/target.rb', line 521

def send_batch_notification_email(notifications, options = {})
  return if notifications.blank?
  if notifications.map{ |n| n.target }.uniq == [self]
    Notification.send_batch_notification_email(self, notifications, options)
  end
end

#send_notification_email(notification, options = {}) ⇒ Mail::Message|ActionMailer::DeliveryJob

Sends notification email to the target.

Parameters:

  • options (Hash) (defaults to: {})

    Options for notification email

Options Hash (options):

  • :send_later (Boolean)

    If it sends notification email asynchronously

  • :fallback (String, Symbol) — default: :default

    Fallback template to use when MissingTemplate is raised

Returns:

  • (Mail::Message|ActionMailer::DeliveryJob)

    Email message or its delivery job, return NilClass for wrong target



507
508
509
510
511
# File 'lib/activity_notification/models/concerns/target.rb', line 507

def send_notification_email(notification, options = {})
  if notification.target == self
    notification.send_notification_email(options)
  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.

Parameters:

  • key (String)

    Key of the notification

  • subscribe_as_default (Boolean) (defaults to: ActivityNotification.config.subscribe_as_default)

    Default subscription value to use when the subscription record does not configured

Returns:

  • (Boolean)

    If the target subscribes the notification or 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.

Parameters:

  • key (String)

    Key of the notification

  • subscribe_as_default (Boolean) (defaults to: ActivityNotification.config.subscribe_to_email_as_default)

    Default subscription value to use when the subscription record does not configured

Returns:

  • (Boolean)

    If the target subscribes the notification email or 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.

Parameters:

  • key (String)

    Key of the notification

  • optional_target_name (String, Symbol)

    Class name of the optional target implementation (e.g. :amazon_sns, :slack)

  • subscribe_as_default (Boolean) (defaults to: ActivityNotification.config.subscribe_to_optional_targets_as_default)

    Default subscription value to use when the subscription record does not configured

Returns:

  • (Boolean)

    If the target subscribes the notification email or 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.

Parameters:

  • key (String)

    Key of the notifications

Returns:

  • (Boolean)

    If subscription management is allowed for the target



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.

Parameters:

  • options (Hash) (defaults to: {})

    Options for notification index

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Integer)

    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(options = {})
  target_notifications = _unopened_notification_index(options)
  target_notifications.present? ? target_notifications.count : 0
end

#unopened_notification_index(options = {}) ⇒ Array<Notificaion>

Returns unopened notification index of the target.

Examples:

Get unopened notification index of @user

@notifications = @user.unopened_notification_index

Parameters:

  • options (Hash) (defaults to: {})

    Options for notification index

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :reverse (Boolean) — default: false

    If notification index will be ordered as earliest first

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :as_latest_group_member (Boolean) — default: false

    If grouped notification will be shown as the latest group member (default is shown as the earliest member)

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Array<Notificaion>)

    Unopened notification index of the target



336
337
338
# File 'lib/activity_notification/models/concerns/target.rb', line 336

def unopened_notification_index(options = {})
  arrange_single_notification_index(method(:_unopened_notification_index), options)
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.

Examples:

Get unopened notification index of the @user with included attributes

@notifications = @user.unopened_notification_index_with_attributes

Parameters:

  • options (Hash) (defaults to: {})

    Options for notification index

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for notifications

  • :reverse (Boolean) — default: false

    If notification index will be ordered as earliest first

  • :with_group_members (Boolean) — default: false

    If notification index will include group members

  • :as_latest_group_member (Boolean) — default: false

    If grouped notification will be shown as the latest group member (default is shown as the earliest member)

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notifications later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notifications earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Array<Notificaion>)

    Unopened notification index of the target with attributes



474
475
476
# File 'lib/activity_notification/models/concerns/target.rb', line 474

def unopened_notification_index_with_attributes(options = {})
  include_attributes(_unopened_notification_index(options)).to_a
end