Class: Queries::Note::Autocomplete

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

Instance Attribute Summary

Attributes inherited from Query::Autocomplete

#dynamic_limit, #project_id, #query_string

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Autocomplete

#autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_common_name_exact, #autocomplete_common_name_like, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #exactly_named, #fragments, #integers, #least_levenshtein, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #scope, #string_fragments, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

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)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/queries/note/autocomplete.rb', line 49

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.present? 
    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



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

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)


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

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)


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

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

#autocomplete_wildcard_anywhereObject



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

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)


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

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)


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

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