From f541a4c9d61d266d262b9daf307207ce150667e2 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Tue, 12 Mar 2013 19:49:19 -0500 Subject: [PATCH] Simplify SearchController#results Give names to the various things being searched for and move them out into their own methods. Also add scopes to give names to things being queried out of the models. --- app/controllers/search_controller.rb | 54 +++++++++++++++------------- app/models/context.rb | 3 +- app/models/note.rb | 1 + app/models/project.rb | 2 ++ 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 817c9f1e..f4cbb776 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -7,33 +7,16 @@ class SearchController < ApplicationController @page_title = "TRACKS::Search Results for #{params[:search]}" terms = "%#{params[:search]}%" - @found_not_complete_todos = 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"). - all - - @found_complete_todos = 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"). - all - + @found_not_complete_todos = incomplete_todos(terms) + @found_complete_todos = complete_todos(terms) @found_todos = @found_not_complete_todos + @found_complete_todos - @found_projects = current_user.projects.where("name LIKE ? OR description LIKE ?", terms, terms).all - @found_notes = current_user.notes.where("body LIKE ?", terms).all - @found_contexts = current_user.contexts.where("name LIKE ?", terms).all + @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 = 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]) - + @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 init_not_done_counts @@ -44,8 +27,29 @@ class SearchController < ApplicationController @page_title = "TRACKS::Search" end - def init - @source_view = params['_source_view'] || 'search' +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/context.rb b/app/models/context.rb index c55cd74c..bb02b45b 100644 --- a/app/models/context.rb +++ b/app/models/context.rb @@ -8,6 +8,7 @@ class Context < ActiveRecord::Base scope :active, :conditions => { :state => :active } scope :hidden, :conditions => { :state => :hidden } scope :closed, :conditions => { :state => :closed } + scope :with_name, lambda { |name| where("name LIKE ?", name) } acts_as_list :scope => :user, :top_of_list => 0 @@ -70,4 +71,4 @@ class NullContext '' end -end \ No newline at end of file +end diff --git a/app/models/note.rb b/app/models/note.rb index 059773fa..575f284d 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -4,4 +4,5 @@ class Note < ActiveRecord::Base attr_protected :user + scope :with_body, lambda { |terms| where("body LIKE ?", terms) } end diff --git a/app/models/project.rb b/app/models/project.rb index 328a77d2..85005f9e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -11,6 +11,8 @@ class Project < ActiveRecord::Base scope :completed, :conditions => { :state => 'completed'} scope :uncompleted, :conditions => ["NOT(state = ?)", 'completed'] + scope :with_name_or_description, lambda { |body| where("name LIKE ? OR description LIKE ?", body, body) } + validates_presence_of :name validates_length_of :name, :maximum => 255 validates_uniqueness_of :name, :scope => "user_id"