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.
This commit is contained in:
Matt Rogers 2013-03-12 19:49:19 -05:00
parent 63fc460c00
commit f541a4c9d6
4 changed files with 34 additions and 26 deletions

View file

@ -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

View file

@ -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
end

View file

@ -4,4 +4,5 @@ class Note < ActiveRecord::Base
attr_protected :user
scope :with_body, lambda { |terms| where("body LIKE ?", terms) }
end

View file

@ -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"