blob: ff29358df86e5d2f773e4448de96f969ba9c4c20 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# frozen_string_literal: true
module Labels
class AvailableLabelsService
attr_reader :current_user, :parent, :params
def initialize(current_user, parent, params)
@current_user = current_user
@parent = parent
@params = params
end
def find_or_create_by_titles(key = :labels, find_only: false)
labels = params.delete(key)
return [] unless labels
labels = labels.split(',').map(&:strip) if labels.is_a?(String)
existing_labels = LabelsFinder.new(current_user, finder_params(labels)).execute.index_by(&:title)
labels.map do |label_name|
label = Labels::FindOrCreateService.new(
current_user,
parent,
include_ancestor_groups: true,
title: label_name,
existing_labels_by_title: existing_labels
).execute(find_only: find_only)
label
end.compact
end
def filter_labels_ids_in_param(key)
ids = Array.wrap(params[key])
return [] if ids.empty?
# rubocop:disable CodeReuse/ActiveRecord
existing_ids = available_labels.id_in(ids).pluck(:id)
# rubocop:enable CodeReuse/ActiveRecord
ids.map(&:to_i) & existing_ids
end
def available_labels
@available_labels ||= LabelsFinder.new(current_user, finder_params).execute
end
private
def finder_params(titles = nil)
finder_params = { include_ancestor_groups: true }
finder_params[:title] = titles if titles
case parent
when Group
finder_params[:group_id] = parent.id
finder_params[:only_group_labels] = true
when Project
finder_params[:project_id] = parent.id
end
finder_params
end
end
end
|