Class: ActiveDelivery::Base

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TestDelivery

clear, enable, enabled?, store, #test?, track

Constructor Details

#initialize(**params) ⇒ Base

Returns a new instance of Base.



83
84
85
86
# File 'lib/active_delivery/base.rb', line 83

def initialize(**params)
  @params = params
  @params.freeze
end

Class Attribute Details

.abstract_classObject

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_nameObject (readonly)

Returns the value of attribute notification_name.



81
82
83
# File 'lib/active_delivery/base.rb', line 81

def notification_name
  @notification_name
end

#paramsObject (readonly)

Returns the value of attribute params.



81
82
83
# File 'lib/active_delivery/base.rb', line 81

def params
  @params
end

Class Method Details

.abstract_class?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/active_delivery/base.rb', line 76

def abstract_class?
  abstract_class == true
end

.delivery_linesObject



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) ⇒ Object

Enqueues delivery (i.e. uses #deliver_later for mailers)



40
41
42
# File 'lib/active_delivery/base.rb', line 40

def notify(*args)
  new.notify(*args)
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, **options)
  delivery_lines[line_id] = line_class.new(id: line_id, owner: self, **options)

  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

Instance Method Details

#notify(mid, *args) ⇒ Object

Enqueues delivery (i.e. uses #deliver_later for mailers)



89
90
91
92
# File 'lib/active_delivery/base.rb', line 89

def notify(mid, *args)
  @notification_name = mid
  do_notify(*args)
end

#notify!(mid, *args, **hargs) ⇒ Object

The same as .notify but delivers synchronously (i.e. #deliver_now for mailers)



96
97
98
# File 'lib/active_delivery/base.rb', line 96

def notify!(mid, *args, **hargs)
  notify(mid, *args, **hargs, sync: true)
end