Module: ActivityNotification::Notifiable

Extended by:
ActiveSupport::Concern
Includes:
PolymorphicHelpers
Defined in:
lib/activity_notification/models/concerns/notifiable.rb

Overview

Notifiable implementation included in notifiable model to be notified, like comments or any other user activities.

Instance Method Summary collapse

Instance Method Details

#default_notification_keyString

Returns default key of the notification. This method can be overridden. "#to_resource_name.default" is defined as default key.

Returns:

  • (String)

    Default key of the notification



424
425
426
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 424

def default_notification_key
  "#{to_resource_name}.default"
end

#default_url_optionsHash

Returns default_url_options for polymorphic_path.

Returns:

  • (Hash)

    Rails.application.routes.default_url_options



38
39
40
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 38

def default_url_options
  Rails.application.routes.default_url_options
end

#instance_subscription_targets(target_type, key = nil) ⇒ Array<Object>

Returns targets that have instance-level subscriptions for this notifiable. This method finds all active instance-level subscriptions for this specific notifiable instance and returns their target objects.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification (defaults to default_notification_key)

Returns:

  • (Array<Object>)

    Array of target instances with active instance-level subscriptions



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 96

def instance_subscription_targets(target_type, key = nil)
  key ||= default_notification_key
  target_class_name = target_type.to_s.to_model_name
  if ActivityNotification.config.orm == :dynamoid
    # :nocov:
    delimiter = ActivityNotification.config.composite_key_delimiter
    Subscription.where(
      notifiable_key: "#{self.class.name}#{delimiter}#{self.id}",
      key:            key,
      subscribing:    true
    ).select { |s| s.target_type == target_class_name }.map(&:target).compact
    # :nocov:
  else
    # :nocov:
    Subscription.where(
      notifiable_type: self.class.name,
      notifiable_id:   self.id,
      key:             key,
      subscribing:     true,
      target_type:     target_class_name
    ).map(&:target).compact
    # :nocov:
  end
end

#notifiable_action_cable_allowed?(target, key = nil) ⇒ Boolean

Returns if publishing WebSocket using ActionCable is allowed for the notifiable from configured field or overridden method. This method can be overridden.

Parameters:

  • target (Object)

    Target instance to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Boolean)

    If publishing WebSocket using ActionCable is allowed for the notifiable



197
198
199
200
201
202
203
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 197

def notifiable_action_cable_allowed?(target, key = nil)
  resolve_parameter(
    "notifiable_action_cable_allowed_for_#{cast_to_resources_name(target.class)}?",
    _notifiable_action_cable_allowed[cast_to_resources_sym(target.class)],
    ActivityNotification.config.action_cable_enabled,
    target, key)
end

#notifiable_action_cable_api_allowed?(target, key = nil) ⇒ Boolean

Returns if publishing WebSocket API using ActionCable is allowed for the notifiable from configured field or overridden method. This method can be overridden.

Parameters:

  • target (Object)

    Target instance to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Boolean)

    If publishing WebSocket API using ActionCable is allowed for the notifiable



211
212
213
214
215
216
217
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 211

def notifiable_action_cable_api_allowed?(target, key = nil)
  resolve_parameter(
    "notifiable_action_cable_api_allowed_for_#{cast_to_resources_name(target.class)}?",
    _notifiable_action_cable_api_allowed[cast_to_resources_sym(target.class)],
    ActivityNotification.config.action_cable_api_enabled,
    target, key)
end

#notifiable_path(target_type, key = nil) ⇒ String

Returns notifiable_path to move after opening notification from configured field or overridden method. This method can be overridden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (String)

    Notifiable path URL to move after opening notification



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 225

def notifiable_path(target_type, key = nil)
  resolved_parameter = resolve_parameter(
    "notifiable_path_for_#{cast_to_resources_name(target_type)}",
    _notifiable_path[cast_to_resources_sym(target_type)],
    nil,
    key)
  unless resolved_parameter
    begin
      resolved_parameter = defined?(super) ? super : polymorphic_path(self)
    rescue NoMethodError, ActionController::UrlGenerationError
      raise NotImplementedError, "You have to implement #{self.class}##{__method__}, "\
                                 "set :notifiable_path in acts_as_notifiable or "\
                                 "set polymorphic_path routing for #{self.class}"
    end
  end
  resolved_parameter
end

#notification_email_allowed?(target, key = nil) ⇒ Boolean

Returns if sending notification email is allowed for the notifiable from configured field or overridden method. This method can be overridden.

Parameters:

  • target (Object)

    Target instance to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Boolean)

    If sending notification email is allowed for the notifiable



183
184
185
186
187
188
189
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 183

def notification_email_allowed?(target, key = nil)
  resolve_parameter(
    "notification_email_allowed_for_#{cast_to_resources_name(target.class)}?",
    _notification_email_allowed[cast_to_resources_sym(target.class)],
    ActivityNotification.config.email_enabled,
    target, key)
end

#notification_group(target_type, key = nil) ⇒ Object

Returns group unit of the notifications from configured field or overridden method. This method can be overridden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Object)

    Group unit of the notifications



127
128
129
130
131
132
133
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 127

def notification_group(target_type, key = nil)
  resolve_parameter(
    "notification_group_for_#{cast_to_resources_name(target_type)}",
    _notification_group[cast_to_resources_sym(target_type)],
    nil,
    key)
end

#notification_group_expiry_delay(target_type, key = nil) ⇒ Object

Returns group expiry period of the notifications from configured field or overridden method. This method can be overridden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Object)

    Group expiry period of the notifications



141
142
143
144
145
146
147
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 141

def notification_group_expiry_delay(target_type, key = nil)
  resolve_parameter(
    "notification_group_expiry_delay_for_#{cast_to_resources_name(target_type)}",
    _notification_group_expiry_delay[cast_to_resources_sym(target_type)],
    nil,
    key)
end

#notification_key_for_tracked_creationString

Returns key of the notification for tracked notifiable creation. This method can be overridden. "#to_resource_name.create" is defined as default creation key.

Returns:

  • (String)

    Key of the notification for tracked notifiable creation



433
434
435
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 433

def notification_key_for_tracked_creation
  "#{to_resource_name}.create"
end

#notification_key_for_tracked_updateString

Returns key of the notification for tracked notifiable update. This method can be overridden. "#to_resource_name.update" is defined as default update key.

Returns:

  • (String)

    Key of the notification for tracked notifiable update



442
443
444
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 442

def notification_key_for_tracked_update
  "#{to_resource_name}.update"
end

#notification_parameters(target_type, key = nil) ⇒ Hash

Returns additional notification parameters from configured field or overridden method. This method can be overridden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Hash)

    Additional notification parameters



155
156
157
158
159
160
161
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 155

def notification_parameters(target_type, key = nil)
  resolve_parameter(
    "notification_parameters_for_#{cast_to_resources_name(target_type)}",
    _notification_parameters[cast_to_resources_sym(target_type)],
    {},
    key)
end

#notification_targets(target_type, options = {}) ⇒ Array<Notification> | ActiveRecord_AssociationRelation<Notification>

Returns notification targets from configured field or overridden method. This method can be overridden.

Parameters:

  • target_type (String)

    Target type to notify

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

    Options for notifications

Options Hash (options):

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

    Key of the notification

  • :parameters (Hash) — default: {}

    Additional parameters of the notifications

Returns:

  • (Array<Notification> | ActiveRecord_AssociationRelation<Notification>)

    Array or database query of the notification targets



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 75

def notification_targets(target_type, options = {})
  target_typed_method_name = "notification_#{cast_to_resources_name(target_type)}"
  resolved_parameter = resolve_parameter(
    target_typed_method_name,
    _notification_targets[cast_to_resources_sym(target_type)],
    nil,
    options)
  unless resolved_parameter
    raise NotImplementedError, "You have to implement #{self.class}##{target_typed_method_name} "\
                               "or set :targets in acts_as_notifiable"
  end
  resolved_parameter
end

#notifier(target_type, key = nil) ⇒ Object

Returns notifier of the notification from configured field or overridden method. This method can be overridden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Object)

    Notifier of the notification



169
170
171
172
173
174
175
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 169

def notifier(target_type, key = nil)
  resolve_parameter(
    "notifier_for_#{cast_to_resources_name(target_type)}",
    _notifier[cast_to_resources_sym(target_type)],
    nil,
    key)
end

#notify(target_type, options = {}) ⇒ Array<Notification> Also known as: notify_now

Generates notifications to configured targets with notifiable model. This method calls NotificationApi#notify internally with self notifiable instance.

Parameters:

  • target_type (Symbol, String, Class)

    Type of target

  • 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:

  • (Array<Notification>)

    Array of generated notifications

See Also:

  • NotificationApi#notify


312
313
314
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 312

def notify(target_type, options = {})
  Notification.notify(target_type, self, options)
end

#notify_all(targets, options = {}) ⇒ Array<Notification> Also known as: notify_all_now

Generates notifications to one target. This method calls NotificationApi#notify_all internally with self notifiable instance.

Parameters:

  • targets (Array<Object>)

    Targets to send notifications

  • 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:

  • (Array<Notification>)

    Array of generated notifications

See Also:

  • NotificationApi#notify_all


353
354
355
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 353

def notify_all(targets, options = {})
  Notification.notify_all(targets, self, options)
end

#notify_all_later(targets, options = {}) ⇒ Array<Notification>

Generates notifications to one target later by ActiveJob queue. This method calls NotificationApi#notify_all_later internally with self notifiable instance.

Parameters:

  • targets (Array<Object>)

    Targets to send notifications

  • 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:

  • (Array<Notification>)

    Array of generated notifications

See Also:

  • NotificationApi#notify_all_later


374
375
376
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 374

def notify_all_later(targets, options = {})
  Notification.notify_all_later(targets, self, options)
end

#notify_later(target_type, options = {}) ⇒ Array<Notification>

Generates notifications to configured targets with notifiable model later by ActiveJob queue. This method calls NotificationApi#notify_later internally with self notifiable instance.

Parameters:

  • target_type (Symbol, String, Class)

    Type of target

  • 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:

  • (Array<Notification>)

    Array of generated notifications

See Also:

  • NotificationApi#notify_later


332
333
334
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 332

def notify_later(target_type, options = {})
  Notification.notify_later(target_type, self, options)
end

#notify_later_to(target, options = {}) ⇒ Notification

Generates notifications to one target later by ActiveJob queue. This method calls NotificationApi#notify_later_to internally with self notifiable instance.

Parameters:

  • target (Object)

    Target to send notifications

  • 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:

  • NotificationApi#notify_later_to


415
416
417
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 415

def notify_later_to(target, options = {})
  Notification.notify_later_to(target, self, options)
end

#notify_to(target, options = {}) ⇒ Notification Also known as: notify_now_to

Generates notifications to one target. This method calls NotificationApi#notify_to internally with self notifiable instance.

Parameters:

  • target (Object)

    Target to send notifications

  • 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:

  • NotificationApi#notify_to


394
395
396
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 394

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

#optional_target_names(target_type, key = nil) ⇒ Array<Symbol>

Returns optional_target names of the notification from configured field or overridden method. This method can be overridden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Array<Symbol>)

    Array of optional target names



273
274
275
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 273

def optional_target_names(target_type, key = nil)
  optional_targets(target_type, key).map { |optional_target| optional_target.to_optional_target_name }
end

#optional_targets(target_type, key = nil) ⇒ Array<ActivityNotification::OptionalTarget::Base>

Returns optional_targets of the notification from configured field or overridden method. This method can be overridden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:



259
260
261
262
263
264
265
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 259

def optional_targets(target_type, key = nil)
  resolve_parameter(
    "optional_targets_for_#{cast_to_resources_name(target_type)}",
    _optional_targets[cast_to_resources_sym(target_type)],
    [],
    key)
end

#printable_notifiable_name(target, key = nil) ⇒ String

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

Returns:

  • (String)

    Printable notifiable model name



245
246
247
248
249
250
251
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 245

def printable_notifiable_name(target, key = nil)
  resolve_parameter(
    "printable_notifiable_name_for_#{cast_to_resources_name(target.class)}?",
    _printable_notifiable_name[cast_to_resources_sym(target.class)],
    printable_name,
    target, key)
end