Class: Queries::Identifier::Autocomplete

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/identifier/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

- (Autocomplete) initialize(string, project_id: nil, identifier_object_types: [])

Returns a new instance of Autocomplete

Parameters:

  • args (Hash)


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

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

- (Object) identifier_object_types

Returns the value of attribute identifier_object_types



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

def identifier_object_types
  @identifier_object_types
end

Instance Method Details

- (Arel:Nodes?) and_clauses

Returns:

  • (Arel:Nodes, nil)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/queries/identifier/autocomplete.rb', line 22

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

- (Object) autocomplete



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/queries/identifier/autocomplete.rb', line 36

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

- (Object) autocomplete_exact_cached



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

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

- (Object) autocomplete_exact_identifier



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

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

- (Object) autocomplete_matching_cached



77
78
79
# File 'lib/queries/identifier/autocomplete.rb', line 77

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

- (Object) autocomplete_matching_cached_anywhere



81
82
83
# File 'lib/queries/identifier/autocomplete.rb', line 81

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

- (Object) base_query



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

def base_query
  ::Identifier.select('identifiers.*')
end

- (Arel::Nodes::<>?) is_identifier_object_type

Returns:

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


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

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

- (Object) table



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

def table
  ::Identifier.arel_table
end