Class: Postburner::Mailer

Inherits:
Job show all
Defined in:
app/models/postburner/mailer.rb

Overview

Send a mailer, tracked.

Postburner::Mailer.deliver(UserMailer, :welcome).with(user_id: 1) Postburner::Mailer.deliver(UserMailer, :welcome).with(user_id: 1).queue! at: Time.zone.now + 1.day Postburner::Mailer.deliver(UserMailer, :welcome).with(user_id: 1).queue! delay: 5.minutes

Constant Summary

Constants inherited from Job

Job::LOG_LEVELS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Job

#beanstalk_job, #beanstalk_job!, #delete!, #elapsed_ms, #intended_at, #kick!, #log, #log!, #log_exception, #log_exception!, perform, #perform!, #queue!, #remove!, #requeue!, #will_insert?

Class Method Details

.delivery(mailer, action) ⇒ Object

queue ‘mailers’



10
11
12
13
14
15
16
17
18
# File 'app/models/postburner/mailer.rb', line 10

def self.delivery(mailer, action)
  job = self.new(
    args: {
      mailer: mailer.to_s,
      action: action.to_s,
    }
  )
  job
end

.delivery!(mailer, action) ⇒ Object



20
21
22
23
24
# File 'app/models/postburner/mailer.rb', line 20

def self.delivery!(mailer, action)
  job = self.delivery(mailer, action)
  job.save!
  job
end

Instance Method Details

#actionObject

Get the mailer action as a symbol.



58
59
60
# File 'app/models/postburner/mailer.rb', line 58

def action
  self.args['action']&.to_sym
end

#assembleObject

Build the mail but don’t send.

Optional ‘args` argument for testing convenience.



45
46
47
48
# File 'app/models/postburner/mailer.rb', line 45

def assemble
  mail = self.mailer.with(self.params).send(self.action)
  mail
end

#mailerObject

Get the mailer class.



52
53
54
# File 'app/models/postburner/mailer.rb', line 52

def mailer
  self.args['mailer'].constantize
end

#paramsObject

Get the deserialized params.



64
65
66
# File 'app/models/postburner/mailer.rb', line 64

def params
  ActiveJob::Arguments.deserialize(self.args['params']).to_h
end

#perform(args) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'app/models/postburner/mailer.rb', line 68

def perform(args)
  self.log! "Building"
  mail = self.assemble

  self.log! "Delivering"
  mail.deliver_now

  self.log! "Delivered"
end

#with(params = {}) ⇒ Object

Similar to ActionMailer #with - set the parameters



28
29
30
31
32
33
# File 'app/models/postburner/mailer.rb', line 28

def with(params={})
  self.args.merge!(
    'params' => ActiveJob::Arguments.serialize(params)
  )
  self
end

#with!(params = {}) ⇒ Object



35
36
37
38
39
# File 'app/models/postburner/mailer.rb', line 35

def with!(params={})
  self.with(params)
  self.save!
  self
end