2016-02-20 08:59:59 -05:00
|
|
|
# TodosFinder
|
2016-02-17 22:20:31 -05:00
|
|
|
#
|
2016-02-20 08:59:59 -05:00
|
|
|
# Used to filter Todos by set of params
|
2016-02-17 22:20:31 -05:00
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - which user use
|
|
|
|
# params:
|
|
|
|
# action_id: integer
|
|
|
|
# author_id: integer
|
|
|
|
# project_id; integer
|
2016-07-11 02:10:04 -04:00
|
|
|
# state: 'pending' (default) or 'done'
|
2016-02-17 22:20:31 -05:00
|
|
|
# type: 'Issue' or 'MergeRequest'
|
|
|
|
#
|
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
class TodosFinder
|
2017-12-11 09:21:06 -05:00
|
|
|
prepend FinderWithCrossProjectAccess
|
|
|
|
include FinderMethods
|
|
|
|
|
|
|
|
requires_cross_project_access unless: -> { project? }
|
|
|
|
|
2017-02-21 18:32:18 -05:00
|
|
|
NONE = '0'.freeze
|
2016-02-17 22:20:31 -05:00
|
|
|
|
|
|
|
attr_accessor :current_user, :params
|
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
def initialize(current_user, params = {})
|
2016-02-17 22:20:31 -05:00
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-06-01 19:44:35 -04:00
|
|
|
items = current_user.todos
|
2016-02-17 22:20:31 -05:00
|
|
|
items = by_action_id(items)
|
2016-06-22 13:15:09 -04:00
|
|
|
items = by_action(items)
|
2016-02-17 22:20:31 -05:00
|
|
|
items = by_author(items)
|
|
|
|
items = by_state(items)
|
|
|
|
items = by_type(items)
|
2016-08-12 08:29:59 -04:00
|
|
|
# Filtering by project HAS TO be the last because we use
|
|
|
|
# the project IDs yielded by the todos query thus far
|
|
|
|
items = by_project(items)
|
2016-02-17 22:20:31 -05:00
|
|
|
|
2016-07-26 17:21:20 -04:00
|
|
|
sort(items)
|
2016-02-17 22:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def action_id?
|
2017-06-02 13:11:26 -04:00
|
|
|
action_id.present? && Todo::ACTION_NAMES.key?(action_id.to_i)
|
2016-02-17 22:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def action_id
|
|
|
|
params[:action_id]
|
|
|
|
end
|
|
|
|
|
2016-06-22 13:15:09 -04:00
|
|
|
def to_action_id
|
|
|
|
Todo::ACTION_NAMES.key(action.to_sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
def action?
|
|
|
|
action.present? && to_action_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def action
|
|
|
|
params[:action]
|
|
|
|
end
|
|
|
|
|
2016-02-17 22:20:31 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
def project?
|
|
|
|
params[:project_id].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
|
|
|
if project?
|
|
|
|
@project = Project.find(params[:project_id])
|
|
|
|
|
2017-06-15 08:06:49 -04:00
|
|
|
@project = nil if @project.pending_delete?
|
|
|
|
|
2016-08-08 14:55:13 -04:00
|
|
|
unless Ability.allowed?(current_user, :read_project, @project)
|
2016-02-17 22:20:31 -05:00
|
|
|
@project = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@project = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@project
|
|
|
|
end
|
|
|
|
|
2018-07-11 12:33:24 -04:00
|
|
|
def project_ids(items)
|
|
|
|
ids = items.except(:order).select(:project_id)
|
|
|
|
if Gitlab::Database.mysql?
|
|
|
|
# To make UPDATE work on MySQL, wrap it in a SELECT with an alias
|
|
|
|
ids = Todo.except(:order).select('*').from("(#{ids.to_sql}) AS t")
|
2018-06-13 12:11:10 -04:00
|
|
|
end
|
2018-07-11 12:33:24 -04:00
|
|
|
|
|
|
|
ids
|
2018-06-13 12:11:10 -04:00
|
|
|
end
|
|
|
|
|
2016-02-17 22:20:31 -05:00
|
|
|
def type?
|
2018-07-11 12:33:24 -04:00
|
|
|
type.present? && %w(Issue MergeRequest).include?(type)
|
2016-02-17 22:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
params[:type]
|
|
|
|
end
|
|
|
|
|
2016-07-26 17:21:20 -04:00
|
|
|
def sort(items)
|
2018-04-04 05:19:47 -04:00
|
|
|
params[:sort] ? items.sort_by_attribute(params[:sort]) : items.order_id_desc
|
2016-07-26 17:21:20 -04:00
|
|
|
end
|
|
|
|
|
2016-06-22 13:15:09 -04:00
|
|
|
def by_action(items)
|
|
|
|
if action?
|
|
|
|
items = items.where(action: to_action_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
2016-02-17 22:20:31 -05:00
|
|
|
def by_action_id(items)
|
|
|
|
if action_id?
|
|
|
|
items = items.where(action: action_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_author(items)
|
|
|
|
if author?
|
|
|
|
items = items.where(author_id: author.try(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_project(items)
|
|
|
|
if project?
|
2018-07-11 12:33:24 -04:00
|
|
|
items.where(project: project)
|
|
|
|
else
|
|
|
|
projects = Project.public_or_visible_to_user(current_user)
|
2016-02-17 22:20:31 -05:00
|
|
|
|
2018-07-11 12:33:24 -04:00
|
|
|
items.joins(:project).merge(projects)
|
2018-03-01 11:16:39 -05:00
|
|
|
end
|
2016-02-17 22:20:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def by_state(items)
|
2016-06-14 22:09:48 -04:00
|
|
|
case params[:state].to_s
|
2016-02-17 22:20:31 -05:00
|
|
|
when 'done'
|
|
|
|
items.done
|
|
|
|
else
|
|
|
|
items.pending
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_type(items)
|
|
|
|
if type?
|
|
|
|
items = items.where(target_type: type)
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|