diff options
-rw-r--r-- | app/helpers/labels_helper.rb | 4 | ||||
-rw-r--r-- | app/views/projects/issues/_issue.html.haml | 2 | ||||
-rw-r--r-- | app/views/projects/merge_requests/_merge_request.html.haml | 2 | ||||
-rw-r--r-- | changelogs/unreleased/dz-sort-labels-alphabetically.yml | 5 | ||||
-rw-r--r-- | spec/features/issuables/issuable_list_spec.rb | 24 | ||||
-rw-r--r-- | spec/helpers/labels_helper_spec.rb | 13 |
6 files changed, 48 insertions, 2 deletions
diff --git a/app/helpers/labels_helper.rb b/app/helpers/labels_helper.rb index 39f661b5f0c..bd53add80ca 100644 --- a/app/helpers/labels_helper.rb +++ b/app/helpers/labels_helper.rb @@ -227,6 +227,10 @@ module LabelsHelper "#{action} at #{level} level" end + def labels_sorted_by_title(labels) + labels.sort_by(&:title) + end + # Required for Banzai::Filter::LabelReferenceFilter module_function :render_colored_label, :text_color_for_bg, :escape_once end diff --git a/app/views/projects/issues/_issue.html.haml b/app/views/projects/issues/_issue.html.haml index 31c72f2f759..ce7c7091c93 100644 --- a/app/views/projects/issues/_issue.html.haml +++ b/app/views/projects/issues/_issue.html.haml @@ -36,7 +36,7 @@ = issue.due_date.to_s(:medium) - if issue.labels.any? - - issue.labels.each do |label| + - labels_sorted_by_title(issue.labels).each do |label| = link_to_label(label, subject: issue.project, css_class: 'label-link') .issuable-meta diff --git a/app/views/projects/merge_requests/_merge_request.html.haml b/app/views/projects/merge_requests/_merge_request.html.haml index ac29cd8f679..90916191d97 100644 --- a/app/views/projects/merge_requests/_merge_request.html.haml +++ b/app/views/projects/merge_requests/_merge_request.html.haml @@ -34,7 +34,7 @@ = merge_request.target_branch - if merge_request.labels.any? - - merge_request.labels.each do |label| + - labels_sorted_by_title(merge_request.labels).each do |label| = link_to_label(label, subject: merge_request.project, type: :merge_request, css_class: 'label-link') .issuable-meta diff --git a/changelogs/unreleased/dz-sort-labels-alphabetically.yml b/changelogs/unreleased/dz-sort-labels-alphabetically.yml new file mode 100644 index 00000000000..acfde3de999 --- /dev/null +++ b/changelogs/unreleased/dz-sort-labels-alphabetically.yml @@ -0,0 +1,5 @@ +--- +title: Sort labels alphabetically on issues and merge requests list +merge_request: 25470 +author: +type: changed diff --git a/spec/features/issuables/issuable_list_spec.rb b/spec/features/issuables/issuable_list_spec.rb index 2f45ef856a5..7b6e9cd66b2 100644 --- a/spec/features/issuables/issuable_list_spec.rb +++ b/spec/features/issuables/issuable_list_spec.rb @@ -28,6 +28,22 @@ describe 'issuable list' do expect(first('.fa-thumbs-down').find(:xpath, '..')).to have_content(1) expect(first('.fa-comments').find(:xpath, '..')).to have_content(2) end + + it 'sorts labels alphabetically' do + label1 = create(:label, project: project, title: 'a') + label2 = create(:label, project: project, title: 'z') + label3 = create(:label, project: project, title: 'X') + label4 = create(:label, project: project, title: 'B') + issuable = create_issuable(issuable_type) + issuable.labels << [label1, label2, label3, label4] + + visit_issuable_list(issuable_type) + + expect(all('.label-link')[0].text).to have_content('B') + expect(all('.label-link')[1].text).to have_content('X') + expect(all('.label-link')[2].text).to have_content('a') + expect(all('.label-link')[3].text).to have_content('z') + end end it "counts merge requests closing issues icons for each issue" do @@ -45,6 +61,14 @@ describe 'issuable list' do end end + def create_issuable(issuable_type) + if issuable_type == :issue + create(:issue, project: project) + else + create(:merge_request, source_project: project) + end + end + def create_issuables(issuable_type) 3.times do |n| issuable = diff --git a/spec/helpers/labels_helper_spec.rb b/spec/helpers/labels_helper_spec.rb index c04f679bcf0..012678db9c2 100644 --- a/spec/helpers/labels_helper_spec.rb +++ b/spec/helpers/labels_helper_spec.rb @@ -236,4 +236,17 @@ describe LabelsHelper do expect(labels_filter_path(format: :json)).to eq(dashboard_labels_path(format: :json)) end end + + describe 'labels_sorted_by_title' do + it 'sorts labels alphabetically' do + label1 = double(:label, title: 'a') + label2 = double(:label, title: 'B') + label3 = double(:label, title: 'c') + label4 = double(:label, title: 'D') + labels = [label1, label2, label3, label4] + + expect(labels_sorted_by_title(labels)) + .to match_array([label2, label4, label1, label3]) + end + end end |