Sha256: a448c234d454c0782ac53303b7f6aef09cf4dc750cbbbe3cd6c4402acf2339bd

Contents?: true

Size: 1.4 KB

Versions: 53

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Motor
  class AlertsMailer < ApplicationMailer
    def alert_email(alert)
      @alert = alert
      @query_result = Queries::RunQuery.call(alert.query)

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

      attachments["#{alert.name.presence || 'data'}.csv"] = generate_csv(@query_result)

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

    private

    def generate_csv(_query_result)
      rows = [@query_result.columns.pluck(:name)] + @query_result.data

      rows.map(&:to_csv).join
    end

    def from_address
      from = ENV['MOTOR_ALERTS_FROM_ADDRESS'].presence

      from ||= application_mailer_default_from
      from ||= mailer_config_from_address
      from ||= "reports@#{ENV['HOST'].delete_prefix('www.')}" if ENV['HOST'].present?

      from || 'reports@example.com'
    end

    def application_mailer_default_from
      return if !defined?(::ApplicationMailer) || ::ApplicationMailer.default[:from].to_s.include?('example.com')

      ::ApplicationMailer.default[:from].presence
    end

    def mailer_config_from_address
      return if Rails.application.config.action_mailer.default_url_options&.dig(:host).blank?

      "reports@#{Rails.application.config.action_mailer.default_url_options[:host].delete_prefix('www.')}"
    end
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
motor-admin-0.1.104 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.103 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.102 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.101 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.100 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.99 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.98 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.97 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.96 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.93 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.92 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.91 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.90 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.89 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.88 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.87 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.86 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.85 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.84 app/mailers/motor/alerts_mailer.rb
motor-admin-0.1.83 app/mailers/motor/alerts_mailer.rb