Class: PendingTodosFinder

Inherits:
Object
  • Object
show all
Defined in:
app/finders/pending_todos_finder.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ PendingTodosFinder

users - The list of users to retrieve the todos for. If nil is passed, it won’t filter todos based on users params - A Hash containing columns and values to use for filtering todos.



18
19
20
21
22
23
# File 'app/finders/pending_todos_finder.rb', line 18

def initialize(params = {})
  @params = params

  # To prevent N+1 queries when fetching the users of the PendingTodos.
  @preload_user_association = params.fetch(:preload_user_association, false)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



14
15
16
# File 'app/finders/pending_todos_finder.rb', line 14

def params
  @params
end

#usersObject (readonly)

Returns the value of attribute users.



14
15
16
# File 'app/finders/pending_todos_finder.rb', line 14

def users
  @users
end

Instance Method Details

#by_action(todos) ⇒ Object



92
93
94
95
96
# File 'app/finders/pending_todos_finder.rb', line 92

def by_action(todos)
  return todos if params[:action].blank?

  todos.for_action(params[:action])
end

#by_author_id(todos) ⇒ Object



70
71
72
73
74
# File 'app/finders/pending_todos_finder.rb', line 70

def by_author_id(todos)
  return todos unless params[:author_id]

  todos.for_author(params[:author_id])
end

#by_commit_id(todos) ⇒ Object



76
77
78
79
80
81
82
# File 'app/finders/pending_todos_finder.rb', line 76

def by_commit_id(todos)
  if (id = params[:commit_id])
    todos.for_commit(id)
  else
    todos
  end
end

#by_discussion(todos) ⇒ Object



84
85
86
87
88
89
90
# File 'app/finders/pending_todos_finder.rb', line 84

def by_discussion(todos)
  if (discussion = params[:discussion])
    todos.for_note(discussion.notes)
  else
    todos
  end
end

#by_project(todos) ⇒ Object



46
47
48
49
50
51
52
# File 'app/finders/pending_todos_finder.rb', line 46

def by_project(todos)
  if (id = params[:project_id])
    todos.for_project(id)
  else
    todos
  end
end

#by_target_id(todos) ⇒ Object



54
55
56
57
58
59
60
# File 'app/finders/pending_todos_finder.rb', line 54

def by_target_id(todos)
  if (id = params[:target_id])
    todos.for_target(id)
  else
    todos
  end
end

#by_target_type(todos) ⇒ Object



62
63
64
65
66
67
68
# File 'app/finders/pending_todos_finder.rb', line 62

def by_target_type(todos)
  if (type = params[:target_type])
    todos.for_type(type)
  else
    todos
  end
end

#by_users(todos) ⇒ Object



40
41
42
43
44
# File 'app/finders/pending_todos_finder.rb', line 40

def by_users(todos)
  return todos unless params[:users].present?

  todos.for_user(params[:users])
end

#executeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/finders/pending_todos_finder.rb', line 25

def execute
  todos = Todo.pending
  todos = by_users(todos)
  todos = by_project(todos)
  todos = by_target_id(todos)
  todos = by_target_type(todos)
  todos = by_author_id(todos)
  todos = by_discussion(todos)
  todos = by_commit_id(todos)

  todos = todos.with_preloaded_user if @preload_user_association

  by_action(todos)
end