2017-02-01 13:41:01 -05:00
|
|
|
module RelativePositioning
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-02-27 06:40:26 -05:00
|
|
|
MIN_POSITION = 0
|
|
|
|
MAX_POSITION = Gitlab::Database::MAX_INT_VALUE
|
2017-02-01 13:41:01 -05:00
|
|
|
|
2017-03-07 12:57:24 -05:00
|
|
|
included do
|
|
|
|
after_save :save_positionable_neighbours
|
|
|
|
end
|
|
|
|
|
2017-02-01 13:41:01 -05:00
|
|
|
def min_relative_position
|
2017-02-27 06:16:26 -05:00
|
|
|
self.class.in_projects(project.id).minimum(:relative_position)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def max_relative_position
|
2017-02-27 06:16:26 -05:00
|
|
|
self.class.in_projects(project.id).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
|
|
|
|
prev_pos = self.class.
|
|
|
|
in_projects(project.id).
|
|
|
|
where('relative_position < ?', self.relative_position).
|
|
|
|
maximum(:relative_position)
|
|
|
|
end
|
|
|
|
|
|
|
|
prev_pos || MIN_POSITION
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_relative_position
|
|
|
|
next_pos = nil
|
|
|
|
|
|
|
|
if self.relative_position
|
|
|
|
next_pos = self.class.
|
|
|
|
in_projects(project.id).
|
|
|
|
where('relative_position > ?', self.relative_position).
|
|
|
|
minimum(:relative_position)
|
|
|
|
end
|
|
|
|
|
|
|
|
next_pos || MAX_POSITION
|
|
|
|
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
|
|
|
|
|
|
|
pos_before = before.relative_position
|
|
|
|
pos_after = after.relative_position
|
|
|
|
|
2017-03-06 11:08:18 -05:00
|
|
|
if pos_after && (pos_before == pos_after)
|
|
|
|
self.relative_position = pos_before
|
2017-03-07 12:57:24 -05:00
|
|
|
before.move_before(self)
|
|
|
|
after.move_after(self)
|
|
|
|
|
|
|
|
@positionable_neighbours = [before, after]
|
2017-02-01 13:41:01 -05:00
|
|
|
else
|
2017-03-06 11:08:18 -05:00
|
|
|
self.relative_position = position_between(pos_before, pos_after)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-07 12:57:24 -05:00
|
|
|
def move_before(after)
|
|
|
|
self.relative_position = position_between(after.prev_relative_position, after.relative_position)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
|
2017-03-07 12:57:24 -05:00
|
|
|
def move_after(before)
|
|
|
|
self.relative_position = position_between(before.relative_position, before.next_relative_position)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
|
2017-03-07 12:57:24 -05:00
|
|
|
def move_to_end
|
|
|
|
self.relative_position = position_between(max_relative_position, MAX_POSITION)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-03-03 13:10:07 -05:00
|
|
|
# This method takes two integer values (positions) and
|
|
|
|
# calculates some random position between them. The range is huge as
|
|
|
|
# the maximum integer value is 2147483647. Ideally, the calculated value would be
|
|
|
|
# exactly between those terminating values, but this will introduce possibility of a race condition
|
|
|
|
# so two or more issues can get the same value, we want to avoid that and we also want to avoid
|
|
|
|
# using a lock here. If we have two issues with distance more than one thousand, we are OK.
|
|
|
|
# Given the huge range of possible values that integer can fit we shoud never face a problem.
|
2017-02-01 13:41:01 -05:00
|
|
|
def position_between(pos_before, pos_after)
|
2017-03-02 11:09:48 -05:00
|
|
|
pos_before ||= MIN_POSITION
|
|
|
|
pos_after ||= MAX_POSITION
|
|
|
|
|
2017-02-01 13:41:01 -05:00
|
|
|
pos_before, pos_after = [pos_before, pos_after].sort
|
|
|
|
|
2017-02-27 06:40:26 -05:00
|
|
|
rand(pos_before.next..pos_after.pred)
|
2017-02-01 13:41:01 -05:00
|
|
|
end
|
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-02-01 13:41:01 -05:00
|
|
|
end
|