Commit 9f1d34d1 authored by Jan Provaznik's avatar Jan Provaznik

Allow to order epics by start or end date

This implements sorting used on epics roadmap page as described
here:
https://gitlab.com/gitlab-org/gitlab-ee/issues/3559#sorting-logic-of-epics

It sorts epics by start_date, if an epic does not have start_date set,
it uses end_date in sorting.

Related #3559
parent 6ac773c1
......@@ -14,6 +14,13 @@ module EE
has_many :epic_issues
validates :group, presence: true
scope :order_start_or_end_date_asc, -> do
# mysql returns null values first in opposite to postgres which
# returns them last by default
nulls_first = ::Gitlab::Database.postgresql? ? 'NULLS FIRST' : ''
reorder("COALESCE(start_date, end_date) ASC #{nulls_first}")
end
end
module ClassMethods
......@@ -54,6 +61,14 @@ module EE
)
}x
end
def order_by(method)
if method.to_s == 'start_or_end_date'
order_start_or_end_date_asc
else
super
end
end
end
def assignees
......
......@@ -23,6 +23,19 @@ describe Epic do
it { is_expected.to include_module(InternalId) }
end
describe '.order_start_or_end_date_asc' do
let(:group) { create(:group) }
it 'returns epics sorted by start or end date' do
epic1 = create(:epic, group: group, start_date: 7.days.ago, end_date: 3.days.ago)
epic2 = create(:epic, group: group, start_date: 3.days.ago)
epic3 = create(:epic, group: group, end_date: 5.days.ago)
epic4 = create(:epic, group: group)
expect(described_class.order_start_or_end_date_asc).to eq([epic4, epic1, epic3, epic2])
end
end
describe '#issues' do
let(:user) { create(:user) }
let(:group) { create(:group, :private) }
......
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