Class: Suggestion

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

Constant Summary

Constants included from Suggestible

Suggestible::MAX_LINES_CONTEXT

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

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

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Instance Method Details

#appliable?(cached: true) ⇒ Boolean

Returns:

  • (Boolean)

45
46
47
# File 'app/models/suggestion.rb', line 45

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

#branchObject


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

def branch
  noteable.source_branch
end

#diff_fileObject


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

def diff_file
  note.latest_diff_file
end

#file_pathObject


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

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).


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

def from_line_index
  from_line - 1
end

#inapplicable_reason(cached: true) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/suggestion.rb', line 49

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?
    next _("This file was modified for readability, and can't accept suggestions. Edit it directly.") if file_path.end_with? "ipynb"
  end
end

#outdated?(cached: true) ⇒ Boolean

Overwrites outdated column

Returns:

  • (Boolean)

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

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

  from_content != fetch_from_content
end

#single_line?Boolean

Returns:

  • (Boolean)

69
70
71
# File 'app/models/suggestion.rb', line 69

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

#source_projectObject


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

def source_project
  noteable.source_project
end

#target_lineObject


73
74
75
# File 'app/models/suggestion.rb', line 73

def target_line
  position.new_line
end

#target_projectObject


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

def target_project
  noteable.target_project
end

#to_line_indexObject


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

def to_line_index
  to_line - 1
end