todos_finder.rb 2.82 KB
Newer Older
1
# TodosFinder
2
#
3
# Used to filter Todos by set of params
4
5
6
7
8
9
10
#
# Arguments:
#   current_user - which user use
#   params:
#     action_id: integer
#     author_id: integer
#     project_id; integer
11
#     state: 'pending' (default) or 'done'
12
13
14
#     type: 'Issue' or 'MergeRequest'
#

15
class TodosFinder
Douwe Maan's avatar
Douwe Maan committed
16
  NONE = '0'.freeze
17
18
19

  attr_accessor :current_user, :params

20
  def initialize(current_user, params = {})
21
22
23
24
25
    @current_user = current_user
    @params = params
  end

  def execute
26
    items = current_user.todos
27
    items = by_action_id(items)
28
    items = by_action(items)
29
30
31
    items = by_author(items)
    items = by_state(items)
    items = by_type(items)
32
33
34
    # 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)
35

Felipe Artur's avatar
Felipe Artur committed
36
    sort(items)
37
38
39
40
41
  end

  private

  def action_id?
Sean McGivern's avatar
Sean McGivern committed
42
    action_id.present? && Todo::ACTION_NAMES.has_key?(action_id.to_i)
43
44
45
46
47
48
  end

  def action_id
    params[:action_id]
  end

49
50
51
52
53
54
55
56
57
58
59
60
  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

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  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])

http://jneen.net/'s avatar
http://jneen.net/ committed
86
      unless Ability.allowed?(current_user, :read_project, @project)
87
88
89
90
91
92
93
94
95
        @project = nil
      end
    else
      @project = nil
    end

    @project
  end

96
97
  def projects(items)
    item_project_ids = items.reorder(nil).select(:project_id)
98
    ProjectsFinder.new.execute(current_user, item_project_ids)
99
100
  end

101
102
103
104
105
106
107
108
  def type?
    type.present? && ['Issue', 'MergeRequest'].include?(type)
  end

  def type
    params[:type]
  end

Felipe Artur's avatar
Felipe Artur committed
109
110
111
112
  def sort(items)
    params[:sort] ? items.sort(params[:sort]) : items.reorder(id: :desc)
  end

113
114
115
116
117
118
119
120
  def by_action(items)
    if action?
      items = items.where(action: to_action_id)
    end

    items
  end

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  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?
      items = items.where(project: project)
140
141
142
    else
      item_projects = projects(items)
      items = items.merge(item_projects).joins(:project)
143
144
145
146
147
148
    end

    items
  end

  def by_state(items)
149
    case params[:state].to_s
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    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