0
votes
def no_products_2
    @users = [].tap{|users| User.created_days_ago(2).each{|user| users << user if user.buyer_products.empty?}}
    sent_mail = [].tap{|sent_mail| @users.each{|user| sent_mail << (mail to: user.email, subject: "You Haven't Submitted Any Designs Yet")}}
end

My intent is to send emails to a collection of users from one Mailer method. I believe this is working fine, but I'd like to be able to ensure that all emails are being sent with a test. To that end, I'm trying to return an array of all sent messages, but it seems that mailer methods will always return the last Mail instance that was sent.

Any ideas? Do I need to assemble my collection of users and then iterate over them OUTSIDE of the mailer, thereby sending one email at a time from the actual function described above? Or is there a way to do all this inside the mailer function and test that all emails are being sent?

1

1 Answers

0
votes

Ultimately I decided the easiest thing to do would be to assemble the collection of users outside this method, and then iterate over the collection and call this method on each individual user.

def other_method
    @users = [].tap{|users| Order.awaiting_payment(3).each{|order| users << order.buyer.user}}
    @users.each{|user| UserMailer.no_products_2(user.id).deliver}
end

and

def no_products_2(user_id)
    @user = User.find(user_id)
    mail to: @user.email, subject: "You Haven't Submitted Any Designs Yet"
end