2019-12-18 09:49:57 -06:00
|
|
|
# typed: true
|
2019-04-07 19:59:01 -05:00
|
|
|
module Todos
|
|
|
|
class UndoneTodosQuery
|
2019-04-07 20:01:22 -05:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
|
|
|
|
2019-04-07 19:59:01 -05:00
|
|
|
attr_reader :current_user
|
|
|
|
def initialize(current_user)
|
|
|
|
@current_user = current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
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]
|
2019-04-11 11:28:43 -05:00
|
|
|
due_within_when = Time.zone.now + params[:due].to_i.days
|
2019-04-07 19:59:01 -05:00
|
|
|
not_done_todos = not_done_todos.where('todos.due <= ?', due_within_when)
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:tag]
|
2019-04-11 11:28:43 -05:00
|
|
|
tag = Tag.where(:name => params[:tag]).first
|
2019-05-21 22:33:21 +03:00
|
|
|
return [] if !tag
|
2019-04-11 09:53:53 -05:00
|
|
|
not_done_todos = not_done_todos.joins(:taggings).where('taggings.tag_id = ?', tag.id)
|
2019-04-07 19:59:01 -05:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|