2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# Search for milestones
|
|
|
|
#
|
|
|
|
# params - Hash
|
|
|
|
# project_ids: Array of project ids or single project id.
|
|
|
|
# group_ids: Array of group ids or single group id.
|
|
|
|
# order - Orders by field default due date asc.
|
|
|
|
# title - filter by title.
|
|
|
|
# state - filters by state.
|
|
|
|
|
2015-11-26 14:37:03 +05:30
|
|
|
class MilestonesFinder
|
2018-03-27 19:54:05 +05:30
|
|
|
include FinderMethods
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
attr_reader :params, :project_ids, :group_ids
|
|
|
|
|
|
|
|
def initialize(params = {})
|
|
|
|
@project_ids = Array(params[:project_ids])
|
|
|
|
@group_ids = Array(params[:group_ids])
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return Milestone.none if project_ids.empty? && group_ids.empty?
|
|
|
|
|
|
|
|
items = Milestone.all
|
|
|
|
items = by_groups_and_projects(items)
|
|
|
|
items = by_title(items)
|
|
|
|
items = by_state(items)
|
|
|
|
|
|
|
|
order(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def by_groups_and_projects(items)
|
|
|
|
items.for_projects_and_groups(project_ids, group_ids)
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
def by_title(items)
|
|
|
|
if params[:title]
|
|
|
|
items.where(title: params[:title])
|
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
def by_state(items)
|
|
|
|
Milestone.filter_by_state(items, params[:state])
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
def order(items)
|
2018-03-17 18:26:18 +05:30
|
|
|
order_statement = Gitlab::Database.nulls_last_order('due_date', 'ASC')
|
|
|
|
items.reorder(order_statement).order('title ASC')
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|