Skip to content

Commit

Permalink
Fix default mailer class not resolving with autoloading in background…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
janko committed Dec 19, 2024
1 parent 65bf4ce commit 2b38831
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/rodauth/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions lib/rodauth/rails/feature/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/rodauth/rails/mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Rodauth
module Rails
class Mailer < ActionMailer::Base
def create_email(options)
mail(options)
end
end
end
end

0 comments on commit 2b38831

Please sign in to comment.