Commit 56707777 authored by Valery Sizov's avatar Valery Sizov

[Issue sorting] Filling positions preferable in the middle

parent 67686e38
...@@ -2,6 +2,7 @@ module RelativePositioning ...@@ -2,6 +2,7 @@ module RelativePositioning
extend ActiveSupport::Concern extend ActiveSupport::Concern
MIN_POSITION = 0 MIN_POSITION = 0
START_POSITION = Gitlab::Database::MAX_INT_VALUE / 2
MAX_POSITION = Gitlab::Database::MAX_INT_VALUE MAX_POSITION = Gitlab::Database::MAX_INT_VALUE
DISTANCE = 500 DISTANCE = 500
...@@ -9,10 +10,6 @@ module RelativePositioning ...@@ -9,10 +10,6 @@ module RelativePositioning
after_save :save_positionable_neighbours after_save :save_positionable_neighbours
end end
def min_relative_position
self.class.in_projects(project.id).minimum(:relative_position)
end
def max_relative_position def max_relative_position
self.class.in_projects(project.id).maximum(:relative_position) self.class.in_projects(project.id).maximum(:relative_position)
end end
...@@ -27,7 +24,7 @@ module RelativePositioning ...@@ -27,7 +24,7 @@ module RelativePositioning
maximum(:relative_position) maximum(:relative_position)
end end
prev_pos || MIN_POSITION prev_pos
end end
def next_relative_position def next_relative_position
...@@ -40,7 +37,7 @@ module RelativePositioning ...@@ -40,7 +37,7 @@ module RelativePositioning
minimum(:relative_position) minimum(:relative_position)
end end
next_pos || MAX_POSITION next_pos
end end
def move_between(before, after) def move_between(before, after)
...@@ -72,7 +69,7 @@ module RelativePositioning ...@@ -72,7 +69,7 @@ module RelativePositioning
end end
def move_to_end def move_to_end
self.relative_position = position_between(max_relative_position, MAX_POSITION) self.relative_position = position_between(max_relative_position || START_POSITION, MAX_POSITION)
end end
private private
...@@ -87,10 +84,16 @@ module RelativePositioning ...@@ -87,10 +84,16 @@ module RelativePositioning
pos_before, pos_after = [pos_before, pos_after].sort pos_before, pos_after = [pos_before, pos_after].sort
if pos_after - pos_before > DISTANCE * 2 if pos_after - pos_before < DISTANCE * 2
(pos_after + pos_before) / 2
else
if pos_before == MIN_POSITION
pos_after - DISTANCE
elsif pos_after == MAX_POSITION
pos_before + DISTANCE pos_before + DISTANCE
else else
pos_before + (pos_after - pos_before) / 2 (pos_after + pos_before) / 2
end
end end
end end
......
...@@ -12,12 +12,6 @@ describe Issue, 'RelativePositioning' do ...@@ -12,12 +12,6 @@ describe Issue, 'RelativePositioning' do
end end
end end
describe '#min_relative_position' do
it 'returns maximum position' do
expect(issue.min_relative_position).to eq issue.relative_position
end
end
describe '#max_relative_position' do describe '#max_relative_position' do
it 'returns maximum position' do it 'returns maximum position' do
expect(issue.max_relative_position).to eq issue1.relative_position expect(issue.max_relative_position).to eq issue1.relative_position
...@@ -29,8 +23,8 @@ describe Issue, 'RelativePositioning' do ...@@ -29,8 +23,8 @@ describe Issue, 'RelativePositioning' do
expect(issue1.prev_relative_position).to eq issue.relative_position expect(issue1.prev_relative_position).to eq issue.relative_position
end end
it 'returns minimum position if there is no issue above' do it 'returns nil if there is no issue above' do
expect(issue.prev_relative_position).to eq RelativePositioning::MIN_POSITION expect(issue.prev_relative_position).to eq nil
end end
end end
...@@ -39,8 +33,8 @@ describe Issue, 'RelativePositioning' do ...@@ -39,8 +33,8 @@ describe Issue, 'RelativePositioning' do
expect(issue.next_relative_position).to eq issue1.relative_position expect(issue.next_relative_position).to eq issue1.relative_position
end end
it 'returns next position if there is no issue below' do it 'returns nil if there is no issue below' do
expect(issue1.next_relative_position).to eq RelativePositioning::MAX_POSITION expect(issue1.next_relative_position).to eq nil
end end
end end
...@@ -110,15 +104,33 @@ describe Issue, 'RelativePositioning' do ...@@ -110,15 +104,33 @@ describe Issue, 'RelativePositioning' do
expect(issue.relative_position).to be < issue1.relative_position expect(issue.relative_position).to be < issue1.relative_position
end end
it 'positions issue closer to before-issue if distance is big enough' do it 'positions issue in the middle of other two if distance is big enough' do
issue.update relative_position: 100 issue.update relative_position: 6000
issue1.update relative_position: 6000 issue1.update relative_position: 10000
new_issue.move_between(issue, issue1) new_issue.move_between(issue, issue1)
expect(new_issue.relative_position).to eq(100 + RelativePositioning::DISTANCE) expect(new_issue.relative_position).to eq(8000)
end
it 'positions issue closer to the middle if we are at the very top' do
issue1.update relative_position: 6000
new_issue.move_between(nil, issue1)
expect(new_issue.relative_position).to eq(6000 - RelativePositioning::DISTANCE)
end
it 'positions issue closer to the middle if we are at the very bottom' do
issue.update relative_position: 6000
issue1.update relative_position: nil
new_issue.move_between(issue, nil)
expect(new_issue.relative_position).to eq(6000 + RelativePositioning::DISTANCE)
end end
it 'positions issue in the middle of other two if distance is not big enough' do it 'positions issue in the middle of other two if distance is not big enough' do
issue.update relative_position: 100 issue.update relative_position: 100
issue1.update relative_position: 400 issue1.update relative_position: 400
......
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