Commit bbca6a0a authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor sorting in project

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