diff options
author | 🙈 jacopo beschi 🙉 <intrip@gmail.com> | 2018-09-21 17:06:55 +0000 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2018-09-21 17:06:55 +0000 |
commit | c49c596d17f8d8b62e29f2fd1344ce08cb0a0b93 (patch) | |
tree | 3656bb26d05d473afd779511a2646f398e4f3123 | |
parent | 8f191590447d8e8aa375f1c1104540db5473ce51 (diff) | |
download | gitlab-ce-c49c596d17f8d8b62e29f2fd1344ce08cb0a0b93.tar.gz |
Allows to sort projects by most stars
-rw-r--r-- | app/helpers/sorting_helper.rb | 11 | ||||
-rw-r--r-- | app/models/project.rb | 4 | ||||
-rw-r--r-- | changelogs/unreleased/add-most-stars-for-filter-option.yml | 5 | ||||
-rw-r--r-- | locale/gitlab.pot | 3 | ||||
-rw-r--r-- | spec/features/dashboard/projects_spec.rb | 8 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 12 |
6 files changed, 41 insertions, 2 deletions
diff --git a/app/helpers/sorting_helper.rb b/app/helpers/sorting_helper.rb index 0c59bdd6abb..53bd43d4861 100644 --- a/app/helpers/sorting_helper.rb +++ b/app/helpers/sorting_helper.rb @@ -35,7 +35,8 @@ module SortingHelper sort_value_name => sort_title_name, sort_value_oldest_activity => sort_title_oldest_activity, sort_value_oldest_created => sort_title_oldest_created, - sort_value_recently_created => sort_title_recently_created + sort_value_recently_created => sort_title_recently_created, + sort_value_most_stars => sort_title_most_stars } if current_controller?('admin/projects') @@ -246,6 +247,10 @@ module SortingHelper s_('SortOptions|Last Contact') end + def sort_title_most_stars + s_('SortOptions|Most stars') + end + # Values. def sort_value_access_level_asc 'access_level_asc' @@ -370,4 +375,8 @@ module SortingHelper def sort_value_contacted_date 'contacted_asc' end + + def sort_value_most_stars + 'stars_desc' + end end diff --git a/app/models/project.rb b/app/models/project.rb index 9e4c7f7a2d0..3e14064a556 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -331,7 +331,7 @@ class Project < ActiveRecord::Base # last_activity_at is throttled every minute, but last_repository_updated_at is updated with every push scope :sorted_by_activity, -> { reorder("GREATEST(COALESCE(last_activity_at, '1970-01-01'), COALESCE(last_repository_updated_at, '1970-01-01')) DESC") } - scope :sorted_by_stars, -> { reorder('projects.star_count DESC') } + scope :sorted_by_stars, -> { reorder(star_count: :desc) } scope :in_namespace, ->(namespace_ids) { where(namespace_id: namespace_ids) } scope :personal, ->(user) { where(namespace_id: user.namespace_id) } @@ -481,6 +481,8 @@ class Project < ActiveRecord::Base reorder(last_activity_at: :desc) when 'latest_activity_asc' reorder(last_activity_at: :asc) + when 'stars_desc' + sorted_by_stars else order_by(method) end diff --git a/changelogs/unreleased/add-most-stars-for-filter-option.yml b/changelogs/unreleased/add-most-stars-for-filter-option.yml new file mode 100644 index 00000000000..be95d6db55f --- /dev/null +++ b/changelogs/unreleased/add-most-stars-for-filter-option.yml @@ -0,0 +1,5 @@ +--- +title: Allows to sort projects by most stars +merge_request: 21762 +author: Jacopo Beschi @jacopo-beschi +type: added diff --git a/locale/gitlab.pot b/locale/gitlab.pot index c0603397dbb..bc5d2aeac46 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -5551,6 +5551,9 @@ msgstr "" msgid "SortOptions|Most popular" msgstr "" +msgid "SortOptions|Most stars" +msgstr "" + msgid "SortOptions|Name" msgstr "" diff --git a/spec/features/dashboard/projects_spec.rb b/spec/features/dashboard/projects_spec.rb index 4daacc61d85..975b7944741 100644 --- a/spec/features/dashboard/projects_spec.rb +++ b/spec/features/dashboard/projects_spec.rb @@ -103,6 +103,14 @@ describe 'Dashboard Projects' do expect(page).not_to have_content(project.name) expect(page).to have_content(project3.name) end + + it 'sorts projects by most stars when sorting by most stars' do + project_with_most_stars = create(:project, namespace: user.namespace, star_count: 10) + + visit dashboard_projects_path(sort: :stars_desc) + + expect(first('.project-row')).to have_content(project_with_most_stars.title) + end end context 'when on Starred projects tab' do diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 909012b7789..567b66b54eb 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -1072,6 +1072,18 @@ describe Project do it { expect(project.builds_enabled?).to be_truthy } end + describe '.sort_by_attribute' do + it 'reorders the input relation by start count desc' do + project1 = create(:project, star_count: 2) + project2 = create(:project, star_count: 1) + project3 = create(:project) + + projects = described_class.sort_by_attribute(:stars_desc) + + expect(projects).to eq([project1, project2, project3]) + end + end + describe '.with_shared_runners' do subject { described_class.with_shared_runners } |