Commit c5be267e authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor issuable sorting a bit

parent bbca6a0a
......@@ -29,6 +29,8 @@ module Issuable
scope :only_opened, -> { with_state(:opened) }
scope :only_reopened, -> { with_state(:reopened) }
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,
:email,
......@@ -54,9 +56,14 @@ module Issuable
end
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)
end
end
end
def today?
Date.today == created_at.to_date
......
......@@ -10,25 +10,26 @@ module Sortable
# by created_at field starting from newest
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_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') }
if column_names.include?('name')
scope :order_name_asc, -> { reorder(name: :asc) }
scope :order_name_desc, -> { reorder(name: :desc) }
end
end
module ClassMethods
def order_by(method)
case method.to_s
when 'name_asc' then order_name_asc
when 'name_desc' then order_name_desc
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
......
......@@ -140,7 +140,7 @@ class Project < ActiveRecord::Base
mount_uploader :avatar, AttachmentUploader
# 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_names, -> { joins(:namespace).reorder('namespaces.name ASC, projects.name ASC') }
......
......@@ -33,7 +33,7 @@ class Spinach::Features::AdminGroups < Spinach::FeatureSteps
end
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
When 'I select user "John Doe" from user list as "Reporter"' do
......
......@@ -484,11 +484,11 @@ describe User do
end
it "sorts users as recently_created" do
User.sort('recently_created').first.should == @user
User.sort('created_desc').first.should == @user
end
it "sorts users as late_created" do
User.sort('late_created').first.should == @user1
User.sort('created_asc').first.should == @user1
end
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