2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Suggestion < ApplicationRecord
|
2019-07-07 11:18:12 +05:30
|
|
|
include Suggestible
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
belongs_to :note, inverse_of: :suggestions
|
|
|
|
validates :note, presence: true
|
|
|
|
validates :commit_id, presence: true, if: :applied?
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
delegate :position, :noteable, to: :note
|
|
|
|
|
|
|
|
scope :active, -> { where(outdated: false) }
|
|
|
|
|
|
|
|
def diff_file
|
|
|
|
note.latest_diff_file
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
def project
|
|
|
|
noteable.source_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def branch
|
|
|
|
noteable.source_branch
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_path
|
|
|
|
position.file_path
|
|
|
|
end
|
|
|
|
|
|
|
|
# `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-07-07 11:18:12 +05:30
|
|
|
def to_line_index
|
|
|
|
to_line - 1
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def appliable?(cached: true)
|
2020-07-28 23:09:34 +05:30
|
|
|
inapplicable_reason(cached: cached).nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def inapplicable_reason(cached: true)
|
|
|
|
strong_memoize("inapplicable_reason_#{cached}") do
|
|
|
|
next :applied if applied?
|
|
|
|
next :merge_request_merged if noteable.merged?
|
|
|
|
next :merge_request_closed if noteable.closed?
|
|
|
|
next :source_branch_deleted unless noteable.source_branch_exists?
|
|
|
|
next :outdated if outdated?(cached: cached) || !note.active?
|
|
|
|
next :same_content unless different_content?
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
# Overwrites outdated column
|
|
|
|
def outdated?(cached: true)
|
|
|
|
return super() if cached
|
|
|
|
return true unless diff_file
|
|
|
|
|
|
|
|
from_content != fetch_from_content
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def single_line?
|
|
|
|
lines_above.zero? && lines_below.zero?
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def target_line
|
|
|
|
position.new_line
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def different_content?
|
|
|
|
from_content != to_content
|
|
|
|
end
|
|
|
|
end
|