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

Expose mailbox_for method #36181

Merged
merged 4 commits into from
May 10, 2019
Merged
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: 2 additions & 0 deletions actionmailbox/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 5 additions & 5 deletions actionmailbox/lib/action_mailbox/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def add_route(address, to:)
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!
Expand All @@ -30,12 +30,12 @@ def route(inbound_email)
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

Expand Down
4 changes: 4 additions & 0 deletions actionmailbox/lib/action_mailbox/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def routing(routes)
def route(inbound_email)
router.route(inbound_email)
end

def mailbox_for(inbound_email)
router.mailbox_for(inbound_email)
end
end
end
end
5 changes: 5 additions & 0 deletions actionmailbox/test/unit/mailbox/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions actionmailbox/test/unit/router_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,19 @@ class RouterTest < ActiveSupport::TestCase
@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