discussion_on_diff.rb 1.4 KB
Newer Older
D
Douwe Maan 已提交
1
# Contains functionality shared between `DiffDiscussion` and `LegacyDiffDiscussion`.
2 3 4
module DiscussionOnDiff
  extend ActiveSupport::Concern

5
  NUMBER_OF_TRUNCATED_DIFF_LINES = 16
6

7
  included do
8 9 10 11 12 13
    delegate  :line_code,
              :original_line_code,
              :diff_file,
              :diff_line,
              :for_line?,
              :active?,
14
              :created_at_diff?,
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

              to: :first_note

    delegate  :file_path,
              :blob,
              :highlighted_diff_lines,
              :diff_lines,

              to: :diff_file,
              allow_nil: true
  end

  def diff_discussion?
    true
  end

F
Felipe Artur 已提交
31 32 33 34
  def file_new_path
    first_note.position.new_path
  end

35 36 37 38
  def on_merge_request_commit?
    for_merge_request? && commit_id.present?
  end

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  # Returns an array of at most 16 highlighted lines above a diff note
  def truncated_diff_lines(highlight: true)
    lines = highlight ? highlighted_diff_lines : diff_lines
    prev_lines = []

    lines.each do |line|
      if line.meta?
        prev_lines.clear
      else
        prev_lines << line

        break if for_line?(line)

        prev_lines.shift if prev_lines.length >= NUMBER_OF_TRUNCATED_DIFF_LINES
      end
    end

    prev_lines
  end
D
Douwe Maan 已提交
58 59 60 61 62 63 64 65

  def line_code_in_diffs(diff_refs)
    if active?(diff_refs)
      line_code
    elsif diff_refs && created_at_diff?(diff_refs)
      original_line_code
    end
  end
66
end