Commit 7022357c authored by Alejandro Rodríguez's avatar Alejandro Rodríguez

Add sorting dropdown to tag page

parent d9812bae
...@@ -84,6 +84,7 @@ v 8.9.0 (unreleased) ...@@ -84,6 +84,7 @@ v 8.9.0 (unreleased)
- Show categorised search queries in the search autocomplete - Show categorised search queries in the search autocomplete
- RepositoryCheck::SingleRepositoryWorker public and private methods are now instrumented - RepositoryCheck::SingleRepositoryWorker public and private methods are now instrumented
- Improve issuables APIs performance when accessing notes !4471 - Improve issuables APIs performance when accessing notes !4471
- Add sorting dropdown to tags page !4423
- External links now open in a new tab - External links now open in a new tab
- Prevent default actions of disabled buttons and links - Prevent default actions of disabled buttons and links
- Markdown editor now correctly resets the input value on edit cancellation !4175 - Markdown editor now correctly resets the input value on edit cancellation !4175
......
...@@ -6,8 +6,10 @@ class Projects::TagsController < Projects::ApplicationController ...@@ -6,8 +6,10 @@ class Projects::TagsController < Projects::ApplicationController
before_action :authorize_admin_project!, only: [:destroy] before_action :authorize_admin_project!, only: [:destroy]
def index def index
sorted = VersionSorter.rsort(@repository.tag_names) @sort = params[:sort] || 'name'
@tags = Kaminari.paginate_array(sorted).page(params[:page]) @tags = @repository.tags_sorted_by(@sort)
@tags = Kaminari.paginate_array(@tags).page(params[:page])
@releases = project.releases.where(tag: @tags) @releases = project.releases.where(tag: @tags)
end end
......
...@@ -598,6 +598,21 @@ class Repository ...@@ -598,6 +598,21 @@ class Repository
end end
end end
def tags_sorted_by(value)
case value
when 'name'
# Would be better to use `sort_by` but `version_sorter` only exposes
# `sort` and `rsort`
VersionSorter.rsort(tag_names).map { |tag_name| find_tag(tag_name) }
when 'updated_desc'
tags_sorted_by_committed_date.reverse
when 'updated_asc'
tags_sorted_by_committed_date
else
tags
end
end
def contributors def contributors
commits = self.commits(nil, limit: 2000, offset: 0, skip_merges: true) commits = self.commits(nil, limit: 2000, offset: 0, skip_merges: true)
...@@ -995,4 +1010,8 @@ class Repository ...@@ -995,4 +1010,8 @@ class Repository
def file_on_head(regex) def file_on_head(regex)
tree(:head).blobs.find { |file| file.name =~ regex } tree(:head).blobs.find { |file| file.name =~ regex }
end end
def tags_sorted_by_committed_date
tags.sort_by { |tag| commit(tag.target).committed_date }
end
end end
...@@ -11,12 +11,23 @@ ...@@ -11,12 +11,23 @@
.nav-controls .nav-controls
= link_to new_namespace_project_tag_path(@project.namespace, @project), class: 'btn btn-create new-tag-btn' do = link_to new_namespace_project_tag_path(@project.namespace, @project), class: 'btn btn-create new-tag-btn' do
New tag New tag
.dropdown.inline
%button.dropdown-toggle.btn{ type: 'button', data: { toggle: 'dropdown'} }
%span.light= @sort.humanize
%b.caret
%ul.dropdown-menu.dropdown-menu-align-right
%li
= link_to namespace_project_tags_path(sort: nil) do
Name
= link_to namespace_project_tags_path(sort: sort_value_recently_updated) do
= sort_title_recently_updated
= link_to namespace_project_tags_path(sort: sort_value_oldest_updated) do
= sort_title_oldest_updated
.tags .tags
- unless @tags.empty? - unless @tags.empty?
%ul.content-list %ul.content-list
- @tags.each do |tag| = render partial: 'tag', collection: @tags
= render 'tag', tag: @repository.find_tag(tag)
= paginate @tags, theme: 'gitlab' = paginate @tags, theme: 'gitlab'
......
...@@ -31,6 +31,47 @@ describe Repository, models: true do ...@@ -31,6 +31,47 @@ describe Repository, models: true do
it { is_expected.not_to include('v1.0.0') } it { is_expected.not_to include('v1.0.0') }
end end
describe 'tags_sorted_by' do
context 'name' do
subject { repository.tags_sorted_by('name').map(&:name) }
it { is_expected.to eq(['v1.1.0', 'v1.0.0']) }
end
context 'updated' do
let(:tag_a) { repository.find_tag('v1.0.0') }
let(:tag_b) { repository.find_tag('v1.1.0') }
context 'desc' do
subject { repository.tags_sorted_by('updated_desc').map(&:name) }
before do
double_first = double(committed_date: Time.now)
double_last = double(committed_date: Time.now - 1.second)
allow(repository).to receive(:commit).with(tag_a.target).and_return(double_first)
allow(repository).to receive(:commit).with(tag_b.target).and_return(double_last)
end
it { is_expected.to eq(['v1.0.0', 'v1.1.0']) }
end
context 'asc' do
subject { repository.tags_sorted_by('updated_asc').map(&:name) }
before do
double_first = double(committed_date: Time.now - 1.second)
double_last = double(committed_date: Time.now)
allow(repository).to receive(:commit).with(tag_a.target).and_return(double_last)
allow(repository).to receive(:commit).with(tag_b.target).and_return(double_first)
end
it { is_expected.to eq(['v1.1.0', 'v1.0.0']) }
end
end
end
describe :last_commit_for_path do describe :last_commit_for_path do
subject { repository.last_commit_for_path(sample_commit.id, '.gitignore').id } subject { repository.last_commit_for_path(sample_commit.id, '.gitignore').id }
......
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