Module: ActivityNotification::Notifiable

Extended by:
ActiveSupport::Concern
Includes:
ActionDispatch::Routing::PolymorphicRoutes, Common, 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.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#printable_name, #printable_type, #resolve_value, #to_class_name, #to_resource_name, #to_resources_name

Class Method Details

.available_as_notifiable?Boolean

Checks if the model includes notifiable and notifiable methods are available.

Returns:

  • (Boolean)

    Always true



40
41
42
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 40

def available_as_notifiable?
  true
end

.set_notifiable_class_defaultsNilClass

Sets default values to notifiable class fields.

Returns:

  • (NilClass)

    nil



46
47
48
49
50
51
52
53
54
55
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 46

def set_notifiable_class_defaults
  self._notification_targets        = {}
  self._notification_group          = {}
  self._notifier                    = {}
  self._notification_parameters     = {}
  self._notification_email_allowed  = {}
  self._notifiable_path             = {}
  self._printable_notifiable_name   = {}
  nil
end

Instance Method Details

#default_notification_keyString

Returns default key of the notification. This method is able to be overriden. “#Common#to_resource_name.default” is defined as default key.

Returns:

  • (String)

    Default Key of the notification



226
227
228
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 226

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



33
34
35
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 33

def default_url_options
  Rails.application.routes.default_url_options
end

#generated_notifications_as_notifiableArray<Notificaion>

Has many notification instances for this notifiable. Dependency for these notifications can be overriden from acts_as_notifiable.

Returns:

  • (Array<Notificaion>)

    Array or database query of notifications for this notifiable



17
18
19
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 17

has_many :generated_notifications_as_notifiable,
class_name: "::ActivityNotification::Notification",
as: :notifiable

#notifiable_path(target_type, key = nil) ⇒ String

Returns notifiable_path to move after opening notification from configured field or overriden method. This method is able to be overriden.

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 136

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 = 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 overriden method. This method is able to be overriden.

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



122
123
124
125
126
127
128
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 122

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 owner of the notification from configured field or overriden method. This method is able to be overriden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Object)

    Group owner of the notification



83
84
85
86
87
88
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 83

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_parameters(target_type, key = nil) ⇒ Hash

Returns additional notification parameters from configured field or overriden method. This method is able to be overriden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Hash)

    Additional notification parameters



96
97
98
99
100
101
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 96

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, key = nil) ⇒ Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>

Returns notification targets from configured field or overriden method. This method is able to be overriden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

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

    Array or database query of the notification targets



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 64

def notification_targets(target_type, key = nil)
  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, key)
  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 overriden method. This method is able to be overriden.

Parameters:

  • target_type (String)

    Target type to notify

  • key (String) (defaults to: nil)

    Key of the notification

Returns:

  • (Object)

    Notifier of the notification



109
110
111
112
113
114
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 109

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<Notificaion>

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

  • :notifier (Object) — default: nil

    Notifier of the notifications

  • :parameters (Hash) — default: {}

    Additional parameters of the notifications

  • :send_email (Boolean) — default: true

    If it sends notification email

  • :send_later (Boolean) — default: true

    If it sends notification email asynchronously

Returns:

  • (Array<Notificaion>)

    Array of generated notifications

See Also:



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

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

#notify_all(targets, options = {}) ⇒ Array<Notificaion>

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

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

Returns:

  • (Array<Notificaion>)

    Array of generated notifications

See Also:



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

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

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

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

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

Returns:

See Also:



217
218
219
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 217

def notify_to(target, options = {})
  Notification.notify_to(target, self, options)
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



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

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