Rails DeliveryJob deprecation warning: How to upgrade to MailDeliveryJob?

Viewed 394

I have a controller action that enqueues the delivery of an email. I wrote a test for that controller action and I've been getting a deprecation warning when I run the tests:

DEPRECATION WARNING: Sending mail with DeliveryJob and Parameterized::DeliveryJob is deprecated and will be removed in Rails 6.1. Please use MailDeliveryJob instead.

controller:

  def send_email
    SubscriptionMailer.with(user_id: @user.id).send_welcome_email.deliver_later
  end

SubscriptionMailer:

class SubscriptionMailer < ApplicationMailer
  helper :auxiliaries
  before_action { @user = User.find_by(id: params[:user_id]) }

  def send_welcome_email
    @base_email = find_base_email_or_raise("welcome_email")
    @subject = "#{@user.fname} #{@base_email.subject}"

    send_email(subject: @subject)
  end
end

test:

  test "#send_email should enqueue SubscriptionMailer's #send_welcome_email" do
    assert_emails 1 do
      get send_email_admin_subscriptions_activation_path(
        @user.id,
      )
    end
  end

How do I upgrade my App to get rid of the warnings?

I've read about uncommenting a line in new_framework_defaults_6_0.rb in different posts but that didn't resolve the issue.

1 Answers

To fix the deprecation warning add this line to application.rb:

config.action_mailer.delivery_job = "ActionMailer::MailDeliveryJob"

Source: https://github.com/rails/rails/issues/37068#issuecomment-545531362

Q: Does this affect the way the Emails are delivered in any way?

The parameters on the method have changed:

# DeliveryJob
def perform(mailer, mail_method, delivery_method, *args)

# MailDeliveryJob
def perform(mailer, mail_method, delivery_method, args:, params: nil)

So you might have to change the arguments in the method call wherever applicable.

You can find more details on this PR: https://github.com/rails/rails/pull/34591

Related