Class: DraftNote

Inherits:
ApplicationRecord show all
Includes:
DiffPositionableNote, Gitlab::Utils::StrongMemoize, ShaAttribute, Sortable
Defined in:
app/models/draft_note.rb

Constant Summary collapse

PUBLISH_ATTRS =
%i(noteable_id noteable_type type note).freeze
DIFF_ATTRS =
%i(position original_position change_position commit_id).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from DiffPositionableNote

#active?, #diff_refs_match_commit, #set_original_position, #supported?, #update_position

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 Attribute Details

#commands_changesObject

Attribute used to store quick actions changes and users referenced.


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

def commands_changes
  @commands_changes
end

#rendered_noteObject

Text with quick actions filtered out


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

def rendered_note
  @rendered_note
end

#reviewObject

Returns the value of attribute review


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

def review
  @review
end

#users_referencedObject

Returns the value of attribute users_referenced


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

def users_referenced
  @users_referenced
end

Class Method Details

.positionsObject


33
34
35
36
37
# File 'app/models/draft_note.rb', line 33

def self.positions
  where.not(position: nil)
    .select(:position)
    .map(&:position)
end

.preload_author(draft_notes) ⇒ Object


105
106
107
# File 'app/models/draft_note.rb', line 105

def self.preload_author(draft_notes)
  ActiveRecord::Associations::Preloader.new.preload(draft_notes, { author: :status })
end

Instance Method Details

#commitObject


119
120
121
# File 'app/models/draft_note.rb', line 119

def commit
  @commit ||= project.commit(commit_id) if commit_id.present?
end

#diff_fileObject


109
110
111
112
113
114
115
116
117
# File 'app/models/draft_note.rb', line 109

def diff_file
  strong_memoize(:diff_file) do
    file = original_position&.diff_file(project.repository)

    file&.unfold_diff_lines(original_position)

    file
  end
end

#emoji_awardable?Boolean

Returns:

  • (Boolean)

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

def emoji_awardable?
  false
end

#for_commit?Boolean

Returns:

  • (Boolean)

57
58
59
# File 'app/models/draft_note.rb', line 57

def for_commit?
  commit_id.present?
end

#importing?Boolean

Returns:

  • (Boolean)

61
62
63
# File 'app/models/draft_note.rb', line 61

def importing?
  false
end

#line_codeObject


91
92
93
# File 'app/models/draft_note.rb', line 91

def line_code
  @line_code ||= diff_file&.line_code_for_position(original_position)
end

#noteableObject


49
50
51
# File 'app/models/draft_note.rb', line 49

def noteable
  merge_request
end

#noteable_idObject

noteable_id and noteable_type methods are used to generate discussion_id on Discussion.discussion_id


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

def noteable_id
  merge_request_id
end

#noteable_typeObject


53
54
55
# File 'app/models/draft_note.rb', line 53

def noteable_type
  "MergeRequest"
end

#on_diff?Boolean

Returns:

  • (Boolean)

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

def on_diff?
  position&.complete?
end

#projectObject


39
40
41
# File 'app/models/draft_note.rb', line 39

def project
  merge_request.target_project
end

#publish_paramsObject


95
96
97
98
99
100
101
102
103
# File 'app/models/draft_note.rb', line 95

def publish_params
  attrs = PUBLISH_ATTRS.dup
  attrs.concat(DIFF_ATTRS) if on_diff?
  params = slice(*attrs)
  params[:in_reply_to_discussion_id] = discussion_id if discussion_id.present?
  params[:review_id] = review.id if review.present?

  params
end

#referencesObject


84
85
86
87
88
89
# File 'app/models/draft_note.rb', line 84

def references
  {
    users: users_referenced,
    commands: commands_changes
  }
end

#resolvable?Boolean

Returns:

  • (Boolean)

65
66
67
# File 'app/models/draft_note.rb', line 65

def resolvable?
  false
end

#typeObject


77
78
79
80
81
82
# File 'app/models/draft_note.rb', line 77

def type
  return 'DiffNote' if on_diff?
  return 'DiscussionNote' if discussion_id.present?

  'Note'
end