2015-04-26 12:48:37 +05:30
|
|
|
# IssuableFinder
|
2014-09-02 18:07:02 +05:30
|
|
|
#
|
|
|
|
# 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-09-11 14:41:01 +05:30
|
|
|
# milestone_title: string
|
2014-09-02 18:07:02 +05:30
|
|
|
# assignee_id: integer
|
|
|
|
# search: string
|
|
|
|
# label_name: string
|
|
|
|
# sort: string
|
|
|
|
#
|
2015-04-26 12:48:37 +05:30
|
|
|
require_relative 'projects_finder'
|
|
|
|
|
|
|
|
class IssuableFinder
|
|
|
|
NONE = '0'
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
attr_accessor :current_user, :params
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def initialize(current_user, params)
|
2014-09-02 18:07:02 +05:30
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def execute
|
2014-09-02 18:07:02 +05:30
|
|
|
items = init_collection
|
|
|
|
items = by_scope(items)
|
|
|
|
items = by_state(items)
|
|
|
|
items = by_group(items)
|
|
|
|
items = by_project(items)
|
|
|
|
items = by_search(items)
|
|
|
|
items = by_milestone(items)
|
|
|
|
items = by_assignee(items)
|
2015-04-26 12:48:37 +05:30
|
|
|
items = by_author(items)
|
2014-09-02 18:07:02 +05:30
|
|
|
items = by_label(items)
|
2015-10-24 18:46:33 +05:30
|
|
|
sort(items)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def group
|
|
|
|
return @group if defined?(@group)
|
|
|
|
|
|
|
|
@group =
|
|
|
|
if params[:group_id].present?
|
|
|
|
Group.find(params[:group_id])
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
def project?
|
|
|
|
params[:project_id].present?
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
if project?
|
|
|
|
@project = Project.find(params[:project_id])
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
unless Ability.abilities.allowed?(current_user, :read_project, @project)
|
|
|
|
@project = nil
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
else
|
|
|
|
@project = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@project
|
|
|
|
end
|
|
|
|
|
|
|
|
def projects
|
|
|
|
return @projects if defined?(@projects)
|
|
|
|
|
|
|
|
if project?
|
2015-12-23 02:04:40 +05:30
|
|
|
@projects = project
|
2015-10-24 18:46:33 +05:30
|
|
|
elsif current_user && params[:authorized_only].presence && !current_user_related?
|
2016-01-14 18:37:52 +05:30
|
|
|
@projects = current_user.authorized_projects.reorder(nil)
|
2015-10-24 18:46:33 +05:30
|
|
|
else
|
2016-04-02 18:10:28 +05:30
|
|
|
@projects = ProjectsFinder.new.execute(current_user, group: group).
|
|
|
|
reorder(nil)
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def search
|
|
|
|
params[:search].presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def milestones?
|
|
|
|
params[:milestone_title].present?
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
def filter_by_no_milestone?
|
|
|
|
milestones? && params[:milestone_title] == Milestone::None.title
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def milestones
|
|
|
|
return @milestones if defined?(@milestones)
|
|
|
|
|
|
|
|
@milestones =
|
2015-10-24 18:46:33 +05:30
|
|
|
if milestones?
|
|
|
|
scope = Milestone.where(project_id: projects)
|
|
|
|
|
|
|
|
scope.where(title: params[:milestone_title])
|
2015-09-11 14:41:01 +05:30
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
def labels?
|
|
|
|
params[:label_name].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_no_label?
|
|
|
|
labels? && params[:label_name] == Label::None.title
|
|
|
|
end
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
def labels
|
|
|
|
return @labels if defined?(@labels)
|
|
|
|
|
|
|
|
if labels? && !filter_by_no_label?
|
|
|
|
@labels = Label.where(title: label_names)
|
|
|
|
|
|
|
|
if projects
|
|
|
|
@labels = @labels.where(project: projects)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@labels = Label.none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
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
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def init_collection
|
2015-10-24 18:46:33 +05:30
|
|
|
klass.all
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def by_scope(items)
|
|
|
|
case params[:scope]
|
|
|
|
when 'created-by-me', 'authored' then
|
|
|
|
items.where(author_id: current_user.id)
|
|
|
|
when 'all' then
|
|
|
|
items
|
|
|
|
when 'assigned-to-me' then
|
|
|
|
items.where(assignee_id: current_user.id)
|
|
|
|
else
|
|
|
|
raise 'You must specify default scope'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_state(items)
|
|
|
|
case params[:state]
|
|
|
|
when 'closed'
|
|
|
|
items.closed
|
2015-09-11 14:41:01 +05:30
|
|
|
when 'merged'
|
|
|
|
items.respond_to?(:merged) ? items.merged : items.closed
|
2014-09-02 18:07:02 +05:30
|
|
|
when 'all'
|
|
|
|
items
|
|
|
|
when 'opened'
|
|
|
|
items.opened
|
|
|
|
else
|
|
|
|
raise 'You must specify default state'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_group(items)
|
2015-09-11 14:41:01 +05:30
|
|
|
items = items.of_group(group) if group
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_project(items)
|
2015-10-24 18:46:33 +05:30
|
|
|
items =
|
2015-12-23 02:04:40 +05:30
|
|
|
if project?
|
|
|
|
items.of_projects(projects).references_project
|
|
|
|
elsif projects
|
|
|
|
items.merge(projects.reorder(nil)).join_project
|
2015-10-24 18:46:33 +05:30
|
|
|
else
|
|
|
|
items.none
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_search(items)
|
2015-09-11 14:41:01 +05:30
|
|
|
items = items.search(search) if search
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort(items)
|
2015-12-23 02:04:40 +05:30
|
|
|
# Ensure we always have an explicit sort order (instead of inheriting
|
|
|
|
# multiple orders when combining ActiveRecord::Relation objects).
|
|
|
|
params[:sort] ? items.sort(params[:sort]) : items.reorder(id: :desc)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def by_assignee(items)
|
2015-09-11 14:41:01 +05:30
|
|
|
if assignee?
|
|
|
|
items = items.where(assignee_id: assignee.try(:id))
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_author(items)
|
2015-09-11 14:41:01 +05:30
|
|
|
if author?
|
|
|
|
items = items.where(author_id: author.try(:id))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
def by_milestone(items)
|
|
|
|
if milestones?
|
|
|
|
if filter_by_no_milestone?
|
|
|
|
items = items.where(milestone_id: [-1, nil])
|
|
|
|
else
|
|
|
|
items = items.joins(:milestone).where(milestones: { title: params[:milestone_title] })
|
|
|
|
|
|
|
|
if projects
|
|
|
|
items = items.where(milestones: { project_id: projects })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def by_label(items)
|
2015-10-24 18:46:33 +05:30
|
|
|
if labels?
|
|
|
|
if filter_by_no_label?
|
|
|
|
items = items.
|
|
|
|
joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{klass.name}' AND label_links.target_id = #{klass.table_name}.id").
|
|
|
|
where(label_links: { id: nil })
|
|
|
|
else
|
|
|
|
items = items.joins(:labels).where(labels: { title: label_names })
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
if projects
|
|
|
|
items = items.where(labels: { project_id: projects })
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
def label_names
|
|
|
|
params[:label_name].split(',')
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def current_user_related?
|
|
|
|
params[:scope] == 'created-by-me' || params[:scope] == 'authored' || params[:scope] == 'assigned-to-me'
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|