diff options
author | George Claghorn <george@basecamp.com> | 2019-05-09 23:02:21 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-09 23:02:21 -0500 |
commit | 6394f9da6938d0dccfd8c95f10b2fb7ee98ab953 (patch) | |
tree | 366fc7d2a8c064f5c4987c4344d27ba5365a92e8 /actionmailbox | |
parent | 3363a3c2a54ab1796f1a520538820271f977bc71 (diff) | |
parent | 937f7d582efc75bae336fbf1de4bd93476ab7f5e (diff) | |
download | rails-6394f9da6938d0dccfd8c95f10b2fb7ee98ab953.tar.gz rails-6394f9da6938d0dccfd8c95f10b2fb7ee98ab953.tar.bz2 rails-6394f9da6938d0dccfd8c95f10b2fb7ee98ab953.zip |
Merge pull request #36181 from jamesdabbs/jcd/action-mailbox-for
Expose `mailbox_for` method
Diffstat (limited to 'actionmailbox')
-rw-r--r-- | actionmailbox/CHANGELOG.md | 2 | ||||
-rw-r--r-- | actionmailbox/lib/action_mailbox/router.rb | 10 | ||||
-rw-r--r-- | actionmailbox/lib/action_mailbox/routing.rb | 4 | ||||
-rw-r--r-- | actionmailbox/test/unit/mailbox/routing_test.rb | 5 | ||||
-rw-r--r-- | actionmailbox/test/unit/router_test.rb | 14 |
5 files changed, 30 insertions, 5 deletions
diff --git a/actionmailbox/CHANGELOG.md b/actionmailbox/CHANGELOG.md index a86adae5a7..605a38b06b 100644 --- a/actionmailbox/CHANGELOG.md +++ b/actionmailbox/CHANGELOG.md @@ -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. diff --git a/actionmailbox/lib/action_mailbox/router.rb b/actionmailbox/lib/action_mailbox/router.rb index 71370e409d..54982eae21 100644 --- a/actionmailbox/lib/action_mailbox/router.rb +++ b/actionmailbox/lib/action_mailbox/router.rb @@ -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 diff --git a/actionmailbox/lib/action_mailbox/routing.rb b/actionmailbox/lib/action_mailbox/routing.rb index 58462a44c6..8391bf9db0 100644 --- a/actionmailbox/lib/action_mailbox/routing.rb +++ b/actionmailbox/lib/action_mailbox/routing.rb @@ -17,6 +17,10 @@ module ActionMailbox def route(inbound_email) router.route(inbound_email) end + + def mailbox_for(inbound_email) + router.mailbox_for(inbound_email) + end end end end diff --git a/actionmailbox/test/unit/mailbox/routing_test.rb b/actionmailbox/test/unit/mailbox/routing_test.rb index d4ba702ac5..8302b1d5cc 100644 --- a/actionmailbox/test/unit/mailbox/routing_test.rb +++ b/actionmailbox/test/unit/mailbox/routing_test.rb @@ -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 + inbound_email = create_inbound_email_from_fixture "welcome.eml", status: :pending + assert_equal RepliesMailbox, ApplicationMailbox.mailbox_for(inbound_email) + end end diff --git a/actionmailbox/test/unit/router_test.rb b/actionmailbox/test/unit/router_test.rb index c5dce60856..4dd3730604 100644 --- a/actionmailbox/test/unit/router_test.rb +++ b/actionmailbox/test/unit/router_test.rb @@ -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 |