Welcome to mirror list, hosted at ThFree Co, Russian Federation.

event_presenter.rb « presenters « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f7c323fcac4e7839775b94a3b78adf809d2e839 (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
82
83
84
# frozen_string_literal: true

class EventPresenter < Gitlab::View::Presenter::Delegated
  presents ::Event, as: :event

  def initialize(event, **attributes)
    super

    @visible_to_user_cache = ActiveSupport::Cache::MemoryStore.new
  end

  # Caching `visible_to_user?` method in the presenter because it might be called multiple times.
  delegator_override :visible_to_user?
  def visible_to_user?(user = nil)
    return super(user) unless user

    @visible_to_user_cache.fetch(user&.id) { super(user) }
  end

  # implement cache here
  def resource_parent_name
    resource_parent&.full_name || ''
  end

  def target_link_options
    case resource_parent
    when Group
      [event.group, event.target]
    when Project
      [event.project, event.target]
    else
      ''
    end
  end

  def target_type_name
    if design?
      'Design'
    elsif wiki_page?
      'Wiki Page'
    elsif issue? || work_item?
      target.issue_type
    elsif target_type.present?
      target_type.titleize
    else
      "Project"
    end.downcase
  end

  def note_target_type_name
    return unless note?

    if design_note?
      'Design'
    else
      target.noteable_type.titleize
    end.downcase
  end

  def push_activity_description
    return unless push_action?

    if batch_push?
      "#{action_name} #{ref_count} #{ref_type.pluralize(ref_count)}"
    else
      "#{action_name} #{ref_type}"
    end
  end

  def batch_push?
    push_action? && ref_count.to_i > 0
  end

  def linked_to_reference?
    return false unless push_action?
    return false if event.project.nil?

    if tag?
      project.repository.tag_exists?(ref_name)
    else
      project.repository.branch_exists?(ref_name)
    end
  end
end