Class: ActiveNotify::Base
- Inherits:
-
Object
- Object
- ActiveNotify::Base
- Includes:
- Callbacks
- Defined in:
- lib/active_notify/base.rb
Overview
Active Notify Base
Provides a base class for defining notifiers that dispatch a single notification across multiple carriers (email, SMS, push, etc.) from a single place.
A notifier declares the carriers it delivers through using .deliver_via and is invoked using .deliver_now or .deliver_later. Parameters that need to reach every carrier can be passed with .with.
A minimal implementation could be:
class CommentNotifier < ActiveNotify::Base
deliver_via :email
deliver_via :sms
end
CommentNotifier.with(user: user).deliver_later
Each call to deliver_via resolves a carrier class (by convention, CommentNotifier::Email and CommentNotifier::Sms) that inherits from ActiveNotify::Carrier and handles the actual delivery. The carrier can be overridden with the :class_name option:
class CommentNotifier < ActiveNotify::Base
deliver_via :email, class_name: "CustomEmailCarrier"
end
Conditional delivery
Each carrier can be conditionally skipped with :if or :unless. The condition may be a symbol (method name), a proc, or a literal value:
class CommentNotifier < ActiveNotify::Base
deliver_via :email, if: :email_subscribed?
deliver_via :sms, unless: ->(notifier) { notifier.params[:silent] }
def email_subscribed?
params[:user].email_subscribed?
end
end
Default arguments
Any options other than :class_name, :if, and :unless are passed through to the carrier’s deliver_later method as default arguments. Procs and symbols are evaluated against the notifier instance:
class CommentNotifier < ActiveNotify::Base
deliver_via :email, queue: :urgent, wait: ->(notifier) { notifier.params[:delay] }
end
Subclasses inherit the carriers of their parent, and can add or override carriers by calling deliver_via again.
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
The params passed to .with (or
new).
Class Method Summary collapse
-
.deliver_later ⇒ Object
Enqueues the notification for background delivery through every registered carrier.
-
.deliver_now ⇒ Object
Delivers the notification synchronously through every registered carrier.
-
.deliver_via(carrier_name, options = {}) ⇒ Object
Registers a carrier that this notifier delivers through.
-
.with(params) ⇒ Object
Builds a notifier instance with the given
paramsso that they can be accessed from carriers and callbacks viaparams.
Instance Method Summary collapse
-
#deliver_later(args = {}) ⇒ Object
Enqueues the notification for background delivery through every registered carrier.
-
#deliver_now ⇒ Object
Delivers the notification synchronously through every registered carrier, running
before_delivery,around_delivery, andafter_deliverycallbacks as well as per-carrier callbacks. -
#initialize(params = {}) ⇒ Base
constructor
Initializes a new notifier with the given
params.
Constructor Details
Instance Attribute Details
#params ⇒ Object (readonly)
The params passed to .with (or new). Available to callbacks and carriers via the params accessor.
121 122 123 |
# File 'lib/active_notify/base.rb', line 121 def params @params end |
Class Method Details
.deliver_later ⇒ Object
Enqueues the notification for background delivery through every registered carrier. Any arguments are forwarded to the carrier’s deliver_later method and override per-carrier defaults declared with .deliver_via.
CommentNotifier.deliver_later
CommentNotifier.deliver_later(wait: 5.minutes, queue: :low)
106 107 108 |
# File 'lib/active_notify/base.rb', line 106 def deliver_later(...) new.deliver_later(...) end |
.deliver_now ⇒ Object
Delivers the notification synchronously through every registered carrier. Equivalent to new.deliver_now.
CommentNotifier.deliver_now
95 96 97 |
# File 'lib/active_notify/base.rb', line 95 def deliver_now new.deliver_now end |
.deliver_via(carrier_name, options = {}) ⇒ Object
Registers a carrier that this notifier delivers through.
The carrier_name is a symbol that identifies the carrier and is used to resolve the carrier class by convention (e.g. :email resolves to Email under the notifier’s namespace). Pass :class_name to override the default resolution.
The :if and :unless options are used to conditionally skip the carrier; any remaining options are forwarded to the carrier’s deliver_later method as default arguments.
class CommentNotifier < ActiveNotify::Base
deliver_via :email
deliver_via :sms, class_name: "TwilioCarrier", if: :phone_verified?
deliver_via :push, queue: :notifications
end
86 87 88 89 |
# File 'lib/active_notify/base.rb', line 86 def deliver_via(carrier_name, = {}) self.carriers = carriers.merge(carrier_name => CarrierDescriptor.new(carrier_name, self, )) define_carrier_callbacks(carrier_name) end |
.with(params) ⇒ Object
Builds a notifier instance with the given params so that they can be accessed from carriers and callbacks via params.
CommentNotifier.with(user: user, comment: comment).deliver_later
114 115 116 |
# File 'lib/active_notify/base.rb', line 114 def with(params) new(params) end |
Instance Method Details
#deliver_later(args = {}) ⇒ Object
Enqueues the notification for background delivery through every registered carrier. args are merged on top of each carrier’s default arguments declared with deliver_via and passed to the carrier’s deliver_later method.
142 143 144 145 146 |
# File 'lib/active_notify/base.rb', line 142 def deliver_later(args = {}) perform_deliveries do |instance, descriptor| instance.deliver_later(descriptor.args(self).merge(args)) end end |
#deliver_now ⇒ Object
Delivers the notification synchronously through every registered carrier, running before_delivery, around_delivery, and after_delivery callbacks as well as per-carrier callbacks.
132 133 134 135 136 |
# File 'lib/active_notify/base.rb', line 132 def deliver_now perform_deliveries do |instance| instance.deliver_now end end |