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

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DiffPositionableNote

#active?, #diff_refs_match_commit, #on_file?, #on_image?, #on_text?, #set_original_position, #should_update_position?, #supported?, #update_position

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



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

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

.preload_author(draft_notes) ⇒ Object



110
111
112
# File 'app/models/draft_note.rb', line 110

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

Instance Method Details

#commitObject



124
125
126
# File 'app/models/draft_note.rb', line 124

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

#diff_fileObject



114
115
116
117
118
119
120
121
122
# File 'app/models/draft_note.rb', line 114

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)


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

def emoji_awardable?
  false
end

#find_line_codeObject



96
97
98
# File 'app/models/draft_note.rb', line 96

def find_line_code
  write_attribute(:line_code, diff_file&.line_code_for_position(original_position))
end

#for_commit?Boolean

Returns:

  • (Boolean)


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

def for_commit?
  commit_id.present?
end

#importing?Boolean

Returns:

  • (Boolean)


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

def importing?
  false
end

#line_codeObject



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

def line_code
  super.presence || find_line_code
end

#noteableObject



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

def noteable
  merge_request
end

#noteable_idObject

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



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

def noteable_id
  merge_request_id
end

#noteable_typeObject



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

def noteable_type
  "MergeRequest"
end

#on_diff?Boolean

Returns:

  • (Boolean)


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

def on_diff?
  position&.complete?
end

#projectObject



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

def project
  merge_request.target_project
end

#publish_paramsObject



100
101
102
103
104
105
106
107
108
# File 'app/models/draft_note.rb', line 100

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



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

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

#resolvable?Boolean

Returns:

  • (Boolean)


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

def resolvable?
  false
end

#typeObject



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

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

  'Note'
end