summaryrefslogtreecommitdiff
path: root/lib/gitlab/light_url_builder.rb
blob: e4517b5610bef8bc51b48e3b7c0c18ee7e9d4b09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Gitlab
  # Similar to UrlBuilder, but using IDs to avoid querying the DB for objects
  # Useful for using in conjunction with Arel queries.
  class LightUrlBuilder
    include Gitlab::Routing.url_helpers
    include GitlabRoutingHelper
    include ActionView::RecordIdentifier

    def self.build(*args)
      new(*args).url
    end

    def initialize(entity:, project: nil, id:, opts: {})
      @entity = entity
      @project = project
      @id = id
      @opts = opts
    end

    def url
      url_method = "#{@entity}_url"
      raise NotImplementedError.new("No Light URL builder defined for #{@entity}") unless respond_to?(url_method)

      public_send(url_method)
    end

    def issue_url
      namespace_project_issue_url({
                                    namespace_id: @project.namespace,
                                    project_id: @project,
                                    id: @id
                                  }.merge!(@opts))
    end

    def user_avatar_url
      User.find(@id).avatar_url
    end

    def commit_url
      namespace_project_commit_url({
                                     namespace_id: @project.namespace,
                                     project_id: @project,
                                     id: @id
                                   }.merge!(@opts))
    end

    def merge_request_url
      namespace_project_merge_request_url({
                                            namespace_id: @project.namespace,
                                            project_id: @project,
                                            id: @id
                                          }.merge!(@opts))
    end

    def branch_url
      "#{project_url(@project)}/commits/#{@id}"
    end

    def user_url
      Gitlab::Routing.url_helpers.user_url(@id)
    end

    def build_url
      namespace_project_build_url(@project.namespace, @project, @id)
    end
  end
end