Commit bbca6a0a authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor sorting in project

parent e0aa5c37
...@@ -2,7 +2,8 @@ class Admin::GroupsController < Admin::ApplicationController ...@@ -2,7 +2,8 @@ class Admin::GroupsController < Admin::ApplicationController
before_filter :group, only: [:edit, :show, :update, :destroy, :project_update, :project_teams_update] before_filter :group, only: [:edit, :show, :update, :destroy, :project_update, :project_teams_update]
def index def index
@groups = Group.order_name @groups = Group.all
@groups = @groups.sort(@sort = params[:sort])
@groups = @groups.search(params[:name]) if params[:name].present? @groups = @groups.search(params[:name]) if params[:name].present?
@groups = @groups.page(params[:page]).per(20) @groups = @groups.page(params[:page]).per(20)
end end
......
...@@ -2,10 +2,10 @@ class Admin::UsersController < Admin::ApplicationController ...@@ -2,10 +2,10 @@ class Admin::UsersController < Admin::ApplicationController
before_filter :user, only: [:show, :edit, :update, :destroy] before_filter :user, only: [:show, :edit, :update, :destroy]
def index def index
@users = User.filter(params[:filter]) @users = User.order_name_asc.filter(params[:filter])
@users = @users.search(params[:name]) if params[:name].present? @users = @users.search(params[:name]) if params[:name].present?
@users = @users.sort(@sort = params[:sort]) @users = @users.sort(@sort = params[:sort])
@users = @users.order_name.page(params[:page]) @users = @users.page(params[:page])
end end
def show def show
......
...@@ -254,7 +254,7 @@ class ApplicationController < ActionController::Base ...@@ -254,7 +254,7 @@ class ApplicationController < ActionController::Base
end end
def set_filters_params def set_filters_params
params[:sort] ||= 'newest' params[:sort] ||= 'created_desc'
params[:scope] = 'all' if params[:scope].blank? params[:scope] = 'all' if params[:scope].blank?
params[:state] = 'opened' if params[:state].blank? params[:state] = 'opened' if params[:state].blank?
...@@ -280,7 +280,7 @@ class ApplicationController < ActionController::Base ...@@ -280,7 +280,7 @@ class ApplicationController < ActionController::Base
author_id = @filter_params[:author_id] author_id = @filter_params[:author_id]
milestone_id = @filter_params[:milestone_id] milestone_id = @filter_params[:milestone_id]
@sort = @filter_params[:sort].try(:humanize) @sort = @filter_params[:sort]
@assignees = User.where(id: collection.pluck(:assignee_id)) @assignees = User.where(id: collection.pluck(:assignee_id))
@authors = User.where(id: collection.pluck(:author_id)) @authors = User.where(id: collection.pluck(:author_id))
@milestones = Milestone.where(id: collection.pluck(:milestone_id)) @milestones = Milestone.where(id: collection.pluck(:milestone_id))
......
...@@ -9,7 +9,7 @@ class DashboardController < ApplicationController ...@@ -9,7 +9,7 @@ class DashboardController < ApplicationController
# If user needs more - point to Dashboard#projects page # If user needs more - point to Dashboard#projects page
@projects_limit = 30 @projects_limit = 30
@groups = current_user.authorized_groups.sort_by(&:human_name) @groups = current_user.authorized_groups.order_name_asc
@has_authorized_projects = @projects.count > 0 @has_authorized_projects = @projects.count > 0
@projects_count = @projects.count @projects_count = @projects.count
@projects = @projects.limit(@projects_limit) @projects = @projects.limit(@projects_limit)
......
module SortingHelper module SortingHelper
def sort_options_hash
{
sort_value_name => sort_title_name,
sort_value_recently_updated => sort_title_recently_updated,
sort_value_oldest_updated => sort_title_oldest_updated,
sort_value_recently_created => sort_title_recently_created,
sort_value_oldest_created => sort_title_oldest_created,
sort_value_milestone_soon => sort_title_milestone_soon,
sort_value_milestone_later => sort_title_milestone_later,
sort_value_largest_repo => sort_title_largest_repo,
sort_value_recently_signin => sort_title_recently_signin,
sort_value_oldest_signin => sort_title_oldest_signin,
}
end
def sort_title_oldest_updated def sort_title_oldest_updated
'Oldest updated' 'Oldest updated'
end end
...@@ -14,4 +29,68 @@ module SortingHelper ...@@ -14,4 +29,68 @@ module SortingHelper
def sort_title_recently_created def sort_title_recently_created
'Recently created' 'Recently created'
end end
def sort_title_milestone_soon
'Milestone due soon'
end
def sort_title_milestone_later
'Milestone due later'
end
def sort_title_name
'Name'
end
def sort_title_largest_repo
'Largest repository'
end
def sort_title_recently_signin
'Recent sign in'
end
def sort_title_oldest_signin
'Oldest sign in'
end
def sort_value_oldest_updated
'updated_asc'
end
def sort_value_recently_updated
'updated_desc'
end
def sort_value_oldest_created
'created_asc'
end
def sort_value_recently_created
'created_desc'
end
def sort_value_milestone_soon
'milestone_due_asc'
end
def sort_value_milestone_later
'milestone_due_desc'
end
def sort_value_name
'name_asc'
end
def sort_value_largest_repo
'repository_size_desc'
end
def sort_value_recently_signin
'recent_sign_in'
end
def sort_value_oldest_signin
'oldest_sign_in'
end
end end
...@@ -54,15 +54,7 @@ module Issuable ...@@ -54,15 +54,7 @@ module Issuable
end end
def sort(method) def sort(method)
case method.to_s order_by(method)
when 'newest' then reorder("#{table_name}.created_at DESC")
when 'oldest' then reorder("#{table_name}.created_at ASC")
when 'recently_updated' then reorder("#{table_name}.updated_at DESC")
when 'last_updated' then reorder("#{table_name}.updated_at ASC")
when 'milestone_due_soon' then joins(:milestone).reorder("milestones.due_date ASC")
when 'milestone_due_later' then joins(:milestone).reorder("milestones.due_date DESC")
else reorder("#{table_name}.created_at DESC")
end
end end
end end
......
...@@ -9,21 +9,26 @@ module Sortable ...@@ -9,21 +9,26 @@ module Sortable
# By default all models should be ordered # By default all models should be ordered
# by created_at field starting from newest # by created_at field starting from newest
default_scope { order(created_at: :desc, id: :desc) } default_scope { order(created_at: :desc, id: :desc) }
scope :order_name, -> { reorder(name: :asc) }
scope :order_recent, -> { reorder(created_at: :desc, id: :desc) } scope :order_name_asc, -> { reorder(name: :asc) }
scope :order_oldest, -> { reorder(created_at: :asc, id: :asc) } scope :order_created_desc, -> { reorder(created_at: :desc, id: :desc) }
scope :order_recent_updated, -> { reorder(updated_at: :desc, id: :desc) } scope :order_created_asc, -> { reorder(created_at: :asc, id: :asc) }
scope :order_oldest_updated, -> { reorder(updated_at: :asc, id: :asc) } scope :order_updated_desc, -> { reorder(updated_at: :desc, id: :desc) }
scope :order_updated_asc, -> { reorder(updated_at: :asc, id: :asc) }
scope :order_milestone_due_desc, -> { joins(:milestone).reorder('milestones.due_date DESC, milestones.id DESC') }
scope :order_milestone_due_asc, -> { joins(:milestone).reorder('milestones.due_date ASC, milestones.id ASC') }
end end
module ClassMethods module ClassMethods
def order_by(method) def order_by(method)
case method.to_s case method.to_s
when 'name' then order_name_asc when 'name_asc' then order_name_asc
when 'recent' then order_recent when 'updated_asc' then order_updated_asc
when 'oldest' then order_oldest when 'updated_desc' then order_updated_desc
when 'recent_updated' then order_recent_updated when 'created_asc' then order_created_asc
when 'oldest_updated' then order_oldest_updated when 'created_desc' then order_created_desc
when 'milestone_due_asc' then order_milestone_due_asc
when 'milestone_due_desc' then order_milestone_due_desc
else else
all all
end end
......
...@@ -28,6 +28,16 @@ class Group < Namespace ...@@ -28,6 +28,16 @@ class Group < Namespace
after_create :post_create_hook after_create :post_create_hook
after_destroy :post_destroy_hook after_destroy :post_destroy_hook
class << self
def search(query)
where("LOWER(namespaces.name) LIKE :query or LOWER(namespaces.path) LIKE :query", query: "%#{query.downcase}%")
end
def sort(method)
order_by(method)
end
end
def human_name def human_name
name name
end end
...@@ -88,20 +98,4 @@ class Group < Namespace ...@@ -88,20 +98,4 @@ class Group < Namespace
def system_hook_service def system_hook_service
SystemHooksService.new SystemHooksService.new
end end
class << self
def search(query)
where("LOWER(namespaces.name) LIKE :query or LOWER(namespaces.path) LIKE :query", query: "%#{query.downcase}%")
end
def sort(method)
case method.to_s
when "newest" then reorder("namespaces.created_at DESC")
when "oldest" then reorder("namespaces.created_at ASC")
when "recently_updated" then reorder("namespaces.updated_at DESC")
when "last_updated" then reorder("namespaces.updated_at ASC")
else reorder("namespaces.path, namespaces.name ASC")
end
end
end
end end
...@@ -231,13 +231,10 @@ class Project < ActiveRecord::Base ...@@ -231,13 +231,10 @@ class Project < ActiveRecord::Base
end end
def sort(method) def sort(method)
case method.to_s if method == 'repository_size_desc'
when 'newest' then reorder('projects.created_at DESC') reorder(repository_size: :desc, id: :desc)
when 'oldest' then reorder('projects.created_at ASC') else
when 'recently_updated' then reorder('projects.updated_at DESC') order_by(method)
when 'last_updated' then reorder('projects.updated_at ASC')
when 'largest_repository' then reorder('projects.repository_size DESC')
else reorder('namespaces.path, projects.name ASC')
end end
end end
end end
......
...@@ -199,11 +199,10 @@ class User < ActiveRecord::Base ...@@ -199,11 +199,10 @@ class User < ActiveRecord::Base
def sort(method) def sort(method)
case method.to_s case method.to_s
when 'recent_sign_in' then reorder('users.last_sign_in_at DESC') when 'recent_sign_in' then reorder(last_sign_in_at: :desc)
when 'oldest_sign_in' then reorder('users.last_sign_in_at ASC') when 'oldest_sign_in' then reorder(last_sign_in_at: :asc)
when 'recently_created' then reorder('users.created_at DESC') else
when 'late_created' then reorder('users.created_at ASC') order_by(method)
else reorder("users.name ASC")
end end
end end
......
...@@ -8,10 +8,31 @@ ...@@ -8,10 +8,31 @@
%hr %hr
= form_tag admin_groups_path, method: :get, class: 'form-inline' do = form_tag admin_groups_path, method: :get, class: 'form-inline' do
= hidden_field_tag :sort, @sort
.form-group .form-group
= text_field_tag :name, params[:name], class: "form-control input-mn-300" = text_field_tag :name, params[:name], class: "form-control input-mn-300"
= button_tag "Search", class: "btn submit btn-primary" = button_tag "Search", class: "btn submit btn-primary"
.pull-right
.dropdown.inline
%a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"}
%span.light sort:
- if @sort.present?
= sort_options_hash[@sort]
- else
= sort_title_recently_created
%b.caret
%ul.dropdown-menu
%li
= link_to admin_groups_path(sort: sort_value_recently_created) do
= sort_title_recently_created
= link_to admin_groups_path(sort: sort_value_oldest_created) do
= sort_title_oldest_created
= link_to admin_groups_path(sort: sort_value_recently_updated) do
= sort_title_recently_updated
= link_to admin_groups_path(sort: sort_value_oldest_updated) do
= sort_title_oldest_updated
%hr %hr
%ul.bordered-list %ul.bordered-list
......
...@@ -47,24 +47,22 @@ ...@@ -47,24 +47,22 @@
%a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"} %a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"}
%span.light sort: %span.light sort:
- if @sort.present? - if @sort.present?
= @sort.humanize = sort_options_hash[@sort]
- else - else
Name = sort_title_recently_created
%b.caret %b.caret
%ul.dropdown-menu %ul.dropdown-menu
%li %li
= link_to admin_projects_path(sort: nil) do = link_to admin_projects_path(sort: sort_value_recently_created) do
Name
= link_to admin_projects_path(sort: 'newest') do
= sort_title_recently_created = sort_title_recently_created
= link_to admin_projects_path(sort: 'oldest') do = link_to admin_projects_path(sort: sort_value_oldest_created) do
= sort_title_oldest_created = sort_title_oldest_created
= link_to admin_projects_path(sort: 'recently_updated') do = link_to admin_projects_path(sort: sort_value_recently_updated) do
= sort_title_recently_updated = sort_title_recently_updated
= link_to admin_projects_path(sort: 'last_updated') do = link_to admin_projects_path(sort: sort_value_oldest_updated) do
= sort_title_oldest_updated = sort_title_oldest_updated
= link_to admin_projects_path(sort: 'largest_repository') do = link_to admin_projects_path(sort: sort_value_largest_repo) do
Largest repository = sort_title_largest_repo
= link_to 'New Project', new_project_path, class: "btn btn-new" = link_to 'New Project', new_project_path, class: "btn btn-new"
%ul.well-list %ul.well-list
- @projects.each do |project| - @projects.each do |project|
......
...@@ -36,22 +36,26 @@ ...@@ -36,22 +36,26 @@
%a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"} %a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"}
%span.light sort: %span.light sort:
- if @sort.present? - if @sort.present?
= @sort.humanize = sort_options_hash[@sort]
- else - else
Name = sort_title_name
%b.caret %b.caret
%ul.dropdown-menu %ul.dropdown-menu
%li %li
= link_to admin_users_path(sort: nil) do = link_to admin_users_path(sort: sort_value_name) do
Name = sort_title_name
= link_to admin_users_path(sort: 'recent_sign_in') do = link_to admin_users_path(sort: sort_value_recently_signin) do
Recent sign in = sort_title_recently_signin
= link_to admin_users_path(sort: 'oldest_sign_in') do = link_to admin_users_path(sort: sort_value_oldest_signin) do
Oldest sign in = sort_title_oldest_signin
= link_to admin_users_path(sort: 'recently_created') do = link_to admin_users_path(sort: sort_value_recently_created) do
= sort_title_recently_created = sort_title_recently_created
= link_to admin_users_path(sort: 'late_created') do = link_to admin_users_path(sort: sort_value_oldest_created) do
= sort_title_oldest_created = sort_title_oldest_created
= link_to admin_users_path(sort: sort_value_recently_updated) do
= sort_title_recently_updated
= link_to admin_users_path(sort: sort_value_oldest_updated) do
= sort_title_oldest_updated
= link_to 'New User', new_admin_user_path, class: "btn btn-new" = link_to 'New User', new_admin_user_path, class: "btn btn-new"
%ul.well-list %ul.well-list
......
...@@ -82,19 +82,19 @@ ...@@ -82,19 +82,19 @@
%a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"} %a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"}
%span.light sort: %span.light sort:
- if @sort.present? - if @sort.present?
= @sort.humanize = sort_options_hash[@sort]
- else - else
Name = sort_title_recently_created
%b.caret %b.caret
%ul.dropdown-menu %ul.dropdown-menu
%li %li
= link_to projects_dashboard_filter_path(sort: nil) do = link_to projects_dashboard_filter_path(sort: sort_value_recently_created) do
Name
= link_to projects_dashboard_filter_path(sort: 'newest') do
= sort_title_recently_created = sort_title_recently_created
= link_to projects_dashboard_filter_path(sort: 'oldest') do = link_to projects_dashboard_filter_path(sort: sort_value_oldest_created) do
= sort_title_oldest_created = sort_title_oldest_created
= link_to projects_dashboard_filter_path(sort: 'recently_updated') do = link_to projects_dashboard_filter_path(sort: sort_value_recently_updated) do
= sort_title_recently_updated = sort_title_recently_updated
= link_to projects_dashboard_filter_path(sort: 'last_updated') do = link_to projects_dashboard_filter_path(sort: sort_value_oldest_updated) do
= sort_title_oldest_updated = sort_title_oldest_updated
= link_to projects_dashboard_filter_path(sort: sort_value_name) do
= sort_title_name
.clearfix .clearfix
.pull-left .pull-left
= form_tag explore_groups_path, method: :get, class: 'form-inline form-tiny' do |f| = form_tag explore_groups_path, method: :get, class: 'form-inline form-tiny' do |f|
= hidden_field_tag :sort, @sort
.form-group .form-group
= search_field_tag :search, params[:search], placeholder: "Filter by name", class: "form-control search-text-input input-mn-300", id: "groups_search" = search_field_tag :search, params[:search], placeholder: "Filter by name", class: "form-control search-text-input input-mn-300", id: "groups_search"
.form-group .form-group
...@@ -11,21 +12,19 @@ ...@@ -11,21 +12,19 @@
%a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"} %a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"}
%span.light sort: %span.light sort:
- if @sort.present? - if @sort.present?
= @sort.humanize = sort_options_hash[@sort]
- else - else
Name = sort_title_recently_created
%b.caret %b.caret
%ul.dropdown-menu %ul.dropdown-menu
%li %li
= link_to explore_groups_path(sort: nil) do = link_to explore_groups_path(sort: sort_value_recently_created) do
Name
= link_to explore_groups_path(sort: 'newest') do
= sort_title_recently_created = sort_title_recently_created
= link_to explore_groups_path(sort: 'oldest') do = link_to explore_groups_path(sort: sort_value_oldest_created) do
= sort_title_oldest_created = sort_title_oldest_created
= link_to explore_groups_path(sort: 'recently_updated') do = link_to explore_groups_path(sort: sort_value_recently_updated) do
= sort_title_recently_updated = sort_title_recently_updated
= link_to explore_groups_path(sort: 'last_updated') do = link_to explore_groups_path(sort: sort_value_oldest_updated) do
= sort_title_oldest_updated = sort_title_oldest_updated
%hr %hr
......
...@@ -11,21 +11,19 @@ ...@@ -11,21 +11,19 @@
%a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"} %a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"}
%span.light sort: %span.light sort:
- if @sort.present? - if @sort.present?
= @sort.humanize = sort_options_hash[@sort]
- else - else
Name = sort_title_recently_created
%b.caret %b.caret
%ul.dropdown-menu %ul.dropdown-menu
%li %li
= link_to explore_projects_path(sort: nil) do = link_to explore_projects_path(sort: sort_value_recently_created) do
Name
= link_to explore_projects_path(sort: 'newest') do
= sort_title_recently_created = sort_title_recently_created
= link_to explore_projects_path(sort: 'oldest') do = link_to explore_projects_path(sort: sort_value_oldest_created) do
= sort_title_oldest_created = sort_title_oldest_created
= link_to explore_projects_path(sort: 'recently_updated') do = link_to explore_projects_path(sort: sort_value_recently_updated) do
= sort_title_recently_updated = sort_title_recently_updated
= link_to explore_projects_path(sort: 'last_updated') do = link_to explore_projects_path(sort: sort_value_oldest_updated) do
= sort_title_oldest_updated = sort_title_oldest_updated
%hr %hr
......
...@@ -2,21 +2,21 @@ ...@@ -2,21 +2,21 @@
%a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"} %a.dropdown-toggle.btn{href: '#', "data-toggle" => "dropdown"}
%span.light sort: %span.light sort:
- if @sort.present? - if @sort.present?
= @sort = sort_options_hash[@sort]
- else - else
Newest = sort_title_recently_created
%b.caret %b.caret
%ul.dropdown-menu.dropdown-menu-align-right %ul.dropdown-menu.dropdown-menu-align-right
%li %li
= link_to page_filter_path(sort: 'newest') do = link_to page_filter_path(sort: sort_value_recently_created) do
= sort_title_recently_created = sort_title_recently_created
= link_to page_filter_path(sort: 'oldest') do = link_to page_filter_path(sort: sort_value_oldest_created) do
= sort_title_oldest_created = sort_title_oldest_created
= link_to page_filter_path(sort: 'recently_updated') do = link_to page_filter_path(sort: sort_value_recently_updated) do
= sort_title_recently_updated = sort_title_recently_updated
= link_to page_filter_path(sort: 'last_updated') do = link_to page_filter_path(sort: sort_value_oldest_updated) do
= sort_title_oldest_updated = sort_title_oldest_updated
= link_to page_filter_path(sort: 'milestone_due_soon') do = link_to page_filter_path(sort: sort_value_milestone_soon) do
Milestone due soon = sort_title_milestone_soon
= link_to page_filter_path(sort: 'milestone_due_later') do = link_to page_filter_path(sort: sort_value_milestone_later) do
Milestone due later = sort_title_milestone_later
...@@ -83,7 +83,7 @@ class Spinach::Features::Groups < Spinach::FeatureSteps ...@@ -83,7 +83,7 @@ class Spinach::Features::Groups < Spinach::FeatureSteps
end end
step 'I should be redirected to group "Samurai" page' do step 'I should be redirected to group "Samurai" page' do
current_path.should == group_path(Group.last) current_path.should == group_path(Group.find_by(name: 'Samurai'))
end end
step 'I should see newly created group "Samurai"' do step 'I should see newly created group "Samurai"' do
......
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