2014-09-02 08:28:27 -04:00
|
|
|
# IssuableFinder
|
2014-01-15 09:16:23 -05:00
|
|
|
#
|
|
|
|
# Used to filter Issues and MergeRequests collections by set of params
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# klass - actual class like Issue or MergeRequest
|
|
|
|
# current_user - which user use
|
|
|
|
# params:
|
|
|
|
# scope: 'created-by-me' or 'assigned-to-me' or 'all'
|
|
|
|
# state: 'open' or 'closed' or 'all'
|
|
|
|
# group_id: integer
|
|
|
|
# project_id: integer
|
2015-06-28 16:12:32 -04:00
|
|
|
# milestone_title: string
|
2014-01-15 09:16:23 -05:00
|
|
|
# assignee_id: integer
|
|
|
|
# search: string
|
|
|
|
# label_name: string
|
|
|
|
# sort: string
|
|
|
|
#
|
2014-09-02 08:28:27 -04:00
|
|
|
require_relative 'projects_finder'
|
|
|
|
|
|
|
|
class IssuableFinder
|
2015-03-27 03:27:51 -04:00
|
|
|
NONE = '0'
|
2015-03-26 21:56:42 -04:00
|
|
|
|
2014-02-25 12:15:08 -05:00
|
|
|
attr_accessor :current_user, :params
|
2014-01-15 09:16:23 -05:00
|
|
|
|
2015-05-25 07:36:28 -04:00
|
|
|
def initialize(current_user, params)
|
2014-01-15 09:16:23 -05:00
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
2015-05-25 07:36:28 -04:00
|
|
|
end
|
2014-01-15 09:16:23 -05:00
|
|
|
|
2015-05-25 07:36:28 -04:00
|
|
|
def execute
|
2014-02-03 10:02:44 -05:00
|
|
|
items = init_collection
|
|
|
|
items = by_scope(items)
|
2014-01-15 09:16:23 -05:00
|
|
|
items = by_state(items)
|
|
|
|
items = by_group(items)
|
2014-02-03 11:56:31 -05:00
|
|
|
items = by_project(items)
|
2014-01-15 09:16:23 -05:00
|
|
|
items = by_search(items)
|
|
|
|
items = by_milestone(items)
|
|
|
|
items = by_assignee(items)
|
2014-12-05 10:13:07 -05:00
|
|
|
items = by_author(items)
|
2014-01-15 09:16:23 -05:00
|
|
|
items = by_label(items)
|
2016-03-10 09:26:56 -05:00
|
|
|
items = by_due_date(items)
|
2015-10-03 01:56:37 -04:00
|
|
|
sort(items)
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|
|
|
|
|
2015-05-25 07:36:28 -04:00
|
|
|
def group
|
|
|
|
return @group if defined?(@group)
|
|
|
|
|
2015-06-18 13:15:17 -04:00
|
|
|
@group =
|
2015-05-25 07:36:28 -04:00
|
|
|
if params[:group_id].present?
|
|
|
|
Group.find(params[:group_id])
|
2015-06-18 13:15:17 -04:00
|
|
|
else
|
2015-05-25 07:36:28 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-14 06:23:49 -04:00
|
|
|
def project?
|
|
|
|
params[:project_id].present?
|
|
|
|
end
|
|
|
|
|
2015-05-25 07:36:28 -04:00
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
2016-10-26 13:34:06 -04:00
|
|
|
project = Project.find(params[:project_id])
|
|
|
|
project = nil unless Ability.allowed?(current_user, :"read_#{klass.to_ability_name}", project)
|
2015-11-11 06:49:16 -05:00
|
|
|
|
2016-10-26 13:34:06 -04:00
|
|
|
@project = project
|
2015-10-14 06:23:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def projects
|
|
|
|
return @projects if defined?(@projects)
|
2016-10-26 13:34:06 -04:00
|
|
|
return @projects = project if project?
|
|
|
|
|
|
|
|
projects =
|
|
|
|
if current_user && params[:authorized_only].presence && !current_user_related?
|
|
|
|
current_user.authorized_projects
|
|
|
|
elsif group
|
|
|
|
GroupProjectsFinder.new(group).execute(current_user)
|
|
|
|
else
|
|
|
|
ProjectsFinder.new.execute(current_user)
|
|
|
|
end
|
2015-10-14 06:23:49 -04:00
|
|
|
|
2016-10-26 13:34:06 -04:00
|
|
|
@projects = projects.with_feature_available_for_user(klass, current_user).reorder(nil)
|
2015-05-25 07:36:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def search
|
|
|
|
params[:search].presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def milestones?
|
|
|
|
params[:milestone_title].present?
|
|
|
|
end
|
|
|
|
|
2015-10-19 05:46:22 -04:00
|
|
|
def filter_by_no_milestone?
|
2015-10-06 12:57:13 -04:00
|
|
|
milestones? && params[:milestone_title] == Milestone::None.title
|
|
|
|
end
|
|
|
|
|
2015-05-25 07:36:28 -04:00
|
|
|
def milestones
|
|
|
|
return @milestones if defined?(@milestones)
|
|
|
|
|
|
|
|
@milestones =
|
2015-10-06 12:57:13 -04:00
|
|
|
if milestones?
|
2015-10-16 05:43:26 -04:00
|
|
|
scope = Milestone.where(project_id: projects)
|
2015-10-14 06:20:48 -04:00
|
|
|
|
|
|
|
scope.where(title: params[:milestone_title])
|
2015-05-25 07:36:28 -04:00
|
|
|
else
|
2016-07-23 19:28:12 -04:00
|
|
|
Milestone.none
|
2015-05-25 07:36:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-16 05:43:26 -04:00
|
|
|
def labels?
|
|
|
|
params[:label_name].present?
|
|
|
|
end
|
|
|
|
|
2015-10-19 05:46:22 -04:00
|
|
|
def filter_by_no_label?
|
2016-04-20 12:23:10 -04:00
|
|
|
labels? && params[:label_name].include?(Label::None.title)
|
2015-10-16 05:43:26 -04:00
|
|
|
end
|
|
|
|
|
2016-02-19 00:27:41 -05:00
|
|
|
def labels
|
|
|
|
return @labels if defined?(@labels)
|
|
|
|
|
2016-10-17 14:55:46 -04:00
|
|
|
@labels =
|
|
|
|
if labels? && !filter_by_no_label?
|
2016-10-27 22:50:38 -04:00
|
|
|
LabelsFinder.new(current_user, project_ids: projects, title: label_names).execute(skip_authorization: true)
|
2016-10-17 14:55:46 -04:00
|
|
|
else
|
|
|
|
Label.none
|
2016-02-19 00:27:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-25 07:36:28 -04:00
|
|
|
def assignee?
|
|
|
|
params[:assignee_id].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def assignee
|
|
|
|
return @assignee if defined?(@assignee)
|
|
|
|
|
2015-06-18 13:15:17 -04:00
|
|
|
@assignee =
|
2015-05-25 07:36:28 -04:00
|
|
|
if assignee? && params[:assignee_id] != NONE
|
|
|
|
User.find(params[:assignee_id])
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def author?
|
|
|
|
params[:author_id].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def author
|
|
|
|
return @author if defined?(@author)
|
|
|
|
|
2015-06-18 13:15:17 -04:00
|
|
|
@author =
|
2015-05-25 07:36:28 -04:00
|
|
|
if author? && params[:author_id] != NONE
|
|
|
|
User.find(params[:author_id])
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-15 09:16:23 -05:00
|
|
|
private
|
|
|
|
|
2014-02-03 10:02:44 -05:00
|
|
|
def init_collection
|
2015-10-14 06:23:49 -04:00
|
|
|
klass.all
|
2014-02-03 10:02:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def by_scope(items)
|
2016-03-21 19:09:20 -04:00
|
|
|
case params[:scope]
|
|
|
|
when 'created-by-me', 'authored'
|
2014-02-10 08:04:52 -05:00
|
|
|
items.where(author_id: current_user.id)
|
2016-03-21 19:09:20 -04:00
|
|
|
when 'assigned-to-me'
|
2014-02-10 08:04:52 -05:00
|
|
|
items.where(assignee_id: current_user.id)
|
2014-01-15 09:16:23 -05:00
|
|
|
else
|
2016-03-21 19:09:20 -04:00
|
|
|
items
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_state(items)
|
2016-09-23 09:16:11 -04:00
|
|
|
params[:state] ||= 'all'
|
|
|
|
|
|
|
|
if items.respond_to?(params[:state])
|
|
|
|
items.public_send(params[:state])
|
2014-01-15 09:16:23 -05:00
|
|
|
else
|
2016-09-23 09:16:11 -04:00
|
|
|
items
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_group(items)
|
2016-03-20 16:14:39 -04:00
|
|
|
# Selection by group is already covered by `by_project` and `projects`
|
2014-01-15 09:16:23 -05:00
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_project(items)
|
2015-10-14 06:23:49 -04:00
|
|
|
items =
|
2015-11-11 06:50:36 -05:00
|
|
|
if project?
|
|
|
|
items.of_projects(projects).references_project
|
|
|
|
elsif projects
|
|
|
|
items.merge(projects.reorder(nil)).join_project
|
2015-10-14 06:23:49 -04:00
|
|
|
else
|
|
|
|
items.none
|
|
|
|
end
|
2014-01-15 09:16:23 -05:00
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_search(items)
|
2016-06-27 16:22:19 -04:00
|
|
|
if search
|
|
|
|
items =
|
|
|
|
if search =~ iid_pattern
|
|
|
|
items.where(iid: $~[:iid])
|
|
|
|
else
|
|
|
|
items.full_search(search)
|
|
|
|
end
|
|
|
|
end
|
2014-01-15 09:16:23 -05:00
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort(items)
|
2015-11-11 06:50:36 -05:00
|
|
|
# Ensure we always have an explicit sort order (instead of inheriting
|
|
|
|
# multiple orders when combining ActiveRecord::Relation objects).
|
2016-05-13 11:26:18 -04:00
|
|
|
params[:sort] ? items.sort(params[:sort], excluded_labels: label_names) : items.reorder(id: :desc)
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def by_assignee(items)
|
2015-05-25 07:36:28 -04:00
|
|
|
if assignee?
|
|
|
|
items = items.where(assignee_id: assignee.try(:id))
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2014-12-05 10:13:07 -05:00
|
|
|
def by_author(items)
|
2015-05-25 07:36:28 -04:00
|
|
|
if author?
|
|
|
|
items = items.where(author_id: author.try(:id))
|
2014-12-05 10:13:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2016-03-13 08:19:27 -04:00
|
|
|
def filter_by_upcoming_milestone?
|
2016-03-24 11:20:35 -04:00
|
|
|
params[:milestone_title] == Milestone::Upcoming.name
|
2016-03-11 12:46:14 -05:00
|
|
|
end
|
|
|
|
|
2015-10-16 05:43:26 -04:00
|
|
|
def by_milestone(items)
|
|
|
|
if milestones?
|
2015-10-19 05:46:22 -04:00
|
|
|
if filter_by_no_milestone?
|
2016-05-19 18:30:38 -04:00
|
|
|
items = items.left_joins_milestones.where(milestone_id: [-1, nil])
|
2016-03-13 08:19:27 -04:00
|
|
|
elsif filter_by_upcoming_milestone?
|
2016-05-11 12:38:34 -04:00
|
|
|
upcoming_ids = Milestone.upcoming_ids_by_projects(projects)
|
2016-04-27 11:35:30 -04:00
|
|
|
items = items.left_joins_milestones.where(milestone_id: upcoming_ids)
|
2015-10-16 05:43:26 -04:00
|
|
|
else
|
2016-05-19 18:30:38 -04:00
|
|
|
items = items.with_milestone(params[:milestone_title])
|
2015-10-16 05:43:26 -04:00
|
|
|
|
|
|
|
if projects
|
|
|
|
items = items.where(milestones: { project_id: projects })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2014-01-15 09:16:23 -05:00
|
|
|
def by_label(items)
|
2015-10-16 05:43:26 -04:00
|
|
|
if labels?
|
2015-10-19 05:46:22 -04:00
|
|
|
if filter_by_no_label?
|
2016-03-06 23:07:19 -05:00
|
|
|
items = items.without_label
|
2015-10-06 12:57:13 -04:00
|
|
|
else
|
2016-05-27 15:53:20 -04:00
|
|
|
items = items.with_label(label_names, params[:sort])
|
2016-09-19 16:21:39 -04:00
|
|
|
|
2015-10-16 05:43:26 -04:00
|
|
|
if projects
|
2016-10-27 22:50:38 -04:00
|
|
|
label_ids = LabelsFinder.new(current_user, project_ids: projects).execute(skip_authorization: true).select(:id)
|
2016-09-19 16:21:39 -04:00
|
|
|
items = items.where(labels: { id: label_ids })
|
2015-10-14 06:23:49 -04:00
|
|
|
end
|
2015-10-06 12:57:13 -04:00
|
|
|
end
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|
|
|
|
|
2016-04-21 12:49:08 -04:00
|
|
|
items
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|
2014-01-31 11:06:13 -05:00
|
|
|
|
2016-03-10 09:26:56 -05:00
|
|
|
def by_due_date(items)
|
|
|
|
if due_date?
|
|
|
|
if filter_by_no_due_date?
|
2016-03-18 13:17:01 -04:00
|
|
|
items = items.without_due_date
|
|
|
|
elsif filter_by_overdue?
|
2016-04-19 07:03:28 -04:00
|
|
|
items = items.due_before(Date.today)
|
2016-03-18 13:17:01 -04:00
|
|
|
elsif filter_by_due_this_week?
|
2016-04-19 07:03:28 -04:00
|
|
|
items = items.due_between(Date.today.beginning_of_week, Date.today.end_of_week)
|
2016-03-18 13:17:01 -04:00
|
|
|
elsif filter_by_due_this_month?
|
2016-04-19 07:03:28 -04:00
|
|
|
items = items.due_between(Date.today.beginning_of_month, Date.today.end_of_month)
|
2016-03-10 09:26:56 -05:00
|
|
|
end
|
|
|
|
end
|
2016-04-19 07:03:28 -04:00
|
|
|
|
2014-01-15 09:16:23 -05:00
|
|
|
items
|
|
|
|
end
|
2014-01-31 11:06:13 -05:00
|
|
|
|
2016-04-20 08:41:50 -04:00
|
|
|
def filter_by_no_due_date?
|
|
|
|
due_date? && params[:due_date] == Issue::NoDueDate.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_overdue?
|
|
|
|
due_date? && params[:due_date] == Issue::Overdue.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_due_this_week?
|
|
|
|
due_date? && params[:due_date] == Issue::DueThisWeek.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_due_this_month?
|
|
|
|
due_date? && params[:due_date] == Issue::DueThisMonth.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def due_date?
|
|
|
|
params[:due_date].present? && klass.column_names.include?('due_date')
|
|
|
|
end
|
|
|
|
|
2016-02-19 00:27:41 -05:00
|
|
|
def label_names
|
2016-03-14 05:46:26 -04:00
|
|
|
if labels?
|
|
|
|
params[:label_name].is_a?(String) ? params[:label_name].split(',') : params[:label_name]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2016-02-19 00:27:41 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 05:02:20 -04:00
|
|
|
def current_user_related?
|
|
|
|
params[:scope] == 'created-by-me' || params[:scope] == 'authored' || params[:scope] == 'assigned-to-me'
|
|
|
|
end
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|