From ccee2aeac5e878201bca5b6d25d020a31b07d784 Mon Sep 17 00:00:00 2001
From: Phil Hughes <me@iamphill.com>
Date: Tue, 19 Jul 2016 10:26:49 +0100
Subject: [PATCH] Removed duplicate labels from the dropdown on dashboard

---
 app/helpers/issuables_helper.rb                  | 14 +++++---------
 app/views/shared/issuable/_filter.html.haml      |  2 +-
 .../shared/issuable/_label_dropdown.html.haml    |  6 ++----
 app/views/shared/issuable/_sidebar.html.haml     | 16 ++++++++--------
 4 files changed, 16 insertions(+), 22 deletions(-)

diff --git a/app/helpers/issuables_helper.rb b/app/helpers/issuables_helper.rb
index 261747c592b..a1e85570e5d 100644
--- a/app/helpers/issuables_helper.rb
+++ b/app/helpers/issuables_helper.rb
@@ -7,16 +7,16 @@ module IssuablesHelper
     "right-sidebar-#{sidebar_gutter_collapsed? ? 'collapsed' : 'expanded'}"
   end
 
-  def multi_label_name(current_labels, selected_param, default_label)
-    if current_labels.any?
+  def multi_label_name(current_labels, default_label)
+    if !current_labels.nil? && current_labels.any?
       title = current_labels.first.try(:title)
-      if current_labels.count > 1
-        "#{title} +#{current_labels.count - 1} more"
+      if current_labels.size > 1
+        "#{title} +#{current_labels.size - 1} more"
       else
         title
       end
     else
-      selected_param.presence || default_label
+      default_label
     end
   end
 
@@ -66,10 +66,6 @@ module IssuablesHelper
     end
   end
 
-  def selected_labels(project, label_query)
-    Label.where(label_query.merge(project_id: project)).pluck(:title).uniq
-  end
-
   private
 
   def sidebar_gutter_collapsed?
diff --git a/app/views/shared/issuable/_filter.html.haml b/app/views/shared/issuable/_filter.html.haml
index 42b5a6d61d7..2b8c2903e7a 100644
--- a/app/views/shared/issuable/_filter.html.haml
+++ b/app/views/shared/issuable/_filter.html.haml
@@ -24,7 +24,7 @@
           = render "shared/issuable/milestone_dropdown", selected: (@issuable_finder.milestones.first unless @issuable_finder.milestones.nil?), name: :milestone_title, show_any: true, show_upcoming: true
 
         .filter-item.inline.labels-filter
-          = render "shared/issuable/label_dropdown", selected: @issuable_finder.labels, use_id: false, selected_toggle: params[:label_name], data_options: { field_name: "label_name[]" }, show_create: controller.controller_name != "groups"
+          = render "shared/issuable/label_dropdown", selected: @issuable_finder.labels.select(:title).uniq, use_id: false, selected_toggle: params[:label_name], data_options: { field_name: "label_name[]" }, show_create: controller.controller_name != "groups"
 
         .pull-right
           = render 'shared/sort_dropdown'
diff --git a/app/views/shared/issuable/_label_dropdown.html.haml b/app/views/shared/issuable/_label_dropdown.html.haml
index cb50d2bc461..1fe2c1956d8 100644
--- a/app/views/shared/issuable/_label_dropdown.html.haml
+++ b/app/views/shared/issuable/_label_dropdown.html.haml
@@ -15,13 +15,11 @@
 
 - if selected
   - selected.each do |label|
-    - id = label.try(:id) || label
-    - title = label.try(:title) || label
-    = hidden_field_tag data_options[:field_name], use_id ? id : title, id: nil
+    = hidden_field_tag data_options[:field_name], use_id ? label.try(:id) : label.try(:title), id: nil
 .dropdown
   %button.dropdown-menu-toggle.js-label-select.js-multiselect{class: classes.join(' '), type: "button", data: dropdown_data}
     %span.dropdown-toggle-text{ class: ("is-default" if selected.nil? || selected.empty?) }
-      = h(multi_label_name(selected, selected_toggle.to_a.first, "Labels"))
+      = h(multi_label_name(selected, "Labels"))
     = icon('chevron-down')
   .dropdown-menu.dropdown-select.dropdown-menu-paging.dropdown-menu-labels.dropdown-menu-selectable
     = render partial: "shared/issuable/label_page_default", locals: { title: "Filter by label", show_footer: show_footer, show_create: show_create }
diff --git a/app/views/shared/issuable/_sidebar.html.haml b/app/views/shared/issuable/_sidebar.html.haml
index d800bd9fd90..2b0b0c2d3c7 100644
--- a/app/views/shared/issuable/_sidebar.html.haml
+++ b/app/views/shared/issuable/_sidebar.html.haml
@@ -108,30 +108,30 @@
                     .js-due-date-calendar
 
       - if issuable.project.labels.any?
+        - selected_labels = issuable.labels
         .block.labels
           .sidebar-collapsed-icon
             = icon('tags')
             %span
-              = issuable.labels_array.size
+              = selected_labels.size
           .title.hide-collapsed
             Labels
             = icon('spinner spin', class: 'block-loading')
             - if can_edit_issuable
               = link_to 'Edit', '#', class: 'edit-link pull-right'
-          .value.bold.issuable-show-labels.hide-collapsed{ class: ("has-labels" if issuable.labels_array.any?) }
-            - if issuable.labels_array.any?
-              - issuable.labels_array.each do |label|
+          .value.bold.issuable-show-labels.hide-collapsed{ class: ("has-labels" if selected_labels.any?) }
+            - if selected_labels.any?
+              - selected_labels.each do |label|
                 = link_to_label(label, type: issuable.to_ability_name)
             - else
               %span.no-value None
           .selectbox.hide-collapsed
-            - selected_labels = issuable.labels
-            - issuable.labels.each do |label|
+            - selected_labels.each do |label|
               = hidden_field_tag "#{issuable.to_ability_name}[label_names][]", label.id, id: nil
             .dropdown
               %button.dropdown-menu-toggle.js-label-select.js-multiselect.js-label-sidebar-dropdown{type: "button", data: {toggle: "dropdown", default_label: "Labels", field_name: "#{issuable.to_ability_name}[label_names][]", ability_name: issuable.to_ability_name, show_no: "true", show_any: "true", project_id: (@project.id if @project), issue_update: issuable_json_path(issuable), labels: (namespace_project_labels_path(@project.namespace, @project, :json) if @project)}}
-                %span.dropdown-toggle-text{ class: ("is-default" if issuable.labels.empty?)}
-                  = h(multi_label_name(selected_labels, selected_labels.first, "Labels"))
+                %span.dropdown-toggle-text{ class: ("is-default" if selected_labels.empty?)}
+                  = h(multi_label_name(selected_labels, "Labels"))
                 = icon('chevron-down')
               .dropdown-menu.dropdown-select.dropdown-menu-paging.dropdown-menu-labels.dropdown-menu-selectable
                 = render partial: "shared/issuable/label_page_default"
-- 
GitLab