diff options
author | Mark Chao <mchao@gitlab.com> | 2018-07-06 06:37:58 +0300 |
---|---|---|
committer | Mark Chao <mchao@gitlab.com> | 2018-07-06 12:03:39 +0300 |
commit | 389a22e7b72c7514ede515c7c4124719edf2d5a6 (patch) | |
tree | 9c79e94ed535c0a64db67ab36f4363215fff2e9f /app | |
parent | 83f79ced3fd98031194e5667c4d80f66d7987c67 (diff) |
Backport of EE changes in https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/6195
Refactor AutocompleteSources#label:
1. The return value was inconsistent, sometimes returning a hash and sometimes returning an ActiveModel.
2. It was inconsistent with respect to other methods in the class since they all return ActiveModels.
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/projects/autocomplete_sources_controller.rb | 2 | ||||
-rw-r--r-- | app/services/projects/autocomplete_service.rb | 34 |
2 files changed, 20 insertions, 16 deletions
diff --git a/app/controllers/projects/autocomplete_sources_controller.rb b/app/controllers/projects/autocomplete_sources_controller.rb index 992c8ea6992..07627ffb69f 100644 --- a/app/controllers/projects/autocomplete_sources_controller.rb +++ b/app/controllers/projects/autocomplete_sources_controller.rb @@ -14,7 +14,7 @@ class Projects::AutocompleteSourcesController < Projects::ApplicationController end def labels - render json: @autocomplete_service.labels(target) + render json: @autocomplete_service.labels_as_hash(target) end def milestones diff --git a/app/services/projects/autocomplete_service.rb b/app/services/projects/autocomplete_service.rb index aa60661f7f2..9d0eaaf3152 100644 --- a/app/services/projects/autocomplete_service.rb +++ b/app/services/projects/autocomplete_service.rb @@ -20,24 +20,28 @@ module Projects MergeRequestsFinder.new(current_user, project_id: project.id, state: 'opened').execute.select([:iid, :title]) end - def labels(target = nil) - labels = LabelsFinder.new(current_user, project_id: project.id, include_ancestor_groups: true) - .execute.select([:color, :title]) - - return labels unless target&.respond_to?(:labels) - - 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 + def labels_as_hash(target = nil) + available_labels = LabelsFinder.new( + current_user, + project_id: project.id, + include_ancestor_groups: true + ).execute + + label_hashes = available_labels.as_json(only: [:title, :color]) + + if target&.respond_to?(:labels) + already_set_labels = available_labels & target.labels + if already_set_labels.present? + titles = already_set_labels.map(&:title) + label_hashes.each do |hash| + if titles.include?(hash['title']) + hash[:set] = true + end + end end end - labels + label_hashes end def commands(noteable, type) |