Class: Queries::Identifier::Autocomplete

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

Instance Attribute Summary collapse

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, identifier_object_types: []) ⇒ Autocomplete

Returns a new instance of Autocomplete.

Parameters:

  • args (Hash)


8
9
10
11
12
# File 'lib/queries/identifier/autocomplete.rb', line 8

def initialize(string, project_id: nil, identifier_object_types: [])
  @identifier_object_types = identifier_object_types || []
  # @exact = exact == 'true' ? true : (exact == 'false' ? false : nil)
  super
end

Instance Attribute Details

#identifier_object_typesObject

Returns the value of attribute identifier_object_types.



5
6
7
# File 'lib/queries/identifier/autocomplete.rb', line 5

def identifier_object_types
  @identifier_object_types
end

Instance Method Details

#and_clausesArel:Nodes?

Returns:

  • (Arel:Nodes, nil)


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/queries/identifier/autocomplete.rb', line 15

def and_clauses
  clauses = [
    is_identifier_object_type
  ].compact

  return nil if clauses.nil?

  a = clauses.shift
  clauses.each do |b|
    a = a.and(b)
  end
  a
end

#autocompleteObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/queries/identifier/autocomplete.rb', line 29

def autocomplete
  queries = [
    autocomplete_exact_cached,
    autocomplete_exact_identifier,
    autocomplete_matching_cached,
    autocomplete_matching_cached_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 = a.where(and_clauses.to_sql) if and_clauses
    a ||= q
    updated_queries[i] = a
  end

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

#autocomplete_exact_cachedObject



62
63
64
# File 'lib/queries/identifier/autocomplete.rb', line 62

def autocomplete_exact_cached
  base_query.where(with_cached.to_sql).limit(2)
end

#autocomplete_exact_identifierObject



66
67
68
# File 'lib/queries/identifier/autocomplete.rb', line 66

def autocomplete_exact_identifier
  base_query.where(with_identifier_identifier.to_sql).limit(15)
end

#autocomplete_matching_cachedObject



70
71
72
# File 'lib/queries/identifier/autocomplete.rb', line 70

def autocomplete_matching_cached
  base_query.where(match_wildcard_end_in_cached).order(Arel.sql('LENGTH(cached)')).limit(20)
end

#autocomplete_matching_cached_anywhereObject



74
75
76
# File 'lib/queries/identifier/autocomplete.rb', line 74

def autocomplete_matching_cached_anywhere
  base_query.where(with_identifier_cached_wildcarded).order(Arel.sql('LENGTH(cached)')).limit(20)
end

#is_identifier_object_typeArel::Nodes::<>?

Returns:

  • (Arel::Nodes::<>, nil)


57
58
59
60
# File 'lib/queries/identifier/autocomplete.rb', line 57

def is_identifier_object_type
  return nil if identifier_object_types.empty?
  table[:identifier_object_type].in(identifier_object_types)
end