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

98 lines
2.3 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
# Finder for retrieving the pending todos of a user, optionally filtered using
# various fields.
#
# While this finder is a bit more verbose compared to use
# `where(params.slice(...))`, it allows us to decouple the input parameters from
# the actual column names. For example, if we ever decide to use separate
# columns for target types (e.g. `issue_id`, `merge_request_id`, etc), we no
# longer need to change _everything_ that uses this finder. Instead, we just
# change the various `by_*` methods in this finder, without having to touch
# everything that uses it.
class PendingTodosFinder
2021-04-29 21:17:54 +05:30
attr_reader :users, :params
2018-12-13 13:39:08 +05:30
2023-07-09 08:55:56 +05:30
# users - The list of users to retrieve the todos for. If nil is passed, it won't filter todos based on users
2018-12-13 13:39:08 +05:30
# params - A Hash containing columns and values to use for filtering todos.
2023-07-09 08:55:56 +05:30
def initialize(params = {})
2018-12-13 13:39:08 +05:30
@params = params
2023-07-09 08:55:56 +05:30
# To prevent N+1 queries when fetching the users of the PendingTodos.
@preload_user_association = params.fetch(:preload_user_association, false)
2018-12-13 13:39:08 +05:30
end
def execute
2023-07-09 08:55:56 +05:30
todos = Todo.pending
todos = by_users(todos)
2018-12-13 13:39:08 +05:30
todos = by_project(todos)
todos = by_target_id(todos)
todos = by_target_type(todos)
2023-07-09 08:55:56 +05:30
todos = by_author_id(todos)
2021-09-04 01:27:46 +05:30
todos = by_discussion(todos)
2022-05-07 20:08:51 +05:30
todos = by_commit_id(todos)
2023-07-09 08:55:56 +05:30
todos = todos.with_preloaded_user if @preload_user_association
2022-05-07 20:08:51 +05:30
by_action(todos)
2018-12-13 13:39:08 +05:30
end
2023-07-09 08:55:56 +05:30
def by_users(todos)
return todos unless params[:users].present?
todos.for_user(params[:users])
end
2018-12-13 13:39:08 +05:30
def by_project(todos)
if (id = params[:project_id])
todos.for_project(id)
else
todos
end
end
def by_target_id(todos)
if (id = params[:target_id])
todos.for_target(id)
else
todos
end
end
def by_target_type(todos)
if (type = params[:target_type])
todos.for_type(type)
else
todos
end
end
2023-07-09 08:55:56 +05:30
def by_author_id(todos)
return todos unless params[:author_id]
todos.for_author(params[:author_id])
end
2018-12-13 13:39:08 +05:30
def by_commit_id(todos)
if (id = params[:commit_id])
todos.for_commit(id)
else
todos
end
end
2021-09-04 01:27:46 +05:30
def by_discussion(todos)
if (discussion = params[:discussion])
todos.for_note(discussion.notes)
else
todos
end
end
2022-05-07 20:08:51 +05:30
def by_action(todos)
return todos if params[:action].blank?
todos.for_action(params[:action])
end
2018-12-13 13:39:08 +05:30
end