summaryrefslogtreecommitdiff
path: root/app/presenters/release_presenter.rb
blob: 70fe63247129c117973473c192d106f5ecc87238 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

class ReleasePresenter < Gitlab::View::Presenter::Delegated
  presents ::Release, as: :release

  def commit_path
    return unless release.commit && can_read_code?

    project_commit_path(project, release.commit.id)
  end

  def tag_path
    return unless can_read_code?

    project_tag_path(project, release.tag)
  end

  def self_url
    project_release_url(project, release)
  end

  def opened_merge_requests_url
    project_merge_requests_url(project, params_for_issues_and_mrs)
  end

  def merged_merge_requests_url
    project_merge_requests_url(project, params_for_issues_and_mrs(state: 'merged'))
  end

  def closed_merge_requests_url
    project_merge_requests_url(project, params_for_issues_and_mrs(state: 'closed'))
  end

  def opened_issues_url
    project_issues_url(project, params_for_issues_and_mrs)
  end

  def closed_issues_url
    project_issues_url(project, params_for_issues_and_mrs(state: 'closed'))
  end

  def edit_url
    return unless release_edit_page_available?

    edit_project_release_url(project, release)
  end

  delegator_override :assets_count
  def assets_count
    if can_read_code?
      release.assets_count
    else
      release.assets_count(except: [:sources])
    end
  end

  delegator_override :name
  def name
    release.name
  end

  def download_url(filepath)
    filepath = filepath.sub(%r{^/}, '') if filepath.start_with?('/')

    downloads_project_release_url(project, release, filepath)
  end

  private

  def can_read_code?
    can?(current_user, :read_code, project)
  end

  def params_for_issues_and_mrs(state: 'opened')
    { scope: 'all', state: state, release_tag: release.tag }
  end

  def release_edit_page_available?
    can?(current_user, :update_release, release)
  end
end