Class: Queries::Note::Autocomplete

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/note/autocomplete.rb

Instance Attribute Summary

Attributes inherited from Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary collapse

Methods inherited from Query

#alphabetic_strings, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_exact_id, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #simple_date, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

#initialize(string, project_id: nil) ⇒ Autocomplete

Returns a new instance of Autocomplete

Parameters:

  • args (Hash)


6
7
8
# File 'lib/queries/note/autocomplete.rb', line 6

def initialize(string, project_id: nil)
  super
end

Instance Method Details

#autocompleteArray

Returns:

  • (Array)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/queries/note/autocomplete.rb', line 54

def autocomplete
  queries = [
    autocomplete_exact_note,
    autocomplete_wildcard_end,
    autocomplete_wildcard_wrapped,
    autocomplete_ordered_wildcard_pieces,
    autocomplete_wildcard_anywhere,
    autocomplete_any_wildcard_anywhere
  ]

  queries.compact!

  updated_queries = []
  queries.each_with_index do |q ,i|
    a = q.where(with_project_id.to_sql) if project_id 
    a ||= q
    updated_queries[i] = a
  end

  result = []
  updated_queries.each do |q|
    result += q.to_a
    result.uniq!
    break if result.count > 40 
  end
  result[0..40]
end

#autocomplete_any_wildcard_anywhereObject



46
47
48
49
50
51
# File 'lib/queries/note/autocomplete.rb', line 46

def autocomplete_any_wildcard_anywhere
  b = fragments
  return nil if b.empty?
  a = table[:text].matches_any(b)
  base_query.where(a.to_sql).limit(20)
end

#autocomplete_exact_noteActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


16
17
18
19
# File 'lib/queries/note/autocomplete.rb', line 16

def autocomplete_exact_note
  a = table[:text].eq(query_string)
  base_query.where(a.to_sql).limit(20)
end

#autocomplete_ordered_wildcard_piecesArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


34
35
36
37
# File 'lib/queries/note/autocomplete.rb', line 34

def autocomplete_ordered_wildcard_pieces
  a = table[:text].matches(wildcard_pieces)
  base_query.where(a.to_sql).limit(10)
end

#autocomplete_wildcard_anywhereObject



39
40
41
42
43
44
# File 'lib/queries/note/autocomplete.rb', line 39

def autocomplete_wildcard_anywhere
  b = fragments
  return nil if b.empty?
  a = table[:text].matches_all(b)
  base_query.where(a.to_sql).limit(20)
end

#autocomplete_wildcard_endActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


22
23
24
25
# File 'lib/queries/note/autocomplete.rb', line 22

def autocomplete_wildcard_end
  a = table[:text].matches(query_string + '%') 
  base_query.where(a.to_sql).limit(8)
end

#autocomplete_wildcard_wrappedActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


28
29
30
31
# File 'lib/queries/note/autocomplete.rb', line 28

def autocomplete_wildcard_wrapped
  a = table[:text].matches('%' + query_string + '%')
  base_query.where(a.to_sql).limit(5)
end

#base_queryScope

Returns:

  • (Scope)


11
12
13
# File 'lib/queries/note/autocomplete.rb', line 11

def base_query
  ::Note.select('notes.*')
end

#tableArel::Table

Returns:

  • (Arel::Table)


83
84
85
# File 'lib/queries/note/autocomplete.rb', line 83

def table
  ::Note.arel_table
end