2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-01 13:41:01 -05:00
|
|
|
module RelativePositioning
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-02-27 06:40:26 -05:00
|
|
|
MIN_POSITION = 0
|
2017-03-10 10:12:31 -05:00
|
|
|
START_POSITION = Gitlab::Database::MAX_INT_VALUE / 2
|
2017-02-27 06:40:26 -05:00
|
|
|
MAX_POSITION = Gitlab::Database::MAX_INT_VALUE
|
2017-03-10 12:04:37 -05:00
|
|
|
IDEAL_DISTANCE = 500
|
2017-02-01 13:41:01 -05:00
|
|
|
|
2017-03-07 12:57:24 -05:00
|
|
|
included do
|
|
|
|
after_save :save_positionable_neighbours
|
|
|
|
end
|
|
|
|
|
2018-09-10 13:26:33 -04:00
|
|
|
class_methods do
|
|
|
|
def move_to_end(objects)
|
|
|
|
parent_ids = objects.map(&:parent_ids).flatten.uniq
|
|
|
|
max_relative_position = in_parents(parent_ids).maximum(:relative_position) || START_POSITION
|
|
|
|
objects = objects.reject(&:relative_position)
|
|
|
|
|
|
|
|
self.transaction do
|
|
|
|
objects.each do |object|
|
|
|
|
relative_position = position_between(max_relative_position, MAX_POSITION)
|
|
|
|
object.relative_position = relative_position
|
|
|
|
max_relative_position = relative_position
|
|
|
|
object.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This method takes two integer values (positions) and
|
|
|
|
# calculates the position between them. The range is huge as
|
|
|
|
# the maximum integer value is 2147483647. We are incrementing position by IDEAL_DISTANCE * 2 every time
|
|
|
|
# when we have enough space. If distance is less then IDEAL_DISTANCE we are calculating an average number
|
|
|
|
def position_between(pos_before, pos_after)
|
|
|
|
pos_before ||= MIN_POSITION
|
|
|
|
pos_after ||= MAX_POSITION
|
|
|
|
|
|
|
|
pos_before, pos_after = [pos_before, pos_after].sort
|
|
|
|
|
|
|
|
halfway = (pos_after + pos_before) / 2
|
|
|
|
distance_to_halfway = pos_after - halfway
|
|
|
|
|
|
|
|
if distance_to_halfway < IDEAL_DISTANCE
|
|
|
|
halfway
|
|
|
|
else
|
|
|
|
if pos_before == MIN_POSITION
|
|
|
|
pos_after - IDEAL_DISTANCE
|
|
|
|
elsif pos_after == MAX_POSITION
|
|
|
|
pos_before + IDEAL_DISTANCE
|
|
|
|
else
|
|
|
|
halfway
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-04 07:29:48 -05:00
|
|
|
def min_relative_position
|
|
|
|
self.class.in_parents(parent_ids).minimum(:relative_position)
|
2017-08-28 17:56:49 -04:00
|
|
|
end
|
|
|
|
|
2017-02-01 13:41:01 -05:00
|
|
|
def max_relative_position
|
2018-01-04 07:29:48 -05:00
|
|
|
self.class.in_parents(parent_ids).maximum(:relative_position)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
|
2017-03-07 12:57:24 -05:00
|
|
|
def prev_relative_position
|
|
|
|
prev_pos = nil
|
|
|
|
|
|
|
|
if self.relative_position
|
2017-06-21 09:48:12 -04:00
|
|
|
prev_pos = self.class
|
2018-01-04 07:29:48 -05:00
|
|
|
.in_parents(parent_ids)
|
2017-06-21 09:48:12 -04:00
|
|
|
.where('relative_position < ?', self.relative_position)
|
|
|
|
.maximum(:relative_position)
|
2017-03-07 12:57:24 -05:00
|
|
|
end
|
|
|
|
|
2017-03-10 10:12:31 -05:00
|
|
|
prev_pos
|
2017-03-07 12:57:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def next_relative_position
|
|
|
|
next_pos = nil
|
|
|
|
|
|
|
|
if self.relative_position
|
2017-06-21 09:48:12 -04:00
|
|
|
next_pos = self.class
|
2018-01-04 07:29:48 -05:00
|
|
|
.in_parents(parent_ids)
|
2017-06-21 09:48:12 -04:00
|
|
|
.where('relative_position > ?', self.relative_position)
|
|
|
|
.minimum(:relative_position)
|
2017-03-07 12:57:24 -05:00
|
|
|
end
|
|
|
|
|
2017-03-10 10:12:31 -05:00
|
|
|
next_pos
|
2017-03-07 12:57:24 -05:00
|
|
|
end
|
|
|
|
|
2017-02-01 13:41:01 -05:00
|
|
|
def move_between(before, after)
|
2017-03-07 12:57:24 -05:00
|
|
|
return move_after(before) unless after
|
|
|
|
return move_before(after) unless before
|
2017-02-01 13:41:01 -05:00
|
|
|
|
2017-03-10 12:04:37 -05:00
|
|
|
# If there is no place to insert an issue we need to create one by moving the before issue closer
|
|
|
|
# to its predecessor. This process will recursively move all the predecessors until we have a place
|
|
|
|
if (after.relative_position - before.relative_position) < 2
|
|
|
|
before.move_before
|
2017-11-22 02:50:36 -05:00
|
|
|
@positionable_neighbours = [before] # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-03-10 12:04:37 -05:00
|
|
|
end
|
|
|
|
|
2018-09-10 13:26:33 -04:00
|
|
|
self.relative_position = self.class.position_between(before.relative_position, after.relative_position)
|
2017-03-10 12:04:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def move_after(before = self)
|
2017-02-01 13:41:01 -05:00
|
|
|
pos_before = before.relative_position
|
2017-03-10 12:04:37 -05:00
|
|
|
pos_after = before.next_relative_position
|
2017-02-01 13:41:01 -05:00
|
|
|
|
2017-03-10 12:04:37 -05:00
|
|
|
if before.shift_after?
|
2018-01-04 07:29:48 -05:00
|
|
|
issue_to_move = self.class.in_parents(parent_ids).find_by!(relative_position: pos_after)
|
2017-03-10 12:04:37 -05:00
|
|
|
issue_to_move.move_after
|
2017-11-22 02:50:36 -05:00
|
|
|
@positionable_neighbours = [issue_to_move] # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-03-07 12:57:24 -05:00
|
|
|
|
2017-03-10 12:04:37 -05:00
|
|
|
pos_after = issue_to_move.relative_position
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
|
2018-09-10 13:26:33 -04:00
|
|
|
self.relative_position = self.class.position_between(pos_before, pos_after)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
|
2017-03-10 12:04:37 -05:00
|
|
|
def move_before(after = self)
|
|
|
|
pos_after = after.relative_position
|
|
|
|
pos_before = after.prev_relative_position
|
|
|
|
|
|
|
|
if after.shift_before?
|
2018-01-04 07:29:48 -05:00
|
|
|
issue_to_move = self.class.in_parents(parent_ids).find_by!(relative_position: pos_before)
|
2017-03-10 12:04:37 -05:00
|
|
|
issue_to_move.move_before
|
2017-11-22 02:50:36 -05:00
|
|
|
@positionable_neighbours = [issue_to_move] # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-03-10 12:04:37 -05:00
|
|
|
|
|
|
|
pos_before = issue_to_move.relative_position
|
|
|
|
end
|
|
|
|
|
2018-09-10 13:26:33 -04:00
|
|
|
self.relative_position = self.class.position_between(pos_before, pos_after)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
|
2017-03-07 12:57:24 -05:00
|
|
|
def move_to_end
|
2018-09-10 13:26:33 -04:00
|
|
|
self.relative_position = self.class.position_between(max_relative_position || START_POSITION, MAX_POSITION)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
|
2018-01-04 07:29:48 -05:00
|
|
|
def move_to_start
|
2018-09-10 13:26:33 -04:00
|
|
|
self.relative_position = self.class.position_between(min_relative_position || START_POSITION, MIN_POSITION)
|
2018-01-04 07:29:48 -05:00
|
|
|
end
|
|
|
|
|
2017-03-10 12:04:37 -05:00
|
|
|
# Indicates if there is an issue that should be shifted to free the place
|
|
|
|
def shift_after?
|
|
|
|
next_pos = next_relative_position
|
|
|
|
next_pos && (next_pos - relative_position) == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# Indicates if there is an issue that should be shifted to free the place
|
|
|
|
def shift_before?
|
|
|
|
prev_pos = prev_relative_position
|
|
|
|
prev_pos && (relative_position - prev_pos) == 1
|
|
|
|
end
|
|
|
|
|
2017-02-01 13:41:01 -05:00
|
|
|
private
|
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-03-07 12:57:24 -05:00
|
|
|
def save_positionable_neighbours
|
|
|
|
return unless @positionable_neighbours
|
|
|
|
|
|
|
|
status = @positionable_neighbours.all?(&:save)
|
|
|
|
@positionable_neighbours = nil
|
|
|
|
|
|
|
|
status
|
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|