1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Expose mailbox_for method

Currently, the only exposed entry point into the ApplicationMailbox's configured
routing system is to call `route`, which performs a lot of work to fully
`process` inbound email. It'd be nice to have a way (e.g. in test) of checking
which mailbox an email would route to without necessarily processing it yet.
This commit is contained in:
James Dabbs 2019-05-04 10:53:08 -07:00
parent ac1ba44f8d
commit 9f96d094a3
4 changed files with 28 additions and 5 deletions

View file

@ -21,7 +21,7 @@ module ActionMailbox
end
def route(inbound_email)
if mailbox = match_to_mailbox(inbound_email)
if mailbox = mailbox_for(inbound_email)
mailbox.receive(inbound_email)
else
inbound_email.bounced!
@ -30,12 +30,12 @@ module ActionMailbox
end
end
def mailbox_for(inbound_email)
routes.detect { |route| route.match?(inbound_email) }.try(:mailbox_class)
end
private
attr_reader :routes
def match_to_mailbox(inbound_email)
routes.detect { |route| route.match?(inbound_email) }.try(:mailbox_class)
end
end
end

View file

@ -17,6 +17,10 @@ module ActionMailbox
def route(inbound_email)
router.route(inbound_email)
end
def mailbox_for(inbound_email)
router.route(inbound_email)
end
end
end
end

View file

@ -28,4 +28,9 @@ class ActionMailbox::Base::RoutingTest < ActiveSupport::TestCase
assert_equal "Discussion: Let's debate these attachments", $processed
end
end
test "mailbox_for" do
mail = create_inbound_email_from_fixture "welcome.eml", status: :pending
assert_equal RepliesMailbox, ApplicationMailbox.mailbox_for(mail)
end
end

View file

@ -135,5 +135,19 @@ module ActionMailbox
@router.add_route Array.new, to: :first
end
end
test "single string mailbox_for" do
@router.add_routes("first@example.com" => :first)
inbound_email = create_inbound_email_from_mail(to: "first@example.com", subject: "This is a reply")
assert_equal FirstMailbox, @router.mailbox_for(inbound_email)
end
test "mailbox_for with no matches" do
@router.add_routes("first@example.com" => :first)
inbound_email = create_inbound_email_from_mail(to: "second@example.com", subject: "This is a reply")
assert_nil @router.mailbox_for(inbound_email)
end
end
end