Class: ActiveDelivery::Base
- Inherits:
-
Object
- Object
- ActiveDelivery::Base
- Includes:
- Callbacks, TestDelivery
- Defined in:
- lib/active_delivery/base.rb
Overview
Base class for deliveries.
Delivery object describes how to notify a user about an event (e.g. via email or via push notification or both).
Delivery class acts like a proxy in front of the different delivery channels (i.e. mailers, notifiers). That means that calling a method on delivery class invokes the same method on the corresponding class, e.g.:
EventsDelivery.notify(:one_hour_before, profile, event)
# under the hood it calls
EventsMailer.one_hour_before(profile, event).deliver_later
# and
EventsNotifier.one_hour_before(profile, event).notify_later
Delivery also supports parameterized calling:
EventsDelivery.with(profile: profile).notify(:canceled, event)
The parameters could be accessed through ‘params` instance method (e.g. to implement guard-like logic).
When params are presents the parametrized mailer is used, i.e.:
EventsMailer.with(profile: profile).canceled(event)
See api.rubyonrails.org/classes/ActionMailer/Parameterized.html
Constant Summary
Constants included from Callbacks
Callbacks::CALLBACK_TERMINATOR
Class Attribute Summary collapse
-
.abstract_class ⇒ Object
Returns the value of attribute abstract_class.
Instance Attribute Summary collapse
-
#notification_name ⇒ Object
readonly
Returns the value of attribute notification_name.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
- .abstract_class? ⇒ Boolean
- .delivery_lines ⇒ Object
-
.notify(*args, **kwargs) ⇒ Object
Enqueues delivery (i.e. uses #deliver_later for mailers).
-
.notify!(mid, *args, **hargs) ⇒ Object
The same as .notify but delivers synchronously (i.e. #deliver_now for mailers).
- .register_line(line_id, line_class, **options) ⇒ Object
- .unregister_line(line_id) ⇒ Object
Instance Method Summary collapse
-
#initialize(**params) ⇒ Base
constructor
A new instance of Base.
-
#notify(mid, *args, **kwargs) ⇒ Object
Enqueues delivery (i.e. uses #deliver_later for mailers).
-
#notify!(mid, *args, **hargs) ⇒ Object
The same as .notify but delivers synchronously (i.e. #deliver_now for mailers).
Methods included from TestDelivery
clear, enable, enabled?, store, #test?, track
Constructor Details
#initialize(**params) ⇒ Base
Returns a new instance of Base.
92 93 94 95 |
# File 'lib/active_delivery/base.rb', line 92 def initialize(**params) @params = params @params.freeze end |
Class Attribute Details
.abstract_class ⇒ Object
Returns the value of attribute abstract_class.
35 36 37 |
# File 'lib/active_delivery/base.rb', line 35 def abstract_class @abstract_class end |
Instance Attribute Details
#notification_name ⇒ Object (readonly)
Returns the value of attribute notification_name.
90 91 92 |
# File 'lib/active_delivery/base.rb', line 90 def notification_name @notification_name end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
90 91 92 |
# File 'lib/active_delivery/base.rb', line 90 def params @params end |
Class Method Details
.abstract_class? ⇒ Boolean
85 86 87 |
# File 'lib/active_delivery/base.rb', line 85 def abstract_class? abstract_class == true end |
.delivery_lines ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_delivery/base.rb', line 50 def delivery_lines @lines ||= begin if superclass.respond_to?(:delivery_lines) superclass.delivery_lines.each_with_object({}) do |(key, val), acc| acc[key] = val.dup_for(self) end else {} end end end |
.notify(*args, **kwargs) ⇒ Object
Enqueues delivery (i.e. uses #deliver_later for mailers)
40 41 42 |
# File 'lib/active_delivery/base.rb', line 40 def notify(*args, **kwargs) new.notify(*args, **kwargs) end |
.notify!(mid, *args, **hargs) ⇒ Object
The same as .notify but delivers synchronously (i.e. #deliver_now for mailers)
46 47 48 |
# File 'lib/active_delivery/base.rb', line 46 def notify!(mid, *args, **hargs) notify(mid, *args, **hargs, sync: true) end |
.register_line(line_id, line_class, **options) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/active_delivery/base.rb', line 62 def register_line(line_id, line_class, **) delivery_lines[line_id] = line_class.new(id: line_id, owner: self, **) instance_eval <<~CODE, __FILE__, __LINE__ + 1 def #{line_id}(val) delivery_lines[:#{line_id}].handler_class = val end def #{line_id}_class delivery_lines[:#{line_id}].handler_class end CODE end |
.unregister_line(line_id) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/active_delivery/base.rb', line 76 def unregister_line(line_id) removed_line = delivery_lines.delete(line_id) return if removed_line.nil? singleton_class.undef_method line_id singleton_class.undef_method "#{line_id}_class" end |
Instance Method Details
#notify(mid, *args, **kwargs) ⇒ Object
Enqueues delivery (i.e. uses #deliver_later for mailers)
98 99 100 101 |
# File 'lib/active_delivery/base.rb', line 98 def notify(mid, *args, **kwargs) @notification_name = mid do_notify(*args, **kwargs) end |
#notify!(mid, *args, **hargs) ⇒ Object
The same as .notify but delivers synchronously (i.e. #deliver_now for mailers)
105 106 107 |
# File 'lib/active_delivery/base.rb', line 105 def notify!(mid, *args, **hargs) notify(mid, *args, **hargs, sync: true) end |