From 2b388317c377a367f11509df0a58af38f60c656e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Thu, 19 Dec 2024 20:45:08 +0100 Subject: [PATCH] Fix default mailer class not resolving with autoloading in background jobs The `Rodauth::Rails::Feature` class gets autoloaded only when the Rodauth configuration is getting evaluated. In the web server, this happens immediately before the request reaches the Rails router, because the Rodauth middleware loads the Rodauth app, which in turn loads Rodauth configuration. However, in a background job process, enqueued email deliveries will first resolve the mailer class, and then the mailer will load the Rodauth configuration. So, the job processor will attempt to resolve `Rodauth::Rails::Feature::Email::Mailer`, which fails, because the `rails` Rodauth feature hasn't yet been loaded. The solution is simple: move the mailer class in a place where it can be autoloaded without the feature being loaded. As a bonus, this results in a much shorter `Rodauth::Rails::Mailer` class. Fixes #337 --- lib/rodauth/rails.rb | 1 + lib/rodauth/rails/feature/email.rb | 9 +-------- lib/rodauth/rails/mailer.rb | 9 +++++++++ 3 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 lib/rodauth/rails/mailer.rb diff --git a/lib/rodauth/rails.rb b/lib/rodauth/rails.rb index c670783..599ea8d 100644 --- a/lib/rodauth/rails.rb +++ b/lib/rodauth/rails.rb @@ -10,6 +10,7 @@ class Error < StandardError # This allows avoiding loading Rodauth at boot time. autoload :App, "rodauth/rails/app" autoload :Auth, "rodauth/rails/auth" + autoload :Mailer, "rodauth/rails/mailer" @app = nil @middleware = true diff --git a/lib/rodauth/rails/feature/email.rb b/lib/rodauth/rails/feature/email.rb index d25fafd..d0c7367 100644 --- a/lib/rodauth/rails/feature/email.rb +++ b/lib/rodauth/rails/feature/email.rb @@ -12,20 +12,13 @@ module Email # Create emails with ActionMailer which uses configured delivery method. def create_email_to(to, subject, body) - Mailer.create_email(to: to, from: email_from, subject: "#{email_subject_prefix}#{subject}", body: body) + Rodauth::Rails::Mailer.create_email(to: to, from: email_from, subject: "#{email_subject_prefix}#{subject}", body: body) end # Delivers the given email. def send_email(email) email.deliver_now end - - # ActionMailer subclass for correct email delivering. - class Mailer < ActionMailer::Base - def create_email(options) - mail(options) - end - end end end end diff --git a/lib/rodauth/rails/mailer.rb b/lib/rodauth/rails/mailer.rb new file mode 100644 index 0000000..c61de1c --- /dev/null +++ b/lib/rodauth/rails/mailer.rb @@ -0,0 +1,9 @@ +module Rodauth + module Rails + class Mailer < ActionMailer::Base + def create_email(options) + mail(options) + end + end + end +end