Module: ActivityNotification::ActsAsNotifiable

Extended by:
ActiveSupport::Concern
Included in:
Models
Defined in:
lib/activity_notification/roles/acts_as_notifiable.rb

Overview

Manages to add all required configurations to notifiable models.

Class Method Summary collapse

Class Method Details

.acts_as_notifiable(target_type, options = {}) ⇒ Hash

Adds required configurations to notifiable models.

Parameters:

  • :targets

    • Targets to send notifications. It it set as ActiveRecord records or array of models. This is a only necessary option. If you do not specify this option, you have to override notification_targets or notification_[plural target type] (e.g. notification_users) method.

  • :group

    • Group unit of notifications. Notifications will be bundled by this group (and target, notifiable_type, key). This parameter is a optional.

  • :notifier

    • Notifier of the notification. This will be stored as notifier with notification record. This parameter is a optional.

  • :parameters

    • Additional parameters of the notifications. This will be stored as parameters with notification record. You can use these additional parameters in your notification view or i18n text. This parameter is a optional.

  • :email_allowed

    • Whether activity_notification sends notification email. Specified method or symbol is expected to return true (not nil) or false (nil). This parameter is a optional since default value is false. To use notification email, email_allowed option must return true (not nil) in both of notifiable and target model. This can be also configured default option in initializer.

  • :notifiable_path

    • Path to redirect from open or move action of notification controller. You can also use this notifiable_path as notifiable link in notification view. This parameter is a optional since polymorphic_path is used as default value.

  • :printable_name or :printable_notifiable_name

    • Printable notifiable name. This parameter is a optional since ActivityNotification::Common.printable_name is used as default value. :printable_name is the same option as :printable_notifiable_name

  • :dependent_notifications

    • Dependency for notifications to delete generated notifications with this notifiable. This option is used to configure generated_notifications_as_notifiable association. You can use :delete_all, :destroy, or :nullify for this option. This parameter is a optional since no dependent option is used as default.

Examples:

Notify to all users

class Comment < ActiveRecord::Base
  acts_as_notifiable :users, targets: User.all
end

Notify to author and users commented to the article, except comment owner self

# app/models/comment.rb
class Comment < ActiveRecord::Base
  belongs_to :article
  belongs_to :user
  acts_as_notifiable :users,
    targets: ->(comment, key) {
      ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
    }
end

All unopened notifications to the same target will be grouped by article

# app/models/comment.rb
class Comment < ActiveRecord::Base
  belongs_to :article
  acts_as_notifiable :users, targets: User.all, group: :article
end

Set comment owner self as notifier

# app/models/comment.rb
class Comment < ActiveRecord::Base
  belongs_to :article
  belongs_to :user
  acts_as_notifiable :users, targets: User.all, notifier: :user
end

Set constant values as additional parameter

# app/models/comment.rb
class Comment < ActiveRecord::Base
  acts_as_notifiable :users, targets: User.all, parameters: { default_param: '1' }
end

Set comment body as additional parameter

# app/models/comment.rb
class Comment < ActiveRecord::Base
  acts_as_notifiable :users, targets: User.all, parameters: ->(comment, key) { body: comment.body }
end

Enable email notification for this notifiable model

# app/models/comment.rb
class Comment < ActiveRecord::Base
  acts_as_notifiable :users, targets: User.all, email_allowed: true
end

Redirect to parent article page from comment notifications

# app/models/comment.rb
class Comment < ActiveRecord::Base
  belongs_to :article
  acts_as_notifiable :users, targets: User.all, notifiable_path: :article_notifiable_path

  def article_notifiable_path
    article_path(article)
  end
end

Define printable name with comment body

# app/models/comment.rb
class Comment < ActiveRecord::Base
  acts_as_notifiable :users, targets: User.all, printable_name: ->(comment) { "comment \"#{comment.body}\"" }
end

Define :delete_all dependency to generated notifications

# app/models/comment.rb
class Comment < ActiveRecord::Base
  acts_as_notifiable :users, targets: User.all, dependent_notifications: :delete_all
end

Parameters:

  • target_type (Symbol)

    Type of notification target as symbol

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

    Options for notifiable model configuration

Options Hash (options):

  • :targets (Symbol, Proc, Array) — default: nil

    Targets to send notifications

  • :group (Symbol, Proc, Object) — default: nil

    Group unit of the notifications

  • :notifier (Symbol, Proc, Object) — default: nil

    Notifier of the notifications

  • :parameters (Symbol, Proc, Hash) — default: {}

    Additional parameters of the notifications

  • :email_allowed (Symbol, Proc, Boolean) — default: ActivityNotification.config.email_enabled

    Whether activity_notification sends notification email

  • :notifiable_path (Symbol, Proc, String) — default: polymorphic_path(self)

    Path to redirect from open or move action of notification controller

  • :printable_name (Symbol, Proc, String) — default: ActivityNotification::Common.printable_name

    Printable notifiable name

  • :dependent_notifications (Symbol, Proc) — default: nil

    Dependency for notifications to delete generated notifications with this notifiable

Returns:

  • (Hash)

    Configured parameters as notifiable model



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/activity_notification/roles/acts_as_notifiable.rb', line 129

def acts_as_notifiable(target_type, options = {})
  include Notifiable

  if [:delete_all, :destroy, :nullify].include? options[:dependent_notifications] 
    has_many :generated_notifications_as_notifiable,
      class_name: "::ActivityNotification::Notification",
      as: :notifiable,
      dependent: options[:dependent_notifications]
  end

  options[:printable_notifiable_name] ||= options.delete(:printable_name)
  set_acts_as_parameters_for_target(target_type, [:targets, :group, :parameters, :email_allowed], options, "notification_")
    .merge set_acts_as_parameters_for_target(target_type, [:notifier, :notifiable_path, :printable_notifiable_name], options)
end

.available_notifiable_optionsArray<Symbol>

Returns array of available notifiable options in acts_as_notifiable.

Returns:

  • (Array<Symbol>)

    Array of available notifiable options



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/activity_notification/roles/acts_as_notifiable.rb', line 146

def available_notifiable_options
  [ :targets,
    :group,
    :notifier,
    :parameters,
    :email_allowed,
    :notifiable_path,
    :printable_notifiable_name, :printable_name,
    :dependent_notifications
  ].freeze
end