Commit 6a6356a9 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Handle moving to start/end with one gap left

When moving to the start / end, the min / max position must also be
available to position the new item to
parent 2ca202df
......@@ -297,13 +297,13 @@ module RelativePositioning
if max_pos.nil?
self.relative_position = START_POSITION
elsif gap_too_small?(max_pos, MAX_POSITION)
elsif gap_too_small?(max_pos, MAX_POSITION + 1)
max = relative_siblings.order(Gitlab::Database.nulls_last_order('relative_position', 'DESC')).first
max.move_sequence_before(true)
max.reset
self.relative_position = self.class.position_between(max.relative_position, MAX_POSITION)
self.relative_position = self.class.position_between(max.relative_position, MAX_POSITION + 1)
else
self.relative_position = self.class.position_between(max_pos, MAX_POSITION)
self.relative_position = self.class.position_between(max_pos, MAX_POSITION + 1)
end
end
......@@ -312,13 +312,13 @@ module RelativePositioning
if min_pos.nil?
self.relative_position = START_POSITION
elsif gap_too_small?(min_pos, MIN_POSITION)
elsif gap_too_small?(min_pos, MIN_POSITION - 1)
min = relative_siblings.order(Gitlab::Database.nulls_last_order('relative_position', 'ASC')).first
min.move_sequence_after(true)
min.reset
self.relative_position = self.class.position_between(MIN_POSITION, min.relative_position)
self.relative_position = self.class.position_between(MIN_POSITION - 1, min.relative_position)
else
self.relative_position = self.class.position_between(MIN_POSITION, min_pos)
self.relative_position = self.class.position_between(MIN_POSITION - 1, min_pos)
end
end
......
......@@ -512,6 +512,14 @@ RSpec.shared_examples 'a class that supports relative positioning' do
expect(new_item.relative_position).to be < item2.relative_position
end
it 'positions the item at MIN_POSITION when there is only one space left' do
item2.update!(relative_position: RelativePositioning::MIN_POSITION + 1)
new_item.move_to_start
expect(new_item.relative_position).to eq RelativePositioning::MIN_POSITION
end
it 'rebalances when there is already an item at the MIN_POSITION' do
item2.update!(relative_position: RelativePositioning::MIN_POSITION)
......@@ -549,6 +557,14 @@ RSpec.shared_examples 'a class that supports relative positioning' do
expect(new_item.relative_position).to be > item2.relative_position
end
it 'positions the item at MAX_POSITION when there is only one space left' do
item2.update!(relative_position: RelativePositioning::MAX_POSITION - 1)
new_item.move_to_end
expect(new_item.relative_position).to eq RelativePositioning::MAX_POSITION
end
it 'rebalances when there is already an item at the MAX_POSITION' do
item2.update!(relative_position: RelativePositioning::MAX_POSITION)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment