Commit c5be267e authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor issuable sorting a bit

parent bbca6a0a
...@@ -29,6 +29,8 @@ module Issuable ...@@ -29,6 +29,8 @@ module Issuable
scope :only_opened, -> { with_state(:opened) } scope :only_opened, -> { with_state(:opened) }
scope :only_reopened, -> { with_state(:reopened) } scope :only_reopened, -> { with_state(:reopened) }
scope :closed, -> { with_state(:closed) } scope :closed, -> { with_state(:closed) }
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') }
delegate :name, delegate :name,
:email, :email,
...@@ -54,9 +56,14 @@ module Issuable ...@@ -54,9 +56,14 @@ module Issuable
end end
def sort(method) def sort(method)
case method.to_s
when 'milestone_due_asc' then order_milestone_due_asc
when 'milestone_due_desc' then order_milestone_due_desc
else
order_by(method) order_by(method)
end end
end end
end
def today? def today?
Date.today == created_at.to_date Date.today == created_at.to_date
......
...@@ -10,25 +10,26 @@ module Sortable ...@@ -10,25 +10,26 @@ module Sortable
# 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_asc, -> { reorder(name: :asc) }
scope :order_created_desc, -> { reorder(created_at: :desc, id: :desc) } scope :order_created_desc, -> { reorder(created_at: :desc, id: :desc) }
scope :order_created_asc, -> { reorder(created_at: :asc, id: :asc) } scope :order_created_asc, -> { reorder(created_at: :asc, id: :asc) }
scope :order_updated_desc, -> { reorder(updated_at: :desc, id: :desc) } scope :order_updated_desc, -> { reorder(updated_at: :desc, id: :desc) }
scope :order_updated_asc, -> { reorder(updated_at: :asc, id: :asc) } 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') } if column_names.include?('name')
scope :order_name_asc, -> { reorder(name: :asc) }
scope :order_name_desc, -> { reorder(name: :desc) }
end
end end
module ClassMethods module ClassMethods
def order_by(method) def order_by(method)
case method.to_s case method.to_s
when 'name_asc' then order_name_asc when 'name_asc' then order_name_asc
when 'name_desc' then order_name_desc
when 'updated_asc' then order_updated_asc when 'updated_asc' then order_updated_asc
when 'updated_desc' then order_updated_desc when 'updated_desc' then order_updated_desc
when 'created_asc' then order_created_asc when 'created_asc' then order_created_asc
when 'created_desc' then order_created_desc 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
......
...@@ -140,7 +140,7 @@ class Project < ActiveRecord::Base ...@@ -140,7 +140,7 @@ class Project < ActiveRecord::Base
mount_uploader :avatar, AttachmentUploader mount_uploader :avatar, AttachmentUploader
# Scopes # Scopes
scope :sorted_by_activity, -> { reorder('projects.last_activity_at DESC') } scope :sorted_by_activity, -> { reorder(last_activity_at: :desc) }
scope :sorted_by_stars, -> { reorder('projects.star_count DESC') } scope :sorted_by_stars, -> { reorder('projects.star_count DESC') }
scope :sorted_by_names, -> { joins(:namespace).reorder('namespaces.name ASC, projects.name ASC') } scope :sorted_by_names, -> { joins(:namespace).reorder('namespaces.name ASC, projects.name ASC') }
......
...@@ -33,7 +33,7 @@ class Spinach::Features::AdminGroups < Spinach::FeatureSteps ...@@ -33,7 +33,7 @@ class Spinach::Features::AdminGroups < Spinach::FeatureSteps
end end
step 'I should be redirected to group page' do step 'I should be redirected to group page' do
current_path.should == admin_group_path(Group.last) current_path.should == admin_group_path(Group.find_by(path: 'gitlab'))
end end
When 'I select user "John Doe" from user list as "Reporter"' do When 'I select user "John Doe" from user list as "Reporter"' do
......
...@@ -484,11 +484,11 @@ describe User do ...@@ -484,11 +484,11 @@ describe User do
end end
it "sorts users as recently_created" do it "sorts users as recently_created" do
User.sort('recently_created').first.should == @user User.sort('created_desc').first.should == @user
end end
it "sorts users as late_created" do it "sorts users as late_created" do
User.sort('late_created').first.should == @user1 User.sort('created_asc').first.should == @user1
end end
it "sorts users by name when nil is passed" do it "sorts users by name when nil is passed" 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