Commit 92783eae authored by Robert Speicher's avatar Robert Speicher

Refactor `UserReferenceFilter#user_link_filter`

parent 9d3a1d00
...@@ -38,57 +38,65 @@ module Gitlab ...@@ -38,57 +38,65 @@ module Gitlab
# Returns a String with `@user` references replaced with links. All links # Returns a String with `@user` references replaced with links. All links
# have `gfm` and `gfm-project_member` class names attached for styling. # have `gfm` and `gfm-project_member` class names attached for styling.
def user_link_filter(text) def user_link_filter(text)
project = context[:project] self.class.references_in(text) do |match, username|
if username == 'all'
link_to_all
elsif namespace = Namespace.find_by(path: username)
link_to_namespace(namespace) || match
else
match
end
end
end
private
def urls
Rails.application.routes.url_helpers
end
self.class.references_in(text) do |match, user| def link_class
klass = reference_class(:project_member) reference_class(:project_member)
end
def link_to_all
project = context[:project]
if user == 'all'
# FIXME (rspeicher): Law of Demeter # FIXME (rspeicher): Law of Demeter
push_result(:user, *project.team.members.flatten) push_result(:user, *project.team.members.flatten)
url = link_to_all(project) url = urls.namespace_project_url(project.namespace, project,
only_path: context[:only_path])
%(<a href="#{url}" class="#{klass}">@#{user}</a>)
elsif namespace = Namespace.find_by(path: user)
if namespace.is_a?(Group)
if user_can_reference_group?(namespace)
push_result(:user, *namespace.users)
url = group_url(user, only_path: context[:only_path]) %(<a href="#{url}" class="#{link_class}">@all</a>)
%(<a href="#{url}" class="#{klass}">@#{user}</a>)
else
match
end end
else
push_result(:user, namespace.owner)
url = user_url(user, only_path: context[:only_path]) def link_to_namespace(namespace)
%(<a href="#{url}" class="#{klass}">@#{user}</a>) if namespace.is_a?(Group)
end link_to_group(namespace.path, namespace)
else else
match link_to_user(namespace.path, namespace)
end
end end
end end
private def link_to_group(group, namespace)
if user_can_reference_group?(namespace)
push_result(:user, *namespace.users)
def urls url = urls.group_url(group, only_path: context[:only_path])
Rails.application.routes.url_helpers
end
def group_url(*args) %(<a href="#{url}" class="#{link_class}">@#{group}</a>)
urls.group_url(*args) else
nil
end end
def user_url(*args)
urls.user_url(*args)
end end
def link_to_all(project) def link_to_user(user, namespace)
urls.namespace_project_url(project.namespace, project, push_result(:user, namespace.owner)
only_path: context[:only_path])
url = urls.user_url(user, only_path: context[:only_path])
%(<a href="#{url}" class="#{link_class}">@#{user}</a>)
end end
def user_can_reference_group?(group) def user_can_reference_group?(group)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment