Skip to content

Registering mail interceptor causes warning in Rails 6.1 #42170

@ryanb

Description

@ryanb

Steps to reproduce

In a new Rails 6.1.3.2 app, create a mail interceptor as documented in the guide.

# config/initializers/my_mail_interceptor.rb
class MyMailInterceptor
  def self.delivering_email(message)
    # ...
  end
end
ActionMailer::Base.register_interceptor(MyMailInterceptor)

And then run the test suite.

rails test

Expected behavior

No deprecation warning.

Actual behavior

Running the tests shows this warning.

DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper and ActionText::TagHelper.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload ActionText::ContentHelper, for example,
the expected changes won't be reflected in that stale Module object.

These autoloaded constants have been unloaded.

In order to autoload safely at boot time, please wrap your code in a reloader
callback this way:

    Rails.application.reloader.to_prepare do
      # Autoload classes and modules needed at boot time here.
    end

That block runs when the application boots, and every time there is a reload.
For historical reasons, it may run twice, so it has to be idempotent.

Check the "Autoloading and Reloading Constants" guide to learn more about how

Wrapping the ActionMailer::Base call in the recommended block appears to resolve this.

Rails.application.reloader.to_prepare do
  ActionMailer::Base.register_interceptor(MyMailInterceptor)
end

However, I have a couple questions.

  1. Will this cause the interceptor to be added multiple times in some cases? I'm uncertain if that call is idempotent.

  2. Should the guide be updated to include this block? Or is there another preferred method for registering an interceptor?

System configuration

Rails version: 6.1.3.2

Ruby version: 2.5.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions