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)
|
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
|
|
|
|
|
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
2015-06-18 13:15:17 -04:00
|
|
|
@project =
|
2015-05-25 07:36:28 -04:00
|
|
|
if params[:project_id].present?
|
|
|
|
Project.find(params[:project_id])
|
2015-06-18 13:15:17 -04:00
|
|
|
else
|
2015-05-25 07:36:28 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search
|
|
|
|
params[:search].presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def milestones?
|
|
|
|
params[:milestone_title].present?
|
|
|
|
end
|
|
|
|
|
2015-10-06 12:57:13 -04:00
|
|
|
def no_milestones?
|
|
|
|
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-05-25 07:36:28 -04:00
|
|
|
Milestone.where(title: params[:milestone_title])
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2014-01-15 09:16:23 -05:00
|
|
|
table_name = klass.table_name
|
|
|
|
|
2014-02-03 10:02:44 -05:00
|
|
|
if project
|
2014-10-19 04:02:13 -04:00
|
|
|
if Ability.abilities.allowed?(current_user, :read_project, project)
|
2014-02-03 10:02:44 -05:00
|
|
|
project.send(table_name)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2014-10-27 05:02:20 -04:00
|
|
|
elsif current_user && params[:authorized_only].presence && !current_user_related?
|
2014-03-18 20:05:10 -04:00
|
|
|
klass.of_projects(current_user.authorized_projects).references(:project)
|
2014-02-13 15:45:51 -05:00
|
|
|
else
|
2014-06-05 13:37:35 -04:00
|
|
|
klass.of_projects(ProjectsFinder.new.execute(current_user)).references(:project)
|
2014-02-03 10:02:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_scope(items)
|
2014-01-15 09:16:23 -05:00
|
|
|
case params[:scope]
|
|
|
|
when 'created-by-me', 'authored' then
|
2014-02-10 08:04:52 -05:00
|
|
|
items.where(author_id: current_user.id)
|
2014-01-15 09:16:23 -05:00
|
|
|
when 'all' then
|
2014-02-10 08:04:52 -05:00
|
|
|
items
|
2014-01-15 09:16:23 -05:00
|
|
|
when 'assigned-to-me' then
|
2014-02-10 08:04:52 -05:00
|
|
|
items.where(assignee_id: current_user.id)
|
2014-01-15 09:16:23 -05:00
|
|
|
else
|
|
|
|
raise 'You must specify default scope'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_state(items)
|
|
|
|
case params[:state]
|
|
|
|
when 'closed'
|
|
|
|
items.closed
|
2015-05-25 11:01:27 -04:00
|
|
|
when 'merged'
|
|
|
|
items.respond_to?(:merged) ? items.merged : items.closed
|
2014-01-15 09:16:23 -05:00
|
|
|
when 'all'
|
|
|
|
items
|
|
|
|
when 'opened'
|
|
|
|
items.opened
|
|
|
|
else
|
|
|
|
raise 'You must specify default state'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_group(items)
|
2015-05-25 07:36:28 -04:00
|
|
|
items = items.of_group(group) if group
|
2014-01-15 09:16:23 -05:00
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_project(items)
|
2015-05-25 07:36:28 -04:00
|
|
|
items = items.of_projects(project.id) if project
|
2014-01-15 09:16:23 -05:00
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_search(items)
|
2015-05-25 07:36:28 -04:00
|
|
|
items = items.search(search) if search
|
2014-01-15 09:16:23 -05:00
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort(items)
|
|
|
|
items.sort(params[:sort])
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_milestone(items)
|
2015-05-25 07:36:28 -04:00
|
|
|
if milestones?
|
2015-10-06 12:57:13 -04:00
|
|
|
if no_milestones?
|
|
|
|
items = items.where(milestone_id: [-1, nil])
|
|
|
|
else
|
|
|
|
items = items.where(milestone_id: milestones.try(:pluck, :id))
|
|
|
|
end
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
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
|
|
|
|
|
2014-01-15 09:16:23 -05:00
|
|
|
def by_label(items)
|
|
|
|
if params[:label_name].present?
|
2015-10-06 12:57:13 -04:00
|
|
|
if params[:label_name] == Label::None.title
|
|
|
|
item_ids = LabelLink.where(target_type: klass.name).pluck(:target_id)
|
2014-07-30 06:26:23 -04:00
|
|
|
|
2015-10-06 12:57:13 -04:00
|
|
|
items = items.where('id NOT IN (?)', item_ids)
|
|
|
|
else
|
|
|
|
label_names = params[:label_name].split(",")
|
|
|
|
|
|
|
|
item_ids = LabelLink.joins(:label).
|
|
|
|
where('labels.title in (?)', label_names).
|
|
|
|
where(target_type: klass.name).pluck(:target_id)
|
2014-07-30 06:26:23 -04:00
|
|
|
|
2015-10-06 12:57:13 -04:00
|
|
|
items = items.where(id: item_ids)
|
|
|
|
end
|
2014-01-15 09:16:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
2014-01-31 11:06:13 -05:00
|
|
|
|
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
|