2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
module Boards
|
|
|
|
module Lists
|
2018-03-17 18:26:18 +05:30
|
|
|
class CreateService < Boards::BaseService
|
2018-11-08 19:23:39 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def execute(board)
|
2016-09-13 17:45:13 +05:30
|
|
|
List.transaction do
|
2018-11-08 19:23:39 +05:30
|
|
|
target = target(board)
|
2016-11-03 12:29:30 +05:30
|
|
|
position = next_position(board)
|
2018-11-08 19:23:39 +05:30
|
|
|
create_list(board, type, target, position)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def type
|
|
|
|
:label
|
|
|
|
end
|
|
|
|
|
|
|
|
def target(board)
|
|
|
|
strong_memoize(:target) do
|
|
|
|
available_labels_for(board).find(params[:label_id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def available_labels_for(board)
|
2018-05-09 12:01:36 +05:30
|
|
|
options = { include_ancestor_groups: true }
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
if board.group_board?
|
2018-05-09 12:01:36 +05:30
|
|
|
options.merge!(group_id: parent.id, only_group_labels: true)
|
2018-03-27 19:54:05 +05:30
|
|
|
else
|
2018-05-09 12:01:36 +05:30
|
|
|
options[:project_id] = parent.id
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
LabelsFinder.new(current_user, options).execute
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def next_position(board)
|
2016-09-13 17:45:13 +05:30
|
|
|
max_position = board.lists.movable.maximum(:position)
|
|
|
|
max_position.nil? ? 0 : max_position.succ
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def create_list(board, type, target, position)
|
|
|
|
board.lists.create(type => target, list_type: type, position: position)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|