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 744ac75
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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
7 changes: 6 additions & 1 deletion 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 All @@ -67,5 +73,4 @@ def reset_config!
@config = Configuration.new
end
end

end
10 changes: 10 additions & 0 deletions test/mailers/passwordless/mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ 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
WithConfig.with_config({default_from_address: nil, parent_mailer: "ApplicationMailer"}) do
email = Passwordless::Mailer.sign_in(
Passwordless::Session.create!(authenticatable: users(:alice), token: "hello")
)

assert_equal ["[email protected]"], email.from
end
end
end

0 comments on commit 744ac75

Please sign in to comment.