Class: Motor::AlertsMailer

Inherits:
ApplicationMailer show all
Defined in:
app/mailers/motor/alerts_mailer.rb

Constant Summary collapse

SenderAddressNotSet =
Class.new(StandardError)
SENDER_NOT_SET_ERROR_MESSAGE =
'Please specify your sender address via MOTOR_ALERTS_FROM_ADDRESS environment variable'

Instance Method Summary collapse

Instance Method Details

#alert_email(alert) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/mailers/motor/alerts_mailer.rb', line 9

def alert_email(alert)
  @alert = alert
  @query_result = Queries::RunQuery.call(alert.query)

  return if @alert.preferences[:send_empty].blank? && @query_result.data.blank?

  assign_attachment(@alert, @query_result)

  raise SenderAddressNotSet, SENDER_NOT_SET_ERROR_MESSAGE unless from_address

  mail(
    from: from_address,
    to: alert.to_emails,
    subject: alert.name.presence || @alert.query.name
  )
end