BigW Consortium Gitlab

url_builder.rb 1.69 KB
Newer Older
1 2
module Gitlab
  class UrlBuilder
3
    include Gitlab::Routing.url_helpers
4
    include GitlabRoutingHelper
5
    include ActionView::RecordIdentifier
6

7
    attr_reader :object
8

9 10 11
    def self.build(object)
      new(object).url
    end
12

13 14 15 16 17 18 19 20 21 22
    def url
      case object
      when Commit
        commit_url
      when Issue
        issue_url(object)
      when MergeRequest
        merge_request_url(object)
      when Note
        note_url
23 24
      when WikiPage
        wiki_page_url
25 26
      when ProjectSnippet
        project_snippet_url(object)
27 28
      when Snippet
        personal_snippet_url(object)
29 30
      else
        raise NotImplementedError.new("No URL builder defined for #{object.class}")
31 32 33 34 35
      end
    end

    private

36 37
    def initialize(object)
      @object = object
38
    end
39

40 41 42 43 44 45 46 47
    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))
48
    end
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    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))
65 66
      end
    end
67 68

    def wiki_page_url
69
      namespace_project_wiki_url(object.wiki.project.namespace, object.wiki.project, object.slug)
70
    end
71 72
  end
end