Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow from address to fall back to parent mailer #187

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading