gitlab-org--gitlab-foss/app/finders/milestones_finder.rb

74 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-07-07 11:08:49 -04:00
# Search for milestones
#
# params - Hash
# ids - filters by id.
# project_ids: Array of project ids or single project id or ActiveRecord relation.
# group_ids: Array of group ids or single group id or ActiveRecord relation.
2017-07-07 11:08:49 -04:00
# order - Orders by field default due date asc.
# title - filter by title.
# state - filters by state.
class MilestonesFinder
include FinderMethods
include TimeFrameFilter
attr_reader :params
2017-07-07 11:08:49 -04:00
def initialize(params = {})
@params = params
end
def execute
items = Milestone.all
items = by_ids(items)
2017-07-07 11:08:49 -04:00
items = by_groups_and_projects(items)
items = by_title(items)
items = by_search_title(items)
2017-07-07 11:08:49 -04:00
items = by_state(items)
items = by_timeframe(items)
2017-07-07 11:08:49 -04:00
order(items)
end
private
def by_ids(items)
return items unless params[:ids].present?
items.id_in(params[:ids])
end
2017-07-07 11:08:49 -04:00
def by_groups_and_projects(items)
items.for_projects_and_groups(params[:project_ids], params[:group_ids])
2017-07-07 11:08:49 -04:00
end
# rubocop: disable CodeReuse/ActiveRecord
2017-07-07 11:08:49 -04:00
def by_title(items)
if params[:title]
items.where(title: params[:title])
else
items
end
end
# rubocop: enable CodeReuse/ActiveRecord
2017-07-07 11:08:49 -04:00
def by_search_title(items)
if params[:search_title].present?
items.search_title(params[:search_title])
else
items
end
end
2017-07-07 11:08:49 -04:00
def by_state(items)
Milestone.filter_by_state(items, params[:state])
end
def order(items)
sort_by = params[:sort].presence || 'due_date_asc'
items.sort_by_attribute(sort_by)
end
end