debian-mirror-gitlab/lib/gitlab/url_builder.rb

80 lines
1.7 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module Gitlab
class UrlBuilder
2017-09-10 17:25:29 +05:30
include Gitlab::Routing
2015-04-26 12:48:37 +05:30
include GitlabRoutingHelper
2016-06-02 11:05:42 +05:30
include ActionView::RecordIdentifier
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
attr_reader :object
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
def self.build(object)
new(object).url
end
2015-04-26 12:48:37 +05:30
2016-06-02 11:05:42 +05:30
def url
case object
when Commit
commit_url
when Issue
issue_url(object)
when MergeRequest
merge_request_url(object)
when Note
note_url
when WikiPage
wiki_page_url
2016-09-29 09:46:39 +05:30
when ProjectSnippet
2017-09-10 17:25:29 +05:30
project_snippet_url(object.project, object)
2017-08-17 22:00:37 +05:30
when Snippet
2017-09-10 17:25:29 +05:30
snippet_url(object)
2018-11-08 19:23:39 +05:30
when Milestone
milestone_url(object)
2016-06-02 11:05:42 +05:30
else
raise NotImplementedError.new("No URL builder defined for #{object.class}")
2014-09-02 18:07:02 +05:30
end
end
private
2016-06-02 11:05:42 +05:30
def initialize(object)
@object = object
2015-04-26 12:48:37 +05:30
end
2016-06-02 11:05:42 +05:30
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-04-26 12:48:37 +05:30
end
2016-06-02 11:05:42 +05:30
def note_url
if object.for_commit?
commit_url(id: object.commit_id, anchor: dom_id(object))
elsif object.for_issue?
2017-09-10 17:25:29 +05:30
issue_url(object.noteable, anchor: dom_id(object))
2016-06-02 11:05:42 +05:30
elsif object.for_merge_request?
2017-09-10 17:25:29 +05:30
merge_request_url(object.noteable, anchor: dom_id(object))
2016-06-02 11:05:42 +05:30
elsif object.for_snippet?
2017-09-10 17:25:29 +05:30
snippet = object.noteable
if snippet.is_a?(PersonalSnippet)
snippet_url(snippet, anchor: dom_id(object))
else
project_snippet_url(snippet.project, snippet, anchor: dom_id(object))
end
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
def wiki_page_url
2017-09-10 17:25:29 +05:30
project_wiki_url(object.wiki.project, object.slug)
2016-06-02 11:05:42 +05:30
end
2014-09-02 18:07:02 +05:30
end
end