2008-12-03 20:25:05 +01:00
|
|
|
class SearchController < ApplicationController
|
|
|
|
|
2012-01-31 12:11:14 +01:00
|
|
|
helper :todos, :application, :notes, :projects, :contexts
|
|
|
|
|
2008-12-03 20:25:05 +01:00
|
|
|
def results
|
|
|
|
@source_view = params['_source_view'] || 'search'
|
|
|
|
@page_title = "TRACKS::Search Results for #{params[:search]}"
|
2012-05-03 23:23:31 +02:00
|
|
|
terms = "%#{params[:search]}%"
|
2011-08-28 10:38:49 +02:00
|
|
|
|
2013-03-12 19:49:19 -05:00
|
|
|
@found_not_complete_todos = incomplete_todos(terms)
|
|
|
|
@found_complete_todos = complete_todos(terms)
|
2011-08-28 10:38:49 +02:00
|
|
|
@found_todos = @found_not_complete_todos + @found_complete_todos
|
|
|
|
|
2013-03-12 19:49:19 -05:00
|
|
|
@found_projects = current_user.projects.with_name_or_description(terms);
|
|
|
|
@found_notes = current_user.notes.with_body(terms);
|
|
|
|
@found_contexts = current_user.contexts.with_name(terms)
|
2012-04-18 14:22:58 +02:00
|
|
|
|
2008-12-03 20:25:05 +01:00
|
|
|
# TODO: limit search to tags on todos
|
2013-03-12 19:49:19 -05:00
|
|
|
@found_tags = todo_tags_by_name(current_user, terms)
|
2008-12-03 20:25:05 +01:00
|
|
|
@count = @found_todos.size + @found_projects.size + @found_notes.size + @found_contexts.size + @found_tags.size
|
2011-08-28 10:38:49 +02:00
|
|
|
|
2008-12-03 20:25:05 +01:00
|
|
|
init_not_done_counts
|
|
|
|
init_project_hidden_todo_counts
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
2011-08-28 10:38:49 +02:00
|
|
|
@page_title = "TRACKS::Search"
|
2008-12-03 20:25:05 +01:00
|
|
|
end
|
2011-08-28 10:38:49 +02:00
|
|
|
|
2013-03-12 19:49:19 -05:00
|
|
|
private
|
|
|
|
def incomplete_todos(terms)
|
|
|
|
current_user.todos.
|
|
|
|
where("(todos.description LIKE ? OR todos.notes LIKE ?) AND todos.completed_at IS NULL", terms, terms).
|
|
|
|
includes(Todo::DEFAULT_INCLUDES).
|
|
|
|
reorder("todos.due IS NULL, todos.due ASC, todos.created_at ASC")
|
2008-12-03 20:25:05 +01:00
|
|
|
end
|
|
|
|
|
2013-03-12 19:49:19 -05:00
|
|
|
def complete_todos(terms)
|
|
|
|
current_user.todos.
|
|
|
|
where("(todos.description LIKE ? OR todos.notes LIKE ?) AND NOT (todos.completed_at IS NULL)", terms, terms).
|
|
|
|
includes(Todo::DEFAULT_INCLUDES).
|
|
|
|
reorder("todos.completed_at DESC")
|
|
|
|
end
|
|
|
|
|
|
|
|
def todo_tags_by_name(current_user, terms)
|
|
|
|
Tagging.find_by_sql([
|
|
|
|
"SELECT DISTINCT tags.name as name "+
|
|
|
|
"FROM tags "+
|
|
|
|
"LEFT JOIN taggings ON tags.id = taggings.tag_id "+
|
|
|
|
"LEFT JOIN todos ON taggings.taggable_id = todos.id "+
|
|
|
|
"WHERE todos.user_id=? "+
|
|
|
|
"AND tags.name LIKE ? ", current_user.id, terms])
|
|
|
|
|
|
|
|
end
|
2008-12-03 20:25:05 +01:00
|
|
|
end
|