debian-mirror-gitlab/app/finders/todos_finder.rb

183 lines
3.1 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-04-02 18:10:28 +05:30
# TodosFinder
#
# Used to filter Todos by set of params
#
# Arguments:
# current_user - which user use
# params:
# action_id: integer
# author_id: integer
# project_id; integer
2016-08-24 12:49:21 +05:30
# state: 'pending' (default) or 'done'
2016-04-02 18:10:28 +05:30
# type: 'Issue' or 'MergeRequest'
#
class TodosFinder
2018-03-27 19:54:05 +05:30
prepend FinderWithCrossProjectAccess
include FinderMethods
2018-11-18 11:00:15 +05:30
include Gitlab::Utils::StrongMemoize
2018-03-27 19:54:05 +05:30
requires_cross_project_access unless: -> { project? }
2017-08-17 22:00:37 +05:30
NONE = '0'.freeze
2016-04-02 18:10:28 +05:30
2018-12-13 13:39:08 +05:30
TODO_TYPES = Set.new(%w(Issue MergeRequest Epic)).freeze
2016-04-02 18:10:28 +05:30
attr_accessor :current_user, :params
2016-09-13 17:45:13 +05:30
def initialize(current_user, params = {})
2016-04-02 18:10:28 +05:30
@current_user = current_user
@params = params
end
def execute
items = current_user.todos
items = by_action_id(items)
2016-08-24 12:49:21 +05:30
items = by_action(items)
2016-04-02 18:10:28 +05:30
items = by_author(items)
items = by_state(items)
items = by_type(items)
2018-11-18 11:00:15 +05:30
items = by_group(items)
2016-09-13 17:45:13 +05:30
# 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-04-02 18:10:28 +05:30
2016-09-13 17:45:13 +05:30
sort(items)
2016-04-02 18:10:28 +05:30
end
2018-12-13 13:39:08 +05:30
# Returns `true` if the current user has any todos for the given target.
#
# target - The value of the `target_type` column, such as `Issue`.
def any_for_target?(target)
current_user.todos.any_for_target?(target)
end
2016-04-02 18:10:28 +05:30
private
def action_id?
2017-09-10 17:25:29 +05:30
action_id.present? && Todo::ACTION_NAMES.key?(action_id.to_i)
2016-04-02 18:10:28 +05:30
end
def action_id
params[:action_id]
end
2016-08-24 12:49:21 +05:30
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-04-02 18:10:28 +05:30
def author?
params[:author_id].present?
end
def author
2018-12-13 13:39:08 +05:30
strong_memoize(:author) do
2016-04-02 18:10:28 +05:30
if author? && params[:author_id] != NONE
User.find(params[:author_id])
end
2018-12-13 13:39:08 +05:30
end
2016-04-02 18:10:28 +05:30
end
def project?
params[:project_id].present?
end
2018-11-18 11:00:15 +05:30
def group?
params[:group_id].present?
end
2016-04-02 18:10:28 +05:30
def project
2018-12-13 13:39:08 +05:30
strong_memoize(:project) do
Project.find_without_deleted(params[:project_id]) if project?
2016-04-02 18:10:28 +05:30
end
end
2018-11-18 11:00:15 +05:30
def group
strong_memoize(:group) do
Group.find(params[:group_id])
2017-09-10 17:25:29 +05:30
end
end
2016-04-02 18:10:28 +05:30
def type?
2018-12-13 13:39:08 +05:30
type.present? && TODO_TYPES.include?(type)
2016-04-02 18:10:28 +05:30
end
def type
params[:type]
end
2016-09-13 17:45:13 +05:30
def sort(items)
2018-12-13 13:39:08 +05:30
if params[:sort]
items.sort_by_attribute(params[:sort])
else
items.order_id_desc
end
2016-09-13 17:45:13 +05:30
end
2016-08-24 12:49:21 +05:30
def by_action(items)
if action?
2018-12-13 13:39:08 +05:30
items.for_action(to_action_id)
else
items
2016-08-24 12:49:21 +05:30
end
end
2016-04-02 18:10:28 +05:30
def by_action_id(items)
if action_id?
2018-12-13 13:39:08 +05:30
items.for_action(action_id)
else
items
2016-04-02 18:10:28 +05:30
end
end
def by_author(items)
if author?
2018-12-13 13:39:08 +05:30
items.for_author(author)
else
items
2016-04-02 18:10:28 +05:30
end
end
def by_project(items)
if project?
2018-12-13 13:39:08 +05:30
items.for_project(project)
else
items
2018-11-18 11:00:15 +05:30
end
end
def by_group(items)
2018-12-13 13:39:08 +05:30
if group?
items.for_group_and_descendants(group)
else
items
end
2016-04-02 18:10:28 +05:30
end
def by_state(items)
2018-12-13 13:39:08 +05:30
if params[:state].to_s == 'done'
2016-04-02 18:10:28 +05:30
items.done
else
items.pending
end
end
def by_type(items)
if type?
2018-12-13 13:39:08 +05:30
items.for_type(type)
else
items
2016-04-02 18:10:28 +05:30
end
end
end