Sha256: 94ff734a94482f2a5614ad5d1f9f88d53c39faaae6f193e81a3f85004c660419

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module EmailSpec
  module MailerDeliveries
    def all_emails
      deliveries
    end

    def last_email_sent
      deliveries.last || raise("No email has been sent!")
    end

    def reset_mailer
      deliveries.clear
    end

    def mailbox_for(address)
      deliveries.select { |email|
        (email.to && email.to.include?(address)) ||
        (email.bcc && email.bcc.include?(address)) ||
        (email.cc && email.cc.include?(address)) }
    end

    protected

    def deliveries
      if ActionMailer::Base.delivery_method == :cache
        mailer.cached_deliveries
      else
        mailer.deliveries
      end
    end
  end

  module ARMailerDeliveries
    def all_emails
      Email.all.map{ |email| parse_to_tmail(email) }
    end

    def last_email_sent
      if email = Email.last
        TMail::Mail.parse(email.mail)
      else
        raise("No email has been sent!")
      end
    end

    def reset_mailer
      Email.delete_all
    end

    def mailbox_for(address)
      Email.all.select { |email|
        (email.to && email.to.include?(address)) ||
        (email.bcc && email.bcc.include?(address)) ||
        (email.cc && email.cc.include?(address)) }.map{ |email| parse_to_tmail(email) }
    end

    def parse_to_tmail(email)
      TMail::Mail.parse(email.mail)
    end
  end

  if defined?(Pony)
    module ::Pony
      def self.deliveries
        @deliveries ||= []
      end

      def self.mail(options)
        deliveries << build_tmail(options)
      end
    end
  end

  module Deliveries
    if defined?(Pony)
      def deliveries; Pony::deliveries ; end
      include EmailSpec::MailerDeliveries
    elsif ActionMailer::Base.delivery_method == :activerecord
      include EmailSpec::ARMailerDeliveries
    else
      def mailer; ActionMailer::Base; end
      include EmailSpec::MailerDeliveries
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
email_spec-tricycle-0.6.3 lib/email_spec/deliveries.rb