2020-04-14 11:09:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DiffNotePosition < ApplicationRecord
|
|
|
|
belongs_to :note
|
2020-04-16 05:09:37 -04:00
|
|
|
attr_accessor :line_range
|
2020-04-14 11:09:44 -04:00
|
|
|
|
|
|
|
enum diff_content_type: {
|
|
|
|
text: 0,
|
|
|
|
image: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
enum diff_type: {
|
|
|
|
head: 0
|
|
|
|
}
|
|
|
|
|
|
|
|
def position
|
|
|
|
Gitlab::Diff::Position.new(
|
|
|
|
old_path: old_path,
|
|
|
|
new_path: new_path,
|
|
|
|
old_line: old_line,
|
|
|
|
new_line: new_line,
|
|
|
|
position_type: diff_content_type,
|
|
|
|
diff_refs: Gitlab::Diff::DiffRefs.new(
|
|
|
|
base_sha: base_sha,
|
|
|
|
start_sha: start_sha,
|
|
|
|
head_sha: head_sha
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def position=(position)
|
2020-04-15 08:09:18 -04:00
|
|
|
assign_attributes(self.class.position_to_attrs(position))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_or_update_for(note, params)
|
|
|
|
attrs = position_to_attrs(params[:position])
|
|
|
|
attrs.merge!(params.slice(:diff_type, :line_code))
|
|
|
|
attrs[:note_id] = note.id
|
|
|
|
|
|
|
|
upsert(attrs, unique_by: [:note_id, :diff_type])
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.position_to_attrs(position)
|
2020-04-14 11:09:44 -04:00
|
|
|
position_attrs = position.to_h
|
|
|
|
position_attrs[:diff_content_type] = position_attrs.delete(:position_type)
|
2020-04-16 05:09:37 -04:00
|
|
|
position_attrs.delete(:line_range)
|
2020-04-15 08:09:18 -04:00
|
|
|
position_attrs
|
2020-04-14 11:09:44 -04:00
|
|
|
end
|
|
|
|
end
|