2014-06-13 06:34:15 -04:00
|
|
|
module Gitlab
|
|
|
|
class UrlBuilder
|
2016-03-24 12:00:26 -04:00
|
|
|
include Gitlab::Routing.url_helpers
|
2015-09-09 08:37:34 -04:00
|
|
|
include GitlabRoutingHelper
|
2016-03-31 03:21:20 -04:00
|
|
|
include ActionView::RecordIdentifier
|
2014-06-13 06:34:15 -04:00
|
|
|
|
2016-04-13 05:25:42 -04:00
|
|
|
attr_reader :object
|
2014-06-13 06:34:15 -04:00
|
|
|
|
2016-04-13 05:25:42 -04:00
|
|
|
def self.build(object)
|
|
|
|
new(object).url
|
|
|
|
end
|
2015-03-05 13:38:23 -05:00
|
|
|
|
2016-04-13 05:25:42 -04: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 01:36:09 -04:00
|
|
|
when WikiPage
|
|
|
|
wiki_page_url
|
2016-08-11 18:11:16 -04:00
|
|
|
when ProjectSnippet
|
|
|
|
project_snippet_url(object)
|
2016-04-13 05:25:42 -04:00
|
|
|
else
|
|
|
|
raise NotImplementedError.new("No URL builder defined for #{object.class}")
|
2014-06-13 06:34:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-04-13 05:25:42 -04:00
|
|
|
def initialize(object)
|
|
|
|
@object = object
|
2014-06-13 06:34:15 -04:00
|
|
|
end
|
2015-02-24 01:50:40 -05:00
|
|
|
|
2016-04-13 05:25:42 -04: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 01:50:40 -05:00
|
|
|
end
|
2015-03-05 13:38:23 -05:00
|
|
|
|
2016-04-13 05:25:42 -04: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 13:38:23 -05:00
|
|
|
end
|
|
|
|
end
|
2016-04-05 01:36:09 -04:00
|
|
|
|
|
|
|
def wiki_page_url
|
2016-05-03 11:33:43 -04:00
|
|
|
namespace_project_wiki_url(object.wiki.project.namespace, object.wiki.project, object.slug)
|
2016-04-05 01:36:09 -04:00
|
|
|
end
|
2014-06-13 06:34:15 -04:00
|
|
|
end
|
|
|
|
end
|