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

69 lines
1.6 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module Gitlab
class UrlBuilder
2016-06-02 11:05:42 +05:30
include Gitlab::Routing.url_helpers
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
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?
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-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
namespace_project_wiki_url(object.wiki.project.namespace, object.wiki.project, object.slug)
end
2014-09-02 18:07:02 +05:30
end
end