Commit 611c5f1d7cb0ae74d923499d01c2f30d5e8f9384

Authored by Dmitriy Zaporozhets
1 parent 231b91d0

Sort dropdown for Dashboard#projects page

Be default it sorts by name now
Respect filters like scope, label, visibility when do sort or another
filter

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/controllers/dashboard_controller.rb
@@ -41,13 +41,13 @@ class DashboardController &lt; ApplicationController @@ -41,13 +41,13 @@ class DashboardController &lt; ApplicationController
41 41
42 @projects = @projects.where(namespace_id: Group.find_by_name(params[:group])) if params[:group].present? 42 @projects = @projects.where(namespace_id: Group.find_by_name(params[:group])) if params[:group].present?
43 @projects = @projects.where(visibility_level: params[:visibility_level]) if params[:visibility_level].present? 43 @projects = @projects.where(visibility_level: params[:visibility_level]) if params[:visibility_level].present?
44 - @projects = @projects.includes(:namespace).sorted_by_activity 44 + @projects = @projects.includes(:namespace)
  45 + @projects = @projects.tagged_with(params[:label]) if params[:label].present?
  46 + @projects = @projects.sort(@sort = params[:sort])
  47 + @projects = @projects.page(params[:page]).per(30)
45 48
46 @labels = current_user.authorized_projects.tags_on(:labels) 49 @labels = current_user.authorized_projects.tags_on(:labels)
47 @groups = current_user.authorized_groups 50 @groups = current_user.authorized_groups
48 -  
49 - @projects = @projects.tagged_with(params[:label]) if params[:label].present?  
50 - @projects = @projects.page(params[:page]).per(30)  
51 end 51 end
52 52
53 def merge_requests 53 def merge_requests
app/controllers/public/projects_controller.rb
@@ -8,14 +8,7 @@ class Public::ProjectsController &lt; ApplicationController @@ -8,14 +8,7 @@ class Public::ProjectsController &lt; ApplicationController
8 def index 8 def index
9 @projects = Project.public_or_internal_only(current_user) 9 @projects = Project.public_or_internal_only(current_user)
10 @projects = @projects.search(params[:search]) if params[:search].present? 10 @projects = @projects.search(params[:search]) if params[:search].present?
11 - @sort = params[:sort]  
12 - @projects = case @sort  
13 - when 'newest' then @projects.order('created_at DESC')  
14 - when 'oldest' then @projects.order('created_at ASC')  
15 - when 'recently_updated' then @projects.order('updated_at DESC')  
16 - when 'last_updated' then @projects.order('updated_at ASC')  
17 - else @projects.order("namespaces.path, projects.name ASC")  
18 - end 11 + @projects = @projects.sort(@sort = params[:sort])
19 @projects = @projects.includes(:namespace).page(params[:page]).per(20) 12 @projects = @projects.includes(:namespace).page(params[:page]).per(20)
20 end 13 end
21 end 14 end
app/helpers/dashboard_helper.rb
@@ -21,4 +21,18 @@ module DashboardHelper @@ -21,4 +21,18 @@ module DashboardHelper
21 [] 21 []
22 end.count 22 end.count
23 end 23 end
  24 +
  25 + def projects_dashboard_filter_path(options={})
  26 + exist_opts = {
  27 + sort: params[:sort],
  28 + scope: params[:scope],
  29 + group: params[:group],
  30 + }
  31 +
  32 + options = exist_opts.merge(options)
  33 +
  34 + path = request.path
  35 + path << "?#{options.to_param}"
  36 + path
  37 + end
24 end 38 end
app/helpers/tab_helper.rb
@@ -92,7 +92,12 @@ module TabHelper @@ -92,7 +92,12 @@ module TabHelper
92 def nav_tab key, value, &block 92 def nav_tab key, value, &block
93 o = {} 93 o = {}
94 o[:class] = "" 94 o[:class] = ""
95 - o[:class] << " active" if params[key] == value 95 +
  96 + if value.nil?
  97 + o[:class] << " active" if params[key].blank?
  98 + else
  99 + o[:class] << " active" if params[key] == value
  100 + end
96 101
97 if block_given? 102 if block_given?
98 content_tag(:li, capture(&block), o) 103 content_tag(:li, capture(&block), o)
app/models/project.rb
@@ -153,6 +153,16 @@ class Project &lt; ActiveRecord::Base @@ -153,6 +153,16 @@ class Project &lt; ActiveRecord::Base
153 def visibility_levels 153 def visibility_levels
154 Gitlab::VisibilityLevel.options 154 Gitlab::VisibilityLevel.options
155 end 155 end
  156 +
  157 + def sort(method)
  158 + case method.to_s
  159 + when 'newest' then reorder('projects.created_at DESC')
  160 + when 'oldest' then reorder('projects.created_at ASC')
  161 + when 'recently_updated' then reorder('projects.updated_at DESC')
  162 + when 'last_updated' then reorder('projects.updated_at ASC')
  163 + else reorder("namespaces.path, projects.name ASC")
  164 + end
  165 + end
156 end 166 end
157 167
158 def team 168 def team
app/views/dashboard/projects.html.haml
1 -%h3.page-title My Projects 1 +%h3.page-title
  2 + My Projects
  3 +.pull-right
  4 + .dropdown.inline
  5 + %a.dropdown-toggle.btn.btn-small{href: '#', "data-toggle" => "dropdown"}
  6 + %span.light sort:
  7 + - if @sort.present?
  8 + = @sort.humanize
  9 + - else
  10 + Name
  11 + %b.caret
  12 + %ul.dropdown-menu
  13 + %li
  14 + = link_to projects_dashboard_filter_path(sort: nil) do
  15 + Name
  16 + = link_to projects_dashboard_filter_path(sort: 'newest') do
  17 + Newest
  18 + = link_to projects_dashboard_filter_path(sort: 'oldest') do
  19 + Oldest
  20 + = link_to projects_dashboard_filter_path(sort: 'recently_updated') do
  21 + Recently updated
  22 + = link_to projects_dashboard_filter_path(sort: 'last_updated') do
  23 + Last updated
2 %p.light 24 %p.light
3 All projects you have access to are listed here. Public projects are not included here unless you are a member 25 All projects you have access to are listed here. Public projects are not included here unless you are a member
4 %hr 26 %hr
@@ -6,22 +28,22 @@ @@ -6,22 +28,22 @@
6 .span3 28 .span3
7 %ul.nav.nav-pills.nav-stacked 29 %ul.nav.nav-pills.nav-stacked
8 = nav_tab :scope, nil do 30 = nav_tab :scope, nil do
9 - = link_to projects_dashboard_path do 31 + = link_to projects_dashboard_filter_path(scope: nil) do
10 All 32 All
11 %span.pull-right 33 %span.pull-right
12 = current_user.authorized_projects.count 34 = current_user.authorized_projects.count
13 = nav_tab :scope, 'personal' do 35 = nav_tab :scope, 'personal' do
14 - = link_to projects_dashboard_path(scope: 'personal') do 36 + = link_to projects_dashboard_filter_path(scope: 'personal') do
15 Personal 37 Personal
16 %span.pull-right 38 %span.pull-right
17 = current_user.personal_projects.count 39 = current_user.personal_projects.count
18 = nav_tab :scope, 'joined' do 40 = nav_tab :scope, 'joined' do
19 - = link_to projects_dashboard_path(scope: 'joined') do 41 + = link_to projects_dashboard_filter_path(scope: 'joined') do
20 Joined 42 Joined
21 %span.pull-right 43 %span.pull-right
22 = current_user.authorized_projects.joined(current_user).count 44 = current_user.authorized_projects.joined(current_user).count
23 = nav_tab :scope, 'owned' do 45 = nav_tab :scope, 'owned' do
24 - = link_to projects_dashboard_path(scope: 'owned') do 46 + = link_to projects_dashboard_filter_path(scope: 'owned') do
25 Owned 47 Owned
26 %span.pull-right 48 %span.pull-right
27 = current_user.owned_projects.count 49 = current_user.owned_projects.count
@@ -31,7 +53,7 @@ @@ -31,7 +53,7 @@
31 %ul.bordered-list.visibility-filter 53 %ul.bordered-list.visibility-filter
32 - Gitlab::VisibilityLevel.values.each do |level| 54 - Gitlab::VisibilityLevel.values.each do |level|
33 %li{ class: (level.to_s == params[:visibility_level]) ? 'active' : 'light' } 55 %li{ class: (level.to_s == params[:visibility_level]) ? 'active' : 'light' }
34 - = link_to projects_dashboard_path(visibility_level: level) do 56 + = link_to projects_dashboard_filter_path(visibility_level: level) do
35 = visibility_level_icon(level) 57 = visibility_level_icon(level)
36 = visibility_level_label(level) 58 = visibility_level_label(level)
37 59
@@ -41,7 +63,7 @@ @@ -41,7 +63,7 @@
41 %ul.bordered-list 63 %ul.bordered-list
42 - @groups.each do |group| 64 - @groups.each do |group|
43 %li{ class: (group.name == params[:group]) ? 'active' : 'light' } 65 %li{ class: (group.name == params[:group]) ? 'active' : 'light' }
44 - = link_to projects_dashboard_path(group: group.name) do 66 + = link_to projects_dashboard_filter_path(group: group.name) do
45 %i.icon-folder-close-alt 67 %i.icon-folder-close-alt
46 = group.name 68 = group.name
47 %small.pull-right 69 %small.pull-right
@@ -55,7 +77,7 @@ @@ -55,7 +77,7 @@
55 %ul.bordered-list 77 %ul.bordered-list
56 - @labels.each do |label| 78 - @labels.each do |label|
57 %li{ class: (label.name == params[:label]) ? 'active' : 'light' } 79 %li{ class: (label.name == params[:label]) ? 'active' : 'light' }
58 - = link_to projects_dashboard_path(scope: params[:scope], label: label.name) do 80 + = link_to projects_dashboard_filter_path(scope: params[:scope], label: label.name) do
59 %i.icon-tag 81 %i.icon-tag
60 = label.name 82 = label.name
61 83