Class: Suggestion

Inherits:
ApplicationRecord show all
Includes:
Suggestible
Defined in:
app/models/suggestion.rb

Constant Summary

Constants included from Suggestible

Suggestible::MAX_LINES_CONTEXT

Instance Method Summary collapse

Methods included from Suggestible

#diff_lines, #fetch_from_content, #from_line, #to_line

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Instance Method Details

#appliable?(cached: true) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
# File 'app/models/suggestion.rb', line 40

def appliable?(cached: true)
  inapplicable_reason(cached: cached).nil?
end

#branchObject


22
23
24
# File 'app/models/suggestion.rb', line 22

def branch
  noteable.source_branch
end

#diff_fileObject


14
15
16
# File 'app/models/suggestion.rb', line 14

def diff_file
  note.latest_diff_file
end

#file_pathObject


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

def file_path
  position.file_path
end

#from_line_indexObject

`from_line_index` and `to_line_index` represents diff/blob line numbers in index-like way (N-1).


32
33
34
# File 'app/models/suggestion.rb', line 32

def from_line_index
  from_line - 1
end

#inapplicable_reason(cached: true) ⇒ Object


44
45
46
47
48
49
50
51
52
53
# File 'app/models/suggestion.rb', line 44

def inapplicable_reason(cached: true)
  strong_memoize("inapplicable_reason_#{cached}") do
    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?
  end
end

#outdated?(cached: true) ⇒ Boolean

Overwrites outdated column

Returns:

  • (Boolean)

56
57
58
59
60
61
# File 'app/models/suggestion.rb', line 56

def outdated?(cached: true)
  return super() if cached
  return true unless diff_file

  from_content != fetch_from_content
end

#projectObject


18
19
20
# File 'app/models/suggestion.rb', line 18

def project
  noteable.source_project
end

#single_line?Boolean

Returns:

  • (Boolean)

63
64
65
# File 'app/models/suggestion.rb', line 63

def single_line?
  lines_above == 0 && lines_below == 0
end

#target_lineObject


67
68
69
# File 'app/models/suggestion.rb', line 67

def target_line
  position.new_line
end

#to_line_indexObject


36
37
38
# File 'app/models/suggestion.rb', line 36

def to_line_index
  to_line - 1
end