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

339 lines
7.2 KiB
Ruby
Raw Normal View History

# IssuableFinder
#
# 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
# milestone_title: string
# assignee_id: integer
# search: string
# label_name: string
# sort: string
#
require_relative 'projects_finder'
class IssuableFinder
NONE = '0'
2015-03-27 01:56:42 +00:00
attr_accessor :current_user, :params
def initialize(current_user, params)
@current_user = current_user
@params = params
end
def execute
2014-02-03 15:02:44 +00:00
items = init_collection
items = by_scope(items)
items = by_state(items)
items = by_group(items)
2014-02-03 16:56:31 +00:00
items = by_project(items)
items = by_search(items)
items = by_milestone(items)
items = by_assignee(items)
items = by_author(items)
items = by_label(items)
items = by_due_date(items)
2015-10-03 05:56:37 +00:00
sort(items)
end
def group
return @group if defined?(@group)
@group =
if params[:group_id].present?
Group.find(params[:group_id])
else
nil
end
end
def project?
params[:project_id].present?
end
def project
return @project if defined?(@project)
if project?
@project = Project.find(params[:project_id])
2016-08-08 18:55:13 +00:00
unless Ability.allowed?(current_user, :read_project, @project)
@project = nil
end
else
@project = nil
end
@project
end
def projects
return @projects if defined?(@projects)
if project?
@projects = project
elsif current_user && params[:authorized_only].presence && !current_user_related?
@projects = current_user.authorized_projects.reorder(nil)
elsif group
@projects = GroupProjectsFinder.new(group).execute(current_user).reorder(nil)
else
@projects = ProjectsFinder.new.execute(current_user).reorder(nil)
end
end
def search
params[:search].presence
end
def milestones?
params[:milestone_title].present?
end
2015-10-19 09:46:22 +00:00
def filter_by_no_milestone?
milestones? && params[:milestone_title] == Milestone::None.title
end
def milestones
return @milestones if defined?(@milestones)
@milestones =
if milestones?
scope = Milestone.where(project_id: projects)
2015-10-14 10:20:48 +00:00
scope.where(title: params[:milestone_title])
else
Milestone.none
end
end
def labels?
params[:label_name].present?
end
2015-10-19 09:46:22 +00:00
def filter_by_no_label?
labels? && params[:label_name].include?(Label::None.title)
end
2016-02-19 05:27:41 +00:00
def labels
return @labels if defined?(@labels)
if labels? && !filter_by_no_label?
@labels = Label.where(title: label_names)
if projects
2016-02-19 12:40:50 +00:00
@labels = @labels.where(project: projects)
2016-02-19 05:27:41 +00:00
end
else
@labels = Label.none
end
end
def assignee?
params[:assignee_id].present?
end
def assignee
return @assignee if defined?(@assignee)
@assignee =
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)
@author =
if author? && params[:author_id] != NONE
User.find(params[:author_id])
else
nil
end
end
private
2014-02-03 15:02:44 +00:00
def init_collection
klass.all
2014-02-03 15:02:44 +00:00
end
def by_scope(items)
2016-03-21 23:09:20 +00:00
case params[:scope]
when 'created-by-me', 'authored'
items.where(author_id: current_user.id)
2016-03-21 23:09:20 +00:00
when 'assigned-to-me'
items.where(assignee_id: current_user.id)
else
2016-03-21 23:09:20 +00:00
items
end
end
def by_state(items)
case params[:state]
when 'closed'
items.closed
when 'merged'
items.respond_to?(:merged) ? items.merged : items.closed
when 'all'
items
when 'opened'
items.opened
else
raise 'You must specify default state'
end
end
def by_group(items)
# Selection by group is already covered by `by_project` and `projects`
items
end
def by_project(items)
items =
if project?
items.of_projects(projects).references_project
elsif projects
items.merge(projects.reorder(nil)).join_project
else
items.none
end
items
end
def by_search(items)
2016-06-27 20:22:19 +00:00
if search
items =
if search =~ iid_pattern
items.where(iid: $~[:iid])
else
items.full_search(search)
end
end
items
end
def sort(items)
# Ensure we always have an explicit sort order (instead of inheriting
# multiple orders when combining ActiveRecord::Relation objects).
params[:sort] ? items.sort(params[:sort], excluded_labels: label_names) : items.reorder(id: :desc)
end
def by_assignee(items)
if assignee?
items = items.where(assignee_id: assignee.try(:id))
end
items
end
def by_author(items)
if author?
items = items.where(author_id: author.try(:id))
end
items
end
2016-03-13 12:19:27 +00:00
def filter_by_upcoming_milestone?
params[:milestone_title] == Milestone::Upcoming.name
2016-03-11 17:46:14 +00:00
end
def by_milestone(items)
if milestones?
2015-10-19 09:46:22 +00:00
if filter_by_no_milestone?
items = items.left_joins_milestones.where(milestone_id: [-1, nil])
2016-03-13 12:19:27 +00:00
elsif filter_by_upcoming_milestone?
upcoming_ids = Milestone.upcoming_ids_by_projects(projects)
items = items.left_joins_milestones.where(milestone_id: upcoming_ids)
else
items = items.with_milestone(params[:milestone_title])
if projects
items = items.where(milestones: { project_id: projects })
end
end
end
items
end
def by_label(items)
if labels?
2015-10-19 09:46:22 +00:00
if filter_by_no_label?
2016-03-07 04:07:19 +00:00
items = items.without_label
else
items = items.with_label(label_names, params[:sort])
if projects
items = items.where(labels: { project_id: projects })
end
end
end
items
end
def by_due_date(items)
if due_date?
if filter_by_no_due_date?
items = items.without_due_date
elsif filter_by_overdue?
items = items.due_before(Date.today)
elsif filter_by_due_this_week?
items = items.due_between(Date.today.beginning_of_week, Date.today.end_of_week)
elsif filter_by_due_this_month?
items = items.due_between(Date.today.beginning_of_month, Date.today.end_of_month)
end
end
items
end
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 05:27:41 +00:00
def label_names
if labels?
params[:label_name].is_a?(String) ? params[:label_name].split(',') : params[:label_name]
else
[]
end
2016-02-19 05:27:41 +00:00
end
def current_user_related?
params[:scope] == 'created-by-me' || params[:scope] == 'authored' || params[:scope] == 'assigned-to-me'
end
end