Commit 9727255a authored by Mark Chao's avatar Mark Chao

Refactor Groups::AutocompleteService#labels

parent a3cfd4b4
......@@ -6,7 +6,7 @@ class Groups::AutocompleteSourcesController < Groups::ApplicationController
end
def labels
render json: @autocomplete_service.labels(target)
render json: @autocomplete_service.labels_as_hash(target)
end
def epics
......
module Groups
class AutocompleteService < Groups::BaseService
def labels(target = nil)
labels = LabelsFinder.new(
def labels_as_hash(target = nil)
available_labels = LabelsFinder.new(
current_user,
group_id: group.id,
include_ancestor_groups: true,
only_group_labels: true
).execute.select([:color, :title])
).execute
return labels unless target&.respond_to?(:labels)
hashes = available_labels.as_json(only: [:title, :color])
issuable_label_titles = target.labels.pluck(:title)
if issuable_label_titles
labels = labels.as_json(only: [:title, :color])
issuable_label_titles.each do |issuable_label_title|
found_label = labels.find { |label| label['title'] == issuable_label_title }
found_label[:set] = true if found_label
if target&.respond_to?(:labels)
if already_set_labels = available_labels & target.labels
titles = already_set_labels.map(&:title)
hashes.each do |hash|
if titles.include?(hash['title'])
hash[:set] = true
end
end
end
end
labels
hashes
end
def epics
......
......@@ -10,12 +10,10 @@ describe Groups::AutocompleteService do
create(:group_member, group: group, user: user)
end
def user_to_autocompletable(user)
{
username: user.username,
name: user.name,
avatar_url: user.avatar_url
}
def expect_labels_to_equal(labels, expected_labels)
expect(labels.size).to eq(expected_labels.size)
extract_title = lambda { |label| label['title'] }
expect(labels.map(&extract_title)).to eq(expected_labels.map(&extract_title))
end
describe '#labels' do
......@@ -26,12 +24,10 @@ describe Groups::AutocompleteService do
it 'returns labels from own group and ancestor groups' do
service = described_class.new(group, user)
result = service.labels
results = service.labels_as_hash
expected_labels = [label1, label2, parent_group_label]
expect(result.size).to eq(3)
expect(result.map(&:title)).to contain_exactly(*expected_labels.map(&:title))
expect_labels_to_equal(results, expected_labels)
end
context 'some labels are already assigned' do
......@@ -41,15 +37,18 @@ describe Groups::AutocompleteService do
it 'marks already assigned as set' do
service = described_class.new(group, user)
result = service.labels(epic)
results = service.labels_as_hash(epic)
expected_labels = [label1, label2, parent_group_label]
expect(result.size).to eq(3)
expect(result.map { |label| label['title'] }).to contain_exactly(*expected_labels.map(&:title))
expect_labels_to_equal(results, expected_labels)
epic.labels.each do |assigned_label|
expect(result.find { |label| label['title'] == assigned_label.title }[:set]).to eq(true)
assigned_label_titles = epic.labels.map(&:title)
results.each do |hash|
if assigned_label_titles.include?(hash['title'])
expect(hash[:set]).to eq(true)
else
expect(hash.key?(:set)).to eq(false)
end
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment