diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index f4cbb776..42e43e67 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -5,19 +5,12 @@ class SearchController < ApplicationController def results @source_view = params['_source_view'] || 'search' @page_title = "TRACKS::Search Results for #{params[:search]}" - terms = "%#{params[:search]}%" - @found_not_complete_todos = incomplete_todos(terms) - @found_complete_todos = complete_todos(terms) - @found_todos = @found_not_complete_todos + @found_complete_todos + searcher = Search::SearchResults.new(current_user, params[:search]) + searcher.search - @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) - - # TODO: limit search to tags on todos - @found_tags = todo_tags_by_name(current_user, terms) - @count = @found_todos.size + @found_projects.size + @found_notes.size + @found_contexts.size + @found_tags.size + @results = searcher.results + @count = searcher.number_of_finds init_not_done_counts init_project_hidden_todo_counts @@ -26,30 +19,4 @@ class SearchController < ApplicationController def index @page_title = "TRACKS::Search" end - -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") - end - - 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 end diff --git a/app/models/search/search_results.rb b/app/models/search/search_results.rb new file mode 100644 index 00000000..692d0fd0 --- /dev/null +++ b/app/models/search/search_results.rb @@ -0,0 +1,54 @@ +module Search + + class SearchResults + attr_reader :results + + def initialize(user, terms) + @user = user + @terms = "%#{terms}%" + @results = {} + end + + def search + results[:not_complete_todos] = incomplete_todos(@terms) + results[:complete_todos] = complete_todos(@terms) + results[:todos] = results[:not_complete_todos] + results[:complete_todos] + results[:projects] = @user.projects.with_name_or_description(@terms) + results[:notes] = @user.notes.with_body(@terms) + results[:contexts] = @user.contexts.with_name(@terms) + results[:tags] = todo_tags_by_name(@terms) + end + + def number_of_finds + results[:todos].size + results[:projects].size + results[:notes].size + results[:contexts].size + results[:tags].size + end + + private + + def incomplete_todos(terms) + @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") + end + + def complete_todos(terms) + @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(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 ? ", @user.id, terms]) + end + + end + +end diff --git a/app/views/search/results.html.erb b/app/views/search/results.html.erb index c73d9761..e97431ca 100644 --- a/app/views/search/results.html.erb +++ b/app/views/search/results.html.erb @@ -2,24 +2,24 @@
<% else -%> - <%= render :layout => 'show_results_collection', :object => @found_todos, :locals => { :collection_name => "found-todos", :collection_title => t('search.todos_matching_query')} do %> - <%= render :partial => "todos/todo", :collection => @found_todos, :locals => { :parent_container_type => 'search', :suppress_context => false, :suppress_project => false, :suppress_edit_button => false } %> + <%= render :layout => 'show_results_collection', :object => @results[:todos], :locals => { :collection_name => "found-todos", :collection_title => t('search.todos_matching_query')} do %> + <%= render :partial => "todos/todo", :collection => @results[:todos], :locals => { :parent_container_type => 'search', :suppress_context => false, :suppress_project => false, :suppress_edit_button => false } %> <% end -%> - <%= render :layout => 'show_results_collection', :object => @found_projects, :locals => { :collection_name => "found-project", :collection_title => t('search.projects_matching_query')} do %> - <%= render :partial => "projects/project_listing", :collection => @found_projects, :locals => { :suppress_drag_handle => true, :suppress_edit_button => true, :suppress_delete_button => true } %> + <%= render :layout => 'show_results_collection', :object => @results[:projects], :locals => { :collection_name => "found-project", :collection_title => t('search.projects_matching_query')} do %> + <%= render :partial => "projects/project_listing", :collection => @results[:projects], :locals => { :suppress_drag_handle => true, :suppress_edit_button => true, :suppress_delete_button => true } %> <% end -%> - <%= render :layout => 'show_results_collection', :object => @found_notes, :locals => { :collection_name => "found-notes", :collection_title => t('search.notes_matching_query')} do %> - <%= render :partial => "notes/notes_summary", :collection=> @found_notes %> + <%= render :layout => 'show_results_collection', :object => @results[:notes], :locals => { :collection_name => "found-notes", :collection_title => t('search.notes_matching_query')} do %> + <%= render :partial => "notes/notes_summary", :collection=> @results[:notes] %> <% end -%> - <%= render :layout => 'show_results_collection', :object => @found_contexts, :locals => { :collection_name => "found-contexts", :collection_title => t('search.contexts_matching_query')} do %> - <%= render :partial => "contexts/context_listing", :collection => @found_contexts, :locals => { :suppress_drag_handle => true, :suppress_edit_button => true } %> + <%= render :layout => 'show_results_collection', :object => @results[:contexts], :locals => { :collection_name => "found-contexts", :collection_title => t('search.contexts_matching_query')} do %> + <%= render :partial => "contexts/context_listing", :collection => @results[:contexts], :locals => { :suppress_drag_handle => true, :suppress_edit_button => true } %> <% end -%> - <%= render :layout => 'show_results_collection', :object => @found_tags, :locals => { :collection_name => "found-tags", :collection_title => t('search.tags_matching_query')} do %> -