initializers: lazy-load Mailjet gem

This fixes an error message on app startup about autoloaded
constants:

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

The reason for this error is that the Mailjet gem forces the
immediate loading of `action_mailer`. Which leads to the
following sequence of events:

On app init, when bundler requires all the gems in the Gemfile:
- The Mailjet gem is required,
- It loads `ActionMailer::Base`.

Later, when Rails initializes itself:
- `ActionText` creates an `action_text.helpers` initializer,
- This initializer register hooks to add `ActionText` helpers
when either `action_controller_base` or `action_mailer` are loaded,
- But as `action_mailer` has already been loaded, the hook is trigerred
immediately,
- ActiveSupport::Dependencies notices `ActionText` constants do not
exist yet, auto-load them, and add the constants to
`ActiveSupport::Dependencies.autoloaded_constants`.

And even later, at the end of the Rails initialization process:
- The `:warn_if_autoloaded` initializer is run,
- It notices that `autoloaded_constants` is not empty, and prints the
warning message.

See https://github.com/mailjet/mailjet-gem/issues/213
This commit is contained in:
Pierre de La Morinerie 2021-04-20 11:57:03 +00:00
parent b96151934e
commit 9ad57fde2a
2 changed files with 9 additions and 5 deletions

View file

@ -49,7 +49,7 @@ gem 'kaminari', '1.2.1' # Pagination
gem 'listen' # Required by ActiveSupport::EventedFileUpdateChecker
gem 'lograge'
gem 'logstash-event'
gem 'mailjet'
gem 'mailjet', require: false
gem 'openid_connect'
gem 'pg'
gem 'phonelib'

View file

@ -1,5 +1,9 @@
Mailjet.configure do |config|
config.api_key = Rails.application.secrets.mailjet[:api_key]
config.secret_key = Rails.application.secrets.mailjet[:secret_key]
config.default_from = CONTACT_EMAIL
ActiveSupport.on_load(:action_mailer) do
require 'mailjet'
Mailjet.configure do |config|
config.api_key = Rails.application.secrets.mailjet[:api_key]
config.secret_key = Rails.application.secrets.mailjet[:secret_key]
config.default_from = CONTACT_EMAIL
end
end