Module: ActiveNotify::Callbacks

Extended by:
ActiveSupport::Concern
Includes:
ActiveSupport::Callbacks
Included in:
Base
Defined in:
lib/active_notify/callbacks.rb

Overview

Active Notify Callbacks

Provides a callback mechanism around notification delivery, mixed in automatically by ActiveNotify::Base. Callbacks can be registered to wrap the full delivery (all carriers) or a single carrier at a time.

Three kinds of callbacks are available: before_delivery, after_delivery, and around_delivery. Each accepts one or more method names, a block, or both, along with an optional :on target to scope the callback to a specific carrier.

class CommentNotifier < ActiveNotify::Base
  deliver_via :email
  deliver_via :sms

  before_delivery :log_delivery
  after_delivery  :record_metrics

  before_delivery :ensure_email_verified, on: :email
  around_delivery :with_timeout,          on: :sms

  private

  def log_delivery
    Rails.logger.info("Delivering #{self.class.name}")
  end
end

Global vs per-carrier callbacks

A callback without :on runs once per delivery, wrapping the whole set of carriers. A callback with :on runs only when that specific carrier is about to deliver. Per-carrier callbacks are skipped if the carrier is skipped by its :if or :unless condition.

Skipping callbacks in subclasses

Inherited callbacks can be removed with skip_before_delivery, skip_after_delivery, and skip_around_delivery:

class QuietNotifier < CommentNotifier
  skip_before_delivery :log_delivery
  skip_before_delivery :ensure_email_verified, on: :email
end