gitlab-org--gitlab-foss/app/services/boards/lists/move_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-07-27 19:33:22 -04:00
module Boards
module Lists
2017-08-31 13:48:57 -04:00
class MoveService < Boards::BaseService
def execute(list)
@board = list.board
@old_position = list.position
@new_position = params[:position]
return false unless list.movable?
return false unless valid_move?
2016-07-27 19:33:22 -04:00
list.with_lock do
reorder_intermediate_lists
update_list_position(list)
2016-07-27 19:33:22 -04:00
end
end
private
attr_reader :board, :old_position, :new_position
2016-07-27 19:33:22 -04:00
def valid_move?
new_position.present? && new_position != old_position &&
new_position >= 0 && new_position < board.lists.movable.size
end
2016-07-27 19:33:22 -04:00
def reorder_intermediate_lists
if old_position < new_position
decrement_intermediate_lists
2016-07-27 19:33:22 -04:00
else
increment_intermediate_lists
2016-07-27 19:33:22 -04:00
end
end
# rubocop: disable CodeReuse/ActiveRecord
def decrement_intermediate_lists
board.lists.movable.where('position > ?', old_position)
.where('position <= ?', new_position)
.update_all('position = position - 1')
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def increment_intermediate_lists
board.lists.movable.where('position >= ?', new_position)
.where('position < ?', old_position)
.update_all('position = position + 1')
end
# rubocop: enable CodeReuse/ActiveRecord
def update_list_position(list)
2016-07-27 19:33:22 -04:00
list.update_attribute(:position, new_position)
end
end
end
end