Move issues sort logic to Issuable concern

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2014-01-14 20:49:32 +02:00
parent adf6ee09b0
commit b85e4d3cbd
No known key found for this signature in database
GPG key ID: 627C5F589F467F17
2 changed files with 13 additions and 15 deletions

View file

@ -31,23 +31,9 @@ module Issues
end
# Sort by :sort param
@issues = sort(@issues, params[:sort])
@issues = @issues.sort(params[:sort])
@issues
end
private
def sort(issues, condition)
case condition
when 'newest' then issues.except(:order).order('created_at DESC')
when 'oldest' then issues.except(:order).order('created_at ASC')
when 'recently_updated' then issues.except(:order).order('updated_at DESC')
when 'last_updated' then issues.except(:order).order('updated_at ASC')
when 'milestone_due_soon' then issues.except(:order).joins(:milestone).order("milestones.due_date ASC")
when 'milestone_due_later' then issues.except(:order).joins(:milestone).order("milestones.due_date DESC")
else issues
end
end
end
end

View file

@ -45,6 +45,18 @@ module Issuable
def search(query)
where("title like :query", query: "%#{query}%")
end
def sort(method)
case method.to_s
when 'newest' then reorder('created_at DESC')
when 'oldest' then reorder('created_at ASC')
when 'recently_updated' then reorder('updated_at DESC')
when 'last_updated' then reorder('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('created_at DESC')
end
end
end
def today?