Sha256: e57f1264f46bc4df700c79cc3c0fdc50d725456dc7be9b2c6fbb73c43e5f35d8

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

module Mailboxer
  class MailDispatcher
    attr_reader :mailable, :receipts

    def initialize(mailable, receipts)
      @mailable, @receipts = mailable, receipts
    end

    def call
      return false unless Mailboxer.uses_emails

      receipts.map do |receipt|
        email_to = receipt.receiver.send(Mailboxer.email_method, mailable)
        send_email(receipt) if email_to.present?
      end
    end

    private

    def mailer
      mailer_config_method || mailer_from_mailable || mailer_constant
    end

    def mailer_from_mailable
      mailable.mailer_class if mailable.respond_to? :mailer_class
    end

    def mailer_config_method
      klass = mailable.class.name.demodulize
      method = "#{klass.downcase}_mailer".to_sym
      Mailboxer.send(method) if Mailboxer.respond_to? method
    end

    def mailer_constant
      "#{mailable.class.name}Mailer".constantize
    end

    def send_email(receipt)
      if Mailboxer.custom_deliver_proc
        Mailboxer.custom_deliver_proc.call(mailer, mailable, receipt.receiver)
      else
        default_send_email(receipt)
      end
    end

    def default_send_email(receipt)
      mail = mailer.send_email(mailable, receipt.receiver)
      mail.respond_to?(:deliver_now) ? mail.deliver_now : mail.deliver
      receipt.assign_attributes(
        :delivery_method => :email,
        :message_id => mail.message_id
      )
      mail
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mailboxer-0.15.1 lib/mailboxer/mail_dispatcher.rb
mailboxer-0.15.0 lib/mailboxer/mail_dispatcher.rb