Commit 3b2223f1 authored by Felipe Artur's avatar Felipe Artur

improve ordering sql for milestones

parent 673196e0
...@@ -37,8 +37,8 @@ module Issuable ...@@ -37,8 +37,8 @@ module Issuable
scope :closed, -> { with_state(:closed) } scope :closed, -> { with_state(:closed) }
scope :left_joins_milestones, -> { joins("LEFT OUTER JOIN milestones ON #{table_name}.milestone_id = milestones.id") } scope :left_joins_milestones, -> { joins("LEFT OUTER JOIN milestones ON #{table_name}.milestone_id = milestones.id") }
scope :order_milestone_due_desc, -> { left_joins_milestones.reorder('CASE WHEN milestones.due_date IS NULL then 0 ELSE 1 END DESC, CASE WHEN milestones.id IS NULL then 0 ELSE 1 END DESC, milestones.due_date DESC') } scope :order_milestone_due_desc, -> { left_joins_milestones.reorder('milestones.due_date IS NULL, milestones.id IS NULL, milestones.due_date DESC') }
scope :order_milestone_due_asc, -> { left_joins_milestones.reorder('CASE WHEN milestones.due_date IS NULL then 1 ELSE 0 END ASC, CASE WHEN milestones.id IS NULL then 1 ELSE 0 END ASC, milestones.due_date ASC') } scope :order_milestone_due_asc, -> { left_joins_milestones.reorder('milestones.due_date IS NULL, milestones.id IS NULL, milestones.due_date ASC') }
scope :without_label, -> { joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{name}' AND label_links.target_id = #{table_name}.id").where(label_links: { id: nil }) } scope :without_label, -> { joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{name}' AND label_links.target_id = #{table_name}.id").where(label_links: { id: nil }) }
scope :join_project, -> { joins(:project) } scope :join_project, -> { joins(:project) }
......
...@@ -115,41 +115,30 @@ describe Issue, "Issuable" do ...@@ -115,41 +115,30 @@ describe Issue, "Issuable" do
end end
describe "#sort" do describe "#sort" do
#Correct order is: let(:project) { build_stubbed(:empty_project) }
#Issues/MRs with milestones ordered by date
#Issues/MRs with milestones without dates
#Issues/MRs without milestones
let!(:issue) { create(:issue) } context "by milestone due date" do
let(:project) { issue.project } #Correct order is:
let!(:early_milestone) { create(:milestone, project: project, due_date: 10.days.from_now) } #Issues/MRs with milestones ordered by date
let!(:late_milestone) { create(:milestone, project: project, due_date: 30.days.from_now) } #Issues/MRs with milestones without dates
let!(:issue1) { create(:issue, project: project, milestone: early_milestone) } #Issues/MRs without milestones
let!(:issue2) { create(:issue, project: project, milestone: late_milestone) }
let!(:issue3) { create(:issue, project: project) }
context "milestone due later" do
subject { Issue.where(project_id: project.id).order_milestone_due_desc }
before { @issues = subject }
it "puts issues with nil values at the end of collection" do
expect(@issues.first).to eq(issue2)
expect(@issues.second).to eq(issue1)
expect(@issues.third).to eq(issue)
expect(@issues.fourth).to eq(issue3)
end
end
context "milestone due soon" do let!(:issue) { create(:issue, project: project) }
subject { Issue.where(project_id: project.id).order_milestone_due_asc } let!(:early_milestone) { create(:milestone, project: project, due_date: 10.days.from_now) }
before { @issues = subject } let!(:late_milestone) { create(:milestone, project: project, due_date: 30.days.from_now) }
let!(:issue1) { create(:issue, project: project, milestone: early_milestone) }
let!(:issue2) { create(:issue, project: project, milestone: late_milestone) }
let!(:issue3) { create(:issue, project: project) }
it "sorts desc" do
issues = project.issues.sort('milestone_due_desc')
expect(issues).to match_array([issue2, issue1, issue, issue3])
end
it "puts issues with nil values at the end of collection" do it "sorts asc" do
expect(@issues.first).to eq(issue1) issues = project.issues.sort('milestone_due_asc')
expect(@issues.second).to eq(issue2) expect(issues).to match_array([issue1, issue2, issue, issue3])
expect(@issues.third).to eq(issue)
expect(@issues.fourth).to eq(issue3)
end end
end end
end end
......
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