Skip to content

Commit

Permalink
Allow from address to fall back to parent mailer
Browse files Browse the repository at this point in the history
  • Loading branch information
djfpaagman committed Nov 28, 2023
1 parent cd0e0ed commit 8bdc573
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Passwordless
# The mailer responsible for sending Passwordless' mails.
class Mailer < Passwordless.config.parent_mailer.constantize
default from: Passwordless.config.default_from_address
default Passwordless.config.mailer_defaults

# Sends a token and a magic link
#
Expand Down
6 changes: 6 additions & 0 deletions lib/passwordless/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class Configuration
def initialize
set_defaults!
end

def mailer_defaults
# Compacting the Hash removes all keys with nil values, allowing to let
# the `from` address fall back to the parent mailer's default.
{ from: default_from_address }.compact
end
end

module Configurable
Expand Down
19 changes: 19 additions & 0 deletions test/mailers/passwordless/mailer_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require "test_helper"

class ParentMailer < ActionMailer::Base
default from: "parent@mailer.com"
end

class Passwordless::MailerTest < ActionMailer::TestCase
test("sign_in") do
user = users(:alice)
Expand Down Expand Up @@ -35,4 +39,19 @@ class Passwordless::MailerTest < ActionMailer::TestCase
assert_match /sign in: hello\n/, email.body.to_s
assert_match %r{/admins/sign_in/#{session.identifier}/hello}, email.body.to_s
end

test("without default_from_address falls back to parent_mailer") do
Passwordless.configure do |config|
config.default_from_address = nil
config.parent_mailer = "ParentMailer"
end

email = Passwordless::Mailer.sign_in(
Passwordless::Session.create!(authenticatable: users(:alice), token: "hello")
)

assert_equal ["parent@mailer.com"], email.from

Passwordless.reset_config!
end
end

0 comments on commit 8bdc573

Please sign in to comment.