Merge pull request #36181 from jamesdabbs/jcd/action-mailbox-for

Expose `mailbox_for` method
This commit is contained in:
George Claghorn 2019-05-09 23:02:21 -05:00 committed by GitHub
commit 6394f9da69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 5 deletions

View File

@ -1,3 +1,5 @@
* Add `ApplicationMailbox.mailbox_for` to expose mailbox routing.
*James Dabbs*
Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/actionmailbox/CHANGELOG.md) for previous changes. Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/actionmailbox/CHANGELOG.md) for previous changes.

View File

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

View File

@ -17,6 +17,10 @@ module ActionMailbox
def route(inbound_email) def route(inbound_email)
router.route(inbound_email) router.route(inbound_email)
end end
def mailbox_for(inbound_email)
router.mailbox_for(inbound_email)
end
end end
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 assert_equal "Discussion: Let's debate these attachments", $processed
end end
end end
test "mailbox_for" do
inbound_email = create_inbound_email_from_fixture "welcome.eml", status: :pending
assert_equal RepliesMailbox, ApplicationMailbox.mailbox_for(inbound_email)
end
end end

View File

@ -135,5 +135,19 @@ module ActionMailbox
@router.add_route Array.new, to: :first @router.add_route Array.new, to: :first
end end
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
end end