diff --git a/app/finders/labels_finder.rb b/app/finders/labels_finder.rb index 6de9eb89468..1427cdaa382 100644 --- a/app/finders/labels_finder.rb +++ b/app/finders/labels_finder.rb @@ -71,7 +71,7 @@ class LabelsFinder < UnionFinder end def projects? - params[:project_ids].present? + params[:project_ids] end def only_group_labels? diff --git a/app/helpers/issuables_helper.rb b/app/helpers/issuables_helper.rb index 2668cf78afe..520a875238c 100644 --- a/app/helpers/issuables_helper.rb +++ b/app/helpers/issuables_helper.rb @@ -304,6 +304,12 @@ module IssuablesHelper issuable.model_name.human.downcase end + def selected_labels + Array(params[:label_name]).map do |label_name| + Label.new(title: label_name) + end + 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 8442d7ff4a2..7704c88905b 100644 --- a/app/views/shared/issuable/_filter.html.haml +++ b/app/views/shared/issuable/_filter.html.haml @@ -22,7 +22,7 @@ = render "shared/issuable/milestone_dropdown", selected: finder.milestones.try(:first), name: :milestone_title, show_any: true, show_upcoming: true, show_started: true .filter-item.inline.labels-filter - = render "shared/issuable/label_dropdown", selected: finder.labels.select(:title).uniq, use_id: false, selected_toggle: params[:label_name], data_options: { field_name: "label_name[]" } + = render "shared/issuable/label_dropdown", selected: selected_labels, use_id: false, selected_toggle: params[:label_name], data_options: { field_name: "label_name[]" } - if issuable_filter_present? .filter-item.inline.reset-filters diff --git a/changelogs/unreleased/issue_37143_2.yml b/changelogs/unreleased/issue_37143_2.yml new file mode 100644 index 00000000000..38125f666b2 --- /dev/null +++ b/changelogs/unreleased/issue_37143_2.yml @@ -0,0 +1,5 @@ +--- +title: Remove unecessary query from labels filter +merge_request: +author: +type: performance diff --git a/spec/helpers/issuables_helper_spec.rb b/spec/helpers/issuables_helper_spec.rb index d601cbdb39b..c8d64d64cf4 100644 --- a/spec/helpers/issuables_helper_spec.rb +++ b/spec/helpers/issuables_helper_spec.rb @@ -192,4 +192,33 @@ describe IssuablesHelper do expect(JSON.parse(helper.issuable_initial_data(issue))).to eq(expected_data) end end + + describe '#selected_labels' do + context 'if label_name param is a string' do + it 'returns a new label with title' do + allow(helper).to receive(:params) + .and_return(ActionController::Parameters.new(label_name: 'test label')) + + labels = helper.selected_labels + + expect(labels).to be_an(Array) + expect(labels.size).to eq(1) + expect(labels.first.title).to eq('test label') + end + end + + context 'if label_name param is an array' do + it 'returns a new label with title for each element' do + allow(helper).to receive(:params) + .and_return(ActionController::Parameters.new(label_name: ['test label 1', 'test label 2'])) + + labels = helper.selected_labels + + expect(labels).to be_an(Array) + expect(labels.size).to eq(2) + expect(labels.first.title).to eq('test label 1') + expect(labels.second.title).to eq('test label 2') + end + end + end end