def _get_active_tab(visible_tabs, request_path):
    """
    return the tab that claims the longest matching url_prefix

    if one tab claims
      '/a/{domain}/data/'
    and another tab claims
      '/a/{domain}/data/edit/case_groups/'
    then the second tab wins because it's a longer match.

    """

    matching_tabs = sorted(
        (url_prefix, tab)
        for tab in visible_tabs
        for url_prefix in tab.url_prefixes
        if request_path.startswith(url_prefix)
    )

    if matching_tabs:
        _, tab = matching_tabs[-1]
        return tab