2019-04-03 18:18:56 +05:30
|
|
|
# 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
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def find_or_create_by_titles(key = :labels, find_only: false)
|
|
|
|
labels = params.delete(key)
|
2019-04-03 18:18:56 +05:30
|
|
|
|
|
|
|
return [] unless labels
|
|
|
|
|
|
|
|
labels = labels.split(',') if labels.is_a?(String)
|
|
|
|
|
|
|
|
labels.map do |label_name|
|
|
|
|
label = Labels::FindOrCreateService.new(
|
|
|
|
current_user,
|
|
|
|
parent,
|
|
|
|
include_ancestor_groups: true,
|
|
|
|
title: label_name.strip,
|
|
|
|
available_labels: available_labels
|
2019-12-04 20:38:33 +05:30
|
|
|
).execute(find_only: find_only)
|
2019-04-03 18:18:56 +05:30
|
|
|
|
|
|
|
label
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_labels_ids_in_param(key)
|
2020-06-23 00:09:42 +05:30
|
|
|
ids = params[key].to_a
|
|
|
|
return [] if ids.empty?
|
2019-04-03 18:18:56 +05:30
|
|
|
|
|
|
|
# rubocop:disable CodeReuse/ActiveRecord
|
2020-06-23 00:09:42 +05:30
|
|
|
existing_ids = available_labels.by_ids(ids).pluck(:id)
|
2019-04-03 18:18:56 +05:30
|
|
|
# rubocop:enable CodeReuse/ActiveRecord
|
2020-06-23 00:09:42 +05:30
|
|
|
ids.map(&:to_i) & existing_ids
|
2019-04-03 18:18:56 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def available_labels
|
|
|
|
@available_labels ||= LabelsFinder.new(current_user, finder_params).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def finder_params
|
|
|
|
params = { include_ancestor_groups: true }
|
|
|
|
|
|
|
|
case parent
|
|
|
|
when Group
|
|
|
|
params[:group_id] = parent.id
|
|
|
|
params[:only_group_labels] = true
|
|
|
|
when Project
|
|
|
|
params[:project_id] = parent.id
|
|
|
|
end
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|