debian-mirror-gitlab/app/models/concerns/discussion_on_diff.rb

70 lines
1.5 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
# Contains functionality shared between `DiffDiscussion` and `LegacyDiffDiscussion`.
module DiscussionOnDiff
extend ActiveSupport::Concern
NUMBER_OF_TRUNCATED_DIFF_LINES = 16
included do
delegate :line_code,
:original_line_code,
:diff_file,
:diff_line,
:active?,
:created_at_diff?,
to: :first_note
delegate :file_path,
:blob,
:highlighted_diff_lines,
:diff_lines,
to: :diff_file,
allow_nil: true
end
def diff_discussion?
true
end
2018-03-17 18:26:18 +05:30
def file_new_path
first_note.position.new_path
end
def on_merge_request_commit?
for_merge_request? && commit_id.present?
end
2017-08-17 22:00:37 +05:30
# Returns an array of at most 16 highlighted lines above a diff note
2018-12-23 12:14:25 +05:30
def truncated_diff_lines(highlight: true, diff_limit: nil)
2018-03-17 18:26:18 +05:30
return [] if diff_line.nil? && first_note.is_a?(LegacyDiffNote)
2018-12-23 12:14:25 +05:30
diff_limit = [diff_limit, NUMBER_OF_TRUNCATED_DIFF_LINES].compact.min
2017-08-17 22:00:37 +05:30
lines = highlight ? highlighted_diff_lines : diff_lines
2018-03-17 18:26:18 +05:30
2018-12-23 12:14:25 +05:30
initial_line_index = [diff_line.index - diff_limit + 1, 0].max
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
prev_lines = []
2018-03-17 18:26:18 +05:30
lines[initial_line_index..diff_line.index].each do |line|
2017-08-17 22:00:37 +05:30
if line.meta?
prev_lines.clear
else
prev_lines << line
end
end
prev_lines
end
2017-09-10 17:25:29 +05:30
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
2017-08-17 22:00:37 +05:30
end