summaryrefslogtreecommitdiff
path: root/app/presenters/snippet_presenter.rb
blob: d814c4404b677eeeb56f8d807b65dc032044e146 (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
# frozen_string_literal: true

class SnippetPresenter < Gitlab::View::Presenter::Delegated
  presents :snippet

  def raw_url
    url_builder.build(snippet, raw: true)
  end

  def ssh_url_to_repo
    snippet.ssh_url_to_repo if snippet.repository_exists?
  end

  def http_url_to_repo
    snippet.http_url_to_repo if snippet.repository_exists?
  end

  def can_read_snippet?
    can_access_resource?("read")
  end

  def can_update_snippet?
    can_access_resource?("update")
  end

  def can_admin_snippet?
    can_access_resource?("admin")
  end

  def can_report_as_spam?
    snippet.submittable_as_spam_by?(current_user)
  end

  def blob
    blobs.first
  end

  def blobs
    if snippet.empty_repo?
      [snippet.blob]
    else
      snippet.blobs
    end
  end

  private

  def can_access_resource?(ability_prefix)
    can?(current_user, ability_name(ability_prefix), snippet)
  end

  def ability_name(ability_prefix)
    "#{ability_prefix}_#{snippet.to_ability_name}".to_sym
  end
end