2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-07 11:08:49 -04:00
|
|
|
# Search for milestones
|
|
|
|
#
|
|
|
|
# params - Hash
|
2020-07-30 14:09:39 -04:00
|
|
|
# ids - filters by id.
|
2019-01-11 09:58:18 -05:00
|
|
|
# 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.
|
2020-10-13 23:08:32 -04:00
|
|
|
# start_date & end_date - filters by timeframe (see TimeFrameFilter)
|
|
|
|
# containing_date - filters by point in time (see TimeFrameFilter)
|
2017-07-07 11:08:49 -04:00
|
|
|
|
2015-11-13 13:20:48 -05:00
|
|
|
class MilestonesFinder
|
2017-12-11 09:21:06 -05:00
|
|
|
include FinderMethods
|
2020-02-05 07:09:15 -05:00
|
|
|
include TimeFrameFilter
|
2017-12-11 09:21:06 -05:00
|
|
|
|
2019-01-11 09:58:18 -05:00
|
|
|
attr_reader :params
|
2017-07-07 11:08:49 -04:00
|
|
|
|
2021-07-02 08:08:31 -04:00
|
|
|
EXPIRED_LAST_SORTS = %i[expired_last_due_date_asc expired_last_due_date_desc].freeze
|
|
|
|
|
2017-07-07 11:08:49 -04:00
|
|
|
def initialize(params = {})
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
items = Milestone.all
|
2020-07-30 14:09:39 -04:00
|
|
|
items = by_ids(items)
|
2017-07-07 11:08:49 -04:00
|
|
|
items = by_groups_and_projects(items)
|
|
|
|
items = by_title(items)
|
2019-01-17 09:35:23 -05:00
|
|
|
items = by_search_title(items)
|
2017-07-07 11:08:49 -04:00
|
|
|
items = by_state(items)
|
2020-02-05 07:09:15 -05:00
|
|
|
items = by_timeframe(items)
|
2020-10-13 23:08:32 -04:00
|
|
|
items = containing_date(items)
|
2017-07-07 11:08:49 -04:00
|
|
|
|
|
|
|
order(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-30 14:09:39 -04:00
|
|
|
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)
|
2019-01-11 09:58:18 -05:00
|
|
|
items.for_projects_and_groups(params[:project_ids], params[:group_ids])
|
2017-07-07 11:08:49 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# 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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-07-07 11:08:49 -04:00
|
|
|
|
2019-01-17 09:35:23 -05: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)
|
2021-07-02 08:08:31 -04:00
|
|
|
sort_by = params[:sort].presence || :due_date_asc
|
|
|
|
|
|
|
|
if sort_by_expired_last?(sort_by)
|
|
|
|
items.sort_with_expired_last(sort_by)
|
|
|
|
else
|
|
|
|
items.sort_by_attribute(sort_by)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_by_expired_last?(sort_by)
|
|
|
|
EXPIRED_LAST_SORTS.include?(sort_by)
|
2015-11-13 13:20:48 -05:00
|
|
|
end
|
|
|
|
end
|