Class: Suggestion

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

Constant Summary

Constants included from Suggestible

Suggestible::MAX_LINES_CONTEXT

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from Importable

#importing, #user_contributions

Instance Method Summary collapse

Methods included from Suggestible

#diff_lines, #fetch_from_content, #from_line, #to_line

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, 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 Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Instance Method Details

#appliable?(cached: true) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#branchObject



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

def branch
  noteable.source_branch
end

#diff_fileObject



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

def diff_file
  note.latest_diff_file
end

#file_pathObject



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

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



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

def from_line_index
  from_line - 1
end

#inapplicable_reason(cached: true) ⇒ Object



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

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)


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

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

  from_content != fetch_from_content
end

#single_line?Boolean

Returns:

  • (Boolean)


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

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

#source_projectObject



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

def source_project
  noteable.source_project
end

#target_lineObject



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

def target_line
  position.new_line
end

#target_projectObject



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

def target_project
  noteable.target_project
end

#to_line_indexObject



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

def to_line_index
  to_line - 1
end