Module: DiscussionOnDiff

Extended by:
ActiveSupport::Concern
Included in:
DiffDiscussion, LegacyDiffDiscussion
Defined in:
app/models/concerns/discussion_on_diff.rb

Overview

Contains functionality shared between `DiffDiscussion` and `LegacyDiffDiscussion`.

Constant Summary collapse

NUMBER_OF_TRUNCATED_DIFF_LINES =
16

Instance Method Summary collapse

Instance Method Details

#diff_discussion?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'app/models/concerns/discussion_on_diff.rb', line 26

def diff_discussion?
  true
end

#diff_fileObject


61
62
63
64
65
66
# File 'app/models/concerns/discussion_on_diff.rb', line 61

def diff_file
  strong_memoize(:diff_file) do
    # Falling back here is important as `note_diff_files` are created async.
    fetch_preloaded_diff_file || first_note.diff_file
  end
end

#file_new_pathObject


30
31
32
# File 'app/models/concerns/discussion_on_diff.rb', line 30

def file_new_path
  first_note.position.new_path
end

#line_code_in_diffs(diff_refs) ⇒ Object


68
69
70
71
72
73
74
# File 'app/models/concerns/discussion_on_diff.rb', line 68

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

#on_merge_request_commit?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'app/models/concerns/discussion_on_diff.rb', line 34

def on_merge_request_commit?
  for_merge_request? && commit_id.present?
end

#truncated_diff_lines(highlight: true, diff_limit: nil) ⇒ Object

Returns an array of at most 16 highlighted lines above a diff note


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/concerns/discussion_on_diff.rb', line 39

def truncated_diff_lines(highlight: true, diff_limit: nil)
  return [] unless on_text?
  return [] if diff_line.nil?

  diff_limit = [diff_limit, NUMBER_OF_TRUNCATED_DIFF_LINES].compact.min
  lines = highlight ? highlighted_diff_lines : diff_lines

  initial_line_index = [diff_line.index - diff_limit + 1, 0].max

  prev_lines = []

  lines[initial_line_index..diff_line.index].each do |line|
    if line.meta?
      prev_lines.clear
    else
      prev_lines << line
    end
  end

  prev_lines
end