Don't use MilestoneFinder for group milestones

This refactors the DashboardGroupMilestone model so that it no
longer uses the MilestoneFinder, instead using methods defined
on the Milestone model directly.
parent a2a00a5e
...@@ -13,7 +13,11 @@ class DashboardGroupMilestone < GlobalMilestone ...@@ -13,7 +13,11 @@ class DashboardGroupMilestone < GlobalMilestone
end end
def self.build_collection(groups) def self.build_collection(groups)
MilestonesFinder.new(group_ids: groups.select(:id)).execute.map { |m| new(m) } # rubocop: disable CodeReuse/Finder Milestone.of_groups(groups.select(:id))
.reorder_by_due_date_asc
.order_by_name_asc
.active
.map { |m| new(m) }
end end
override :group_milestone? override :group_milestone?
......
...@@ -46,6 +46,9 @@ class Milestone < ActiveRecord::Base ...@@ -46,6 +46,9 @@ class Milestone < ActiveRecord::Base
where(conditions.reduce(:or)) where(conditions.reduce(:or))
end end
scope :order_by_name_asc, -> { order(Arel::Nodes::Ascending.new(arel_table[:title].lower)) }
scope :reorder_by_due_date_asc, -> { reorder(Gitlab::Database.nulls_last_order('due_date', 'ASC')) }
validates :group, presence: true, unless: :project validates :group, presence: true, unless: :project
validates :project, presence: true, unless: :group validates :project, presence: true, unless: :group
...@@ -149,7 +152,7 @@ class Milestone < ActiveRecord::Base ...@@ -149,7 +152,7 @@ class Milestone < ActiveRecord::Base
sorted = sorted =
case method.to_s case method.to_s
when 'due_date_asc' when 'due_date_asc'
reorder(Gitlab::Database.nulls_last_order('due_date', 'ASC')) reorder_by_due_date_asc
when 'due_date_desc' when 'due_date_desc'
reorder(Gitlab::Database.nulls_last_order('due_date', 'DESC')) reorder(Gitlab::Database.nulls_last_order('due_date', 'DESC'))
when 'name_asc' when 'name_asc'
......
...@@ -95,6 +95,24 @@ describe Milestone do ...@@ -95,6 +95,24 @@ describe Milestone do
end end
end end
describe '.order_by_name_asc' do
it 'sorts by name ascending' do
milestone1 = create(:milestone, title: 'Foo')
milestone2 = create(:milestone, title: 'Bar')
expect(described_class.order_by_name_asc).to eq([milestone2, milestone1])
end
end
describe '.reorder_by_due_date_asc' do
it 'reorders the input relation' do
milestone1 = create(:milestone, due_date: Date.new(2018, 9, 30))
milestone2 = create(:milestone, due_date: Date.new(2018, 10, 20))
expect(described_class.reorder_by_due_date_asc).to eq([milestone1, milestone2])
end
end
describe "#percent_complete" do describe "#percent_complete" do
it "does not count open issues" do it "does not count open issues" do
milestone.issues << issue milestone.issues << issue
......
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