summaryrefslogtreecommitdiff
path: root/app/serializers/diffs_metadata_entity.rb
blob: 681e629244fac725ec5c18a57da7ac2ef927cae9 (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
# frozen_string_literal: true

class DiffsMetadataEntity < DiffsEntity
  unexpose :diff_files
  expose :diff_files, using: DiffFileMetadataEntity do |diffs, _|
    diffs.raw_diff_files(sorted: true)
  end

  expose :conflict_resolution_path do |_, options|
    presenter(options[:merge_request]).conflict_resolution_path
  end

  expose :has_conflicts do |_, options|
    options[:merge_request].cannot_be_merged?
  end

  expose :can_merge do |_, options|
    options[:merge_request].can_be_merged_by?(request.current_user)
  end

  expose :project_path
  expose :project_name

  expose :username
  expose :user_full_name

  private

  def project_path
    request.project&.full_path
  end

  def project_name
    request.project&.name
  end

  def username
    request.current_user&.username
  end

  def user_full_name
    request.current_user&.name
  end

  def presenter(merge_request)
    @presenters ||= {}
    @presenters[merge_request] ||= MergeRequestPresenter.new(merge_request, current_user: request.current_user) # rubocop: disable CodeReuse/Presenter
  end
end