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.



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

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

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



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

def results
  @results
end

Instance Method Details

#complete_todos(terms) ⇒ Object (private)



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

def complete_todos(terms)
  @user.todos
    .where("(todos.description " + Common.like_operator + " ? OR todos.notes " + Common.like_operator + " ?) AND NOT (todos.completed_at IS NULL)", terms, terms)
    .includes(Todo::DEFAULT_INCLUDES)
    .reorder("todos.completed_at DESC")
end

#incomplete_todos(terms) ⇒ Object (private)



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

def incomplete_todos(terms)
  @user.todos
    .where("(todos.description " + Common.like_operator + " ? OR todos.notes " + Common.like_operator + " ?) AND todos.completed_at IS NULL", terms, terms)
    .includes(Todo::DEFAULT_INCLUDES)
    .reorder(Arel.sql("todos.due IS NULL, todos.due ASC, todos.created_at ASC"))
end

#number_of_findsObject



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

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

#searchObject



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

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)



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

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 " + Common.like_operator + " ? ", @user.id, terms])
end