Commit 6d59a7a3 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Sort move issue projects dropdown by star count

When a search term matches many projects, it is hard to find the
intended project because it is just sorted by name.

Ideally we would match by best match but that is not possible right
now. So we sort by star count for now.
parent 2ca22554
......@@ -28,7 +28,8 @@ module Autocomplete
.optionally_search(search, include_namespace: true)
.excluding_project(project_id)
.eager_load_namespace_and_owner
.sorted_by_name_asc_limited(LIMIT)
.sorted_by_stars_desc
.limit(LIMIT) # rubocop: disable CodeReuse/ActiveRecord
end
end
end
......@@ -415,7 +415,6 @@ class Project < ApplicationRecord
scope :sorted_by_activity, -> { reorder(Arel.sql("GREATEST(COALESCE(last_activity_at, '1970-01-01'), COALESCE(last_repository_updated_at, '1970-01-01')) DESC")) }
scope :sorted_by_stars_desc, -> { reorder(self.arel_table['star_count'].desc) }
scope :sorted_by_stars_asc, -> { reorder(self.arel_table['star_count'].asc) }
scope :sorted_by_name_asc_limited, ->(limit) { reorder(name: :asc).limit(limit) }
# Sometimes queries (e.g. using CTEs) require explicit disambiguation with table name
scope :projects_order_id_desc, -> { reorder(self.arel_table['id'].desc) }
......
---
title: Sort the project dropdown by star count when moving issues
merge_request: 29766
author:
type: changed
......@@ -62,19 +62,20 @@ describe Autocomplete::MoveToProjectFinder do
expect(finder.execute.to_a).to eq([other_reporter_project])
end
it 'returns a page of projects ordered by name' do
it 'returns a page of projects ordered by star count' do
stub_const('Autocomplete::MoveToProjectFinder::LIMIT', 2)
projects = create_list(:project, 3) do |project|
project.add_developer(user)
end
projects = [
create(:project, namespace: user.namespace, star_count: 1),
create(:project, namespace: user.namespace, star_count: 5),
create(:project, namespace: user.namespace)
]
finder = described_class.new(user, project_id: project.id)
page = finder.execute.to_a
expected_projects = projects.sort_by(&:name).first(2)
expect(page.length).to eq(2)
expect(page).to eq(expected_projects)
expect(page).to eq([projects[1], projects[0]])
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