2014-06-13 10:34:15 +00:00
|
|
|
module Gitlab
|
|
|
|
class UrlBuilder
|
2016-03-24 16:00:26 +00:00
|
|
|
include Gitlab::Routing.url_helpers
|
2015-09-09 12:37:34 +00:00
|
|
|
include GitlabRoutingHelper
|
2016-03-31 07:21:20 +00:00
|
|
|
include ActionView::RecordIdentifier
|
2014-06-13 10:34:15 +00:00
|
|
|
|
2016-04-13 09:25:42 +00:00
|
|
|
attr_reader :object
|
2014-06-13 10:34:15 +00:00
|
|
|
|
2016-04-13 09:25:42 +00:00
|
|
|
def self.build(object)
|
|
|
|
new(object).url
|
|
|
|
end
|
2015-03-05 18:38:23 +00:00
|
|
|
|
2016-04-13 09:25:42 +00:00
|
|
|
def url
|
|
|
|
case object
|
|
|
|
when Commit
|
|
|
|
commit_url
|
|
|
|
when Issue
|
|
|
|
issue_url(object)
|
|
|
|
when MergeRequest
|
|
|
|
merge_request_url(object)
|
|
|
|
when Note
|
|
|
|
note_url
|
2016-04-05 05:36:09 +00:00
|
|
|
when WikiPage
|
|
|
|
wiki_page_url
|
2016-04-13 09:25:42 +00:00
|
|
|
else
|
|
|
|
raise NotImplementedError.new("No URL builder defined for #{object.class}")
|
2014-06-13 10:34:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-04-13 09:25:42 +00:00
|
|
|
def initialize(object)
|
|
|
|
@object = object
|
2014-06-13 10:34:15 +00:00
|
|
|
end
|
2015-02-24 06:50:40 +00:00
|
|
|
|
2016-04-13 09:25:42 +00:00
|
|
|
def commit_url(opts = {})
|
|
|
|
return '' if object.project.nil?
|
|
|
|
|
|
|
|
namespace_project_commit_url({
|
|
|
|
namespace_id: object.project.namespace,
|
|
|
|
project_id: object.project,
|
|
|
|
id: object.id
|
|
|
|
}.merge!(opts))
|
2015-02-24 06:50:40 +00:00
|
|
|
end
|
2015-03-05 18:38:23 +00:00
|
|
|
|
2016-04-13 09:25:42 +00:00
|
|
|
def note_url
|
|
|
|
if object.for_commit?
|
|
|
|
commit_url(id: object.commit_id, anchor: dom_id(object))
|
|
|
|
|
|
|
|
elsif object.for_issue?
|
|
|
|
issue = Issue.find(object.noteable_id)
|
|
|
|
issue_url(issue, anchor: dom_id(object))
|
|
|
|
|
|
|
|
elsif object.for_merge_request?
|
|
|
|
merge_request = MergeRequest.find(object.noteable_id)
|
|
|
|
merge_request_url(merge_request, anchor: dom_id(object))
|
|
|
|
|
|
|
|
elsif object.for_snippet?
|
|
|
|
snippet = Snippet.find(object.noteable_id)
|
|
|
|
project_snippet_url(snippet, anchor: dom_id(object))
|
2015-03-05 18:38:23 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-05 05:36:09 +00:00
|
|
|
|
|
|
|
def wiki_page_url
|
2016-05-10 08:56:58 +00:00
|
|
|
"#{Gitlab.config.gitlab.url}#{object.wiki.wiki_base_path}/#{object.slug}"
|
2016-04-05 05:36:09 +00:00
|
|
|
end
|
2014-06-13 10:34:15 +00:00
|
|
|
end
|
|
|
|
end
|