Class: Queries::DataAttribute::Autocomplete

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

Instance Attribute Summary collapse

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)


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)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/queries/data_attribute/autocomplete.rb', line 71

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 
    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



30
31
32
33
34
35
36
# File 'lib/queries/data_attribute/autocomplete.rb', line 30

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



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

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



62
63
64
65
66
67
68
# File 'lib/queries/data_attribute/autocomplete.rb', line 62

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



22
23
24
25
26
27
28
# File 'lib/queries/data_attribute/autocomplete.rb', line 22

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



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

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



54
55
56
57
58
59
60
# File 'lib/queries/data_attribute/autocomplete.rb', line 54

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

#base_queryScope

Returns:

  • (Scope)


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

def base_query
  ::DataAttribute.select('data_attributes.*')
end

#predicate_tableArel::Table

Returns:

  • (Arel::Table)


105
106
107
# File 'lib/queries/data_attribute/autocomplete.rb', line 105

def predicate_table
  ::Predicate.arel_table
end

#set_key_valueObject



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

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

#tableArel::Table

Returns:

  • (Arel::Table)


100
101
102
# File 'lib/queries/data_attribute/autocomplete.rb', line 100

def table
  ::DataAttribute.arel_table
end