Class: Search::SearchResults

Inherits:
Object
  • Object
show all
Defined in:
app/models/search/search_results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, terms) ⇒ SearchResults

Returns a new instance of SearchResults



6
7
8
9
10
# File 'app/models/search/search_results.rb', line 6

def initialize(user, terms)
  @user = user
  @terms = "%#{terms}%"
  @results = {}
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results



4
5
6
# File 'app/models/search/search_results.rb', line 4

def results
  @results
end

Instance Method Details

#complete_todos(terms) ⇒ Object (private)



35
36
37
38
39
40
# File 'app/models/search/search_results.rb', line 35

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

#incomplete_todos(terms) ⇒ Object (private)



28
29
30
31
32
33
# File 'app/models/search/search_results.rb', line 28

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

#number_of_findsObject



22
23
24
# File 'app/models/search/search_results.rb', line 22

def number_of_finds
  results[:todos].size  + results[:projects].size + results[:notes].size + results[:contexts].size + results[:tags].size
end

#searchObject



12
13
14
15
16
17
18
19
20
# File 'app/models/search/search_results.rb', line 12

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

#todo_tags_by_name(terms) ⇒ Object (private)



42
43
44
45
46
47
48
49
50
# File 'app/models/search/search_results.rb', line 42

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