summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2015-04-25 18:07:20 -0400
committerRobert Speicher <rspeicher@gmail.com>2015-04-25 18:07:20 -0400
commit92783eae2be4bc9305759a57f2f4f17d7705f18d (patch)
tree0878dbf27a9494f95e9018171eae53534a1be3ac /lib
parent9d3a1d00543f2bbf591a880575795e416d45c763 (diff)
downloadgitlab-ce-92783eae2be4bc9305759a57f2f4f17d7705f18d.tar.gz
Refactor `UserReferenceFilter#user_link_filter`
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/markdown/user_reference_filter.rb78
1 files changed, 43 insertions, 35 deletions
diff --git a/lib/gitlab/markdown/user_reference_filter.rb b/lib/gitlab/markdown/user_reference_filter.rb
index b4c48d29684..9cd5245cbcd 100644
--- a/lib/gitlab/markdown/user_reference_filter.rb
+++ b/lib/gitlab/markdown/user_reference_filter.rb
@@ -38,34 +38,11 @@ module Gitlab
# Returns a String with `@user` references replaced with links. All links
# have `gfm` and `gfm-project_member` class names attached for styling.
def user_link_filter(text)
- project = context[:project]
-
- self.class.references_in(text) do |match, user|
- klass = reference_class(:project_member)
-
- if user == 'all'
- # FIXME (rspeicher): Law of Demeter
- push_result(:user, *project.team.members.flatten)
-
- url = link_to_all(project)
-
- %(<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="#{klass}">@#{user}</a>)
- else
- match
- end
- else
- push_result(:user, namespace.owner)
-
- url = user_url(user, only_path: context[:only_path])
- %(<a href="#{url}" class="#{klass}">@#{user}</a>)
- end
+ 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
@@ -78,17 +55,48 @@ module Gitlab
Rails.application.routes.url_helpers
end
- def group_url(*args)
- urls.group_url(*args)
+ def link_class
+ reference_class(:project_member)
end
- def user_url(*args)
- urls.user_url(*args)
+ def link_to_all
+ project = context[:project]
+
+ # FIXME (rspeicher): Law of Demeter
+ push_result(:user, *project.team.members.flatten)
+
+ url = urls.namespace_project_url(project.namespace, project,
+ only_path: context[:only_path])
+
+ %(<a href="#{url}" class="#{link_class}">@all</a>)
end
- def link_to_all(project)
- urls.namespace_project_url(project.namespace, project,
- only_path: context[:only_path])
+ def link_to_namespace(namespace)
+ if namespace.is_a?(Group)
+ link_to_group(namespace.path, namespace)
+ else
+ link_to_user(namespace.path, namespace)
+ end
+ end
+
+ def link_to_group(group, namespace)
+ if user_can_reference_group?(namespace)
+ push_result(:user, *namespace.users)
+
+ url = urls.group_url(group, only_path: context[:only_path])
+
+ %(<a href="#{url}" class="#{link_class}">@#{group}</a>)
+ else
+ nil
+ end
+ end
+
+ def link_to_user(user, namespace)
+ push_result(:user, namespace.owner)
+
+ url = urls.user_url(user, only_path: context[:only_path])
+
+ %(<a href="#{url}" class="#{link_class}">@#{user}</a>)
end
def user_can_reference_group?(group)