Skip to content

Commit 2b38831

Browse files
committed
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
1 parent 65bf4ce commit 2b38831

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

lib/rodauth/rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Error < StandardError
1010
# This allows avoiding loading Rodauth at boot time.
1111
autoload :App, "rodauth/rails/app"
1212
autoload :Auth, "rodauth/rails/auth"
13+
autoload :Mailer, "rodauth/rails/mailer"
1314

1415
@app = nil
1516
@middleware = true

lib/rodauth/rails/feature/email.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@ module Email
1212

1313
# Create emails with ActionMailer which uses configured delivery method.
1414
def create_email_to(to, subject, body)
15-
Mailer.create_email(to: to, from: email_from, subject: "#{email_subject_prefix}#{subject}", body: body)
15+
Rodauth::Rails::Mailer.create_email(to: to, from: email_from, subject: "#{email_subject_prefix}#{subject}", body: body)
1616
end
1717

1818
# Delivers the given email.
1919
def send_email(email)
2020
email.deliver_now
2121
end
22-
23-
# ActionMailer subclass for correct email delivering.
24-
class Mailer < ActionMailer::Base
25-
def create_email(options)
26-
mail(options)
27-
end
28-
end
2922
end
3023
end
3124
end

lib/rodauth/rails/mailer.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Rodauth
2+
module Rails
3+
class Mailer < ActionMailer::Base
4+
def create_email(options)
5+
mail(options)
6+
end
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)