Class: Todos::UndoneTodosQuery

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
app/models/todos/undone_todos_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_user) ⇒ UndoneTodosQuery

Returns a new instance of UndoneTodosQuery.



6
7
8
# File 'app/models/todos/undone_todos_query.rb', line 6

def initialize(current_user)
  @current_user = current_user
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



5
6
7
# File 'app/models/todos/undone_todos_query.rb', line 5

def current_user
  @current_user
end

Instance Method Details

#query(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/todos/undone_todos_query.rb', line 10

def query(params)
  if params[:done]
    not_done_todos = current_user.todos.completed.completed_after(Time.zone.now - params[:done].to_i.days)
  else
    not_done_todos = current_user.todos.active.not_hidden
  end

  not_done_todos = not_done_todos
    .reorder(Arel.sql("todos.due IS NULL, todos.due ASC, todos.created_at ASC"))
    .includes(Todo::DEFAULT_INCLUDES)

  not_done_todos = not_done_todos.limit(sanitize(params[:limit])) if params[:limit]

  if params[:due]
    due_within_when = Time.zone.now + params[:due].to_i.days
    not_done_todos = not_done_todos.where('todos.due <= ?', due_within_when)
  end

  if params[:tag]
    tag = Tag.where(:name => params[:tag]).first
    return [] if !tag
    not_done_todos = not_done_todos.joins(:taggings).where('taggings.tag_id = ?', tag.id)
  end

  if params[:context_id]
    context = current_user.contexts.find(params[:context_id])
    not_done_todos = not_done_todos.where('context_id' => context.id)
  end

  if params[:project_id]
    project = current_user.projects.find(params[:project_id])
    not_done_todos = not_done_todos.where('project_id' => project)
  end

  return not_done_todos
end