Class: Queries::DataAttribute::Autocomplete

Inherits:
Query::Autocomplete show all
Defined in:
lib/queries/data_attribute/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) ⇒ Autocomplete

Returns a new instance of Autocomplete.

Parameters:

  • args (Hash)


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

def initialize(string, project_id: nil)
  super
  set_key_value
end

Instance Attribute Details

#term_keyObject

Returns the value of attribute term_key.



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

def term_key
  @term_key
end

#term_valueObject

Returns the value of attribute term_value.



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

def term_value
  @term_value
end

Instance Method Details

#autocompleteArray

Returns:

  • (Array)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/queries/data_attribute/autocomplete.rb', line 66

def autocomplete
  queries = [
    autocomplete_internal_exact_key_value,
    autocomplete_import_exact_key_value,
    autocomplete_internal_exact_key_wildcard_value,
    autocomplete_import_exact_key_wildcard_value,
    autocomplete_internal_wildcard_key_value,
    autocomplete_import_wildcard_key_value
  ]

  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_import_exact_key_valueObject



25
26
27
28
29
30
31
# File 'lib/queries/data_attribute/autocomplete.rb', line 25

def autocomplete_import_exact_key_value
  return nil if term_value.nil? || term_key.nil?
  ::ImportAttribute.where(
    import_predicate: term_key,
    value: term_value
  ).limit(20)
end

#autocomplete_import_exact_key_wildcard_valueObject



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

def autocomplete_import_exact_key_wildcard_value
  return nil if term_value.nil? || term_key.nil?
  ::ImportAttribute.where(
    import_predicate: term_key,
    value: '%' + term_value + '%'
  ).limit(20)
end

#autocomplete_import_wildcard_key_valueObject



57
58
59
60
61
62
63
# File 'lib/queries/data_attribute/autocomplete.rb', line 57

def autocomplete_import_wildcard_key_value
  return nil if term_value.nil? || term_key.nil?
  ::ImportAttribute.where(
    import_predicate: '%' + term_key + '%',
    value: '%' + term_value + '%'
  ).limit(20)
end

#autocomplete_internal_exact_key_valueObject



17
18
19
20
21
22
23
# File 'lib/queries/data_attribute/autocomplete.rb', line 17

def autocomplete_internal_exact_key_value
  return nil if term_value.nil? || term_key.nil?
  ::InternalAttribute.joins(:predicate).where(
    predicate_table[:name].eq(term_key).and(
      table[:value].eq(term_value))
  ).limit(20)
end

#autocomplete_internal_exact_key_wildcard_valueObject



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

def autocomplete_internal_exact_key_wildcard_value
  return nil if term_value.nil? || term_key.nil?
  ::InternalAttribute.joins(:predicate).where(
    predicate_table[:name].eq(term_key).and(
      table[:value].matches('%' + term_value + '%'))
  ).limit(20)
end

#autocomplete_internal_wildcard_key_valueObject



49
50
51
52
53
54
55
# File 'lib/queries/data_attribute/autocomplete.rb', line 49

def autocomplete_internal_wildcard_key_value
  return nil if term_value.nil? || term_key.nil?
  ::InternalAttribute.joins(:predicate).where(
    predicate_table[:name].matches('%' + term_key + '%').and(
      table[:value].matches('%' + term_value + '%'))
  ).limit(20)
end

#predicate_tableArel::Table

Returns:

  • (Arel::Table)


95
96
97
# File 'lib/queries/data_attribute/autocomplete.rb', line 95

def predicate_table
  ::Predicate.arel_table
end

#set_key_valueObject



13
14
15
# File 'lib/queries/data_attribute/autocomplete.rb', line 13

def set_key_value
  @term_key, @term_value = query_string.split(' ')
end