Class: TodosFinder

Inherits:
Object
  • Object
show all
Includes:
FinderMethods, Gitlab::Utils::StrongMemoize
Defined in:
app/finders/todos_finder.rb

Overview

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
  target_id; integer
  state: 'pending' (default) or 'done'
  type: 'Issue' or 'MergeRequest' or ['Issue', 'MergeRequest']

Constant Summary collapse

NONE =
'0'
TODO_TYPES =
Set.new(%w(Issue MergeRequest DesignManagement::Design AlertManagement::Alert)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from FinderMethods

#find, #find_by, #find_by!

Constructor Details

#initialize(current_user, params = {}) ⇒ TodosFinder

Returns a new instance of TodosFinder.


37
38
39
40
# File 'app/finders/todos_finder.rb', line 37

def initialize(current_user, params = {})
  @current_user = current_user
  @params = params
end

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user


29
30
31
# File 'app/finders/todos_finder.rb', line 29

def current_user
  @current_user
end

#paramsObject

Returns the value of attribute params


29
30
31
# File 'app/finders/todos_finder.rb', line 29

def params
  @params
end

Class Method Details

.todo_typesObject


32
33
34
# File 'app/finders/todos_finder.rb', line 32

def todo_types
  TODO_TYPES
end

Instance Method Details

#any_for_target?(target, state = nil) ⇒ Boolean

Returns `true` if the current user has any todos for the given target with the optional given state.

target - The value of the `target_type` column, such as `Issue`. state - The value of the `state` column, such as `pending` or `done`.

Returns:

  • (Boolean)

65
66
67
# File 'app/finders/todos_finder.rb', line 65

def any_for_target?(target, state = nil)
  current_user.todos.any_for_target?(target, state)
end

#executeObject

Raises:

  • (ArgumentError)

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/finders/todos_finder.rb', line 42

def execute
  return Todo.none if current_user.nil?
  raise ArgumentError, invalid_type_message unless valid_types?

  items = current_user.todos
  items = by_action_id(items)
  items = by_action(items)
  items = by_author(items)
  items = by_state(items)
  items = by_target_id(items)
  items = by_types(items)
  items = by_group(items)
  # 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)

  sort(items)
end