2018-12-13 14:17:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Suggestion < ApplicationRecord
|
2020-12-16 10:10:18 -05:00
|
|
|
include Importable
|
2019-03-15 13:35:34 -04:00
|
|
|
include Suggestible
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
belongs_to :note, inverse_of: :suggestions
|
2020-12-16 10:10:18 -05:00
|
|
|
validates :note, presence: true, unless: :importing?
|
2018-12-13 14:17:19 -05:00
|
|
|
validates :commit_id, presence: true, if: :applied?
|
|
|
|
|
2019-03-15 13:35:34 -04:00
|
|
|
delegate :position, :noteable, to: :note
|
|
|
|
|
|
|
|
scope :active, -> { where(outdated: false) }
|
|
|
|
|
|
|
|
def diff_file
|
|
|
|
note.latest_diff_file
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
|
|
|
def project
|
|
|
|
noteable.source_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def branch
|
|
|
|
noteable.source_branch
|
|
|
|
end
|
|
|
|
|
2019-01-10 07:38:15 -05:00
|
|
|
def file_path
|
|
|
|
position.file_path
|
|
|
|
end
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
# `from_line_index` and `to_line_index` represents diff/blob line numbers in
|
|
|
|
# index-like way (N-1).
|
|
|
|
def from_line_index
|
|
|
|
from_line - 1
|
|
|
|
end
|
|
|
|
|
2019-03-15 13:35:34 -04:00
|
|
|
def to_line_index
|
|
|
|
to_line - 1
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2019-03-15 13:35:34 -04:00
|
|
|
def appliable?(cached: true)
|
2020-07-03 17:09:31 -04:00
|
|
|
inapplicable_reason(cached: cached).nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def inapplicable_reason(cached: true)
|
|
|
|
strong_memoize("inapplicable_reason_#{cached}") do
|
2020-07-20 14:09:27 -04:00
|
|
|
next _("Can't apply this suggestion.") if applied?
|
|
|
|
next _("This merge request was merged. To apply this suggestion, edit this file directly.") if noteable.merged?
|
|
|
|
next _("This merge request is closed. To apply this suggestion, edit this file directly.") if noteable.closed?
|
|
|
|
next _("Can't apply as the source branch was deleted.") unless noteable.source_branch_exists?
|
|
|
|
next outdated_reason if outdated?(cached: cached) || !note.active?
|
|
|
|
next _("This suggestion already matches its content.") unless different_content?
|
2021-11-10 16:09:47 -05:00
|
|
|
next _("This file was modified for readability, and can't accept suggestions. Edit it directly.") if file_path.end_with? "ipynb"
|
2020-07-03 17:09:31 -04:00
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2019-03-15 13:35:34 -04:00
|
|
|
# Overwrites outdated column
|
|
|
|
def outdated?(cached: true)
|
|
|
|
return super() if cached
|
|
|
|
return true unless diff_file
|
|
|
|
|
|
|
|
from_content != fetch_from_content
|
|
|
|
end
|
|
|
|
|
2020-07-03 17:09:31 -04:00
|
|
|
def single_line?
|
2020-08-12 02:09:53 -04:00
|
|
|
lines_above == 0 && lines_below == 0
|
2020-07-03 17:09:31 -04:00
|
|
|
end
|
|
|
|
|
2019-03-15 13:35:34 -04:00
|
|
|
def target_line
|
|
|
|
position.new_line
|
|
|
|
end
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def different_content?
|
|
|
|
from_content != to_content
|
|
|
|
end
|
2020-07-20 14:09:27 -04:00
|
|
|
|
|
|
|
def outdated_reason
|
|
|
|
if single_line?
|
|
|
|
_("Can't apply as this line was changed in a more recent version.")
|
|
|
|
else
|
|
|
|
_("Can't apply as these lines were changed in a more recent version.")
|
|
|
|
end
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|