Class: Queries::Identifier::Autocomplete
- Inherits:
-
Query
- Object
- Query
- Queries::Identifier::Autocomplete
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, #attribute_exact_facet, #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, #build_terms, #cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #end_wildcard, #exactly_named, #fragments, #integers, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_terms?, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years
Constructor Details
#initialize(string, project_id: nil, identifier_object_types: []) ⇒ Autocomplete
Returns a new instance of Autocomplete.
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 || []
super
end
|
Instance Attribute Details
#identifier_object_types ⇒ Object
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
#and_clauses ⇒ Arel:Nodes?
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
|
#autocomplete ⇒ Object
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
|
#autocomplete_exact_cached ⇒ Object
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
|
#autocomplete_exact_identifier ⇒ Object
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
|
#autocomplete_matching_cached ⇒ Object
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
|
#autocomplete_matching_cached_anywhere ⇒ Object
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
|
#base_query ⇒ Object
13
14
15
|
# File 'lib/queries/identifier/autocomplete.rb', line 13
def base_query
::Identifier.select('identifiers.*')
end
|
#is_identifier_object_type ⇒ Arel::Nodes::<>?
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
|
#table ⇒ Object
17
18
19
|
# File 'lib/queries/identifier/autocomplete.rb', line 17
def table
::Identifier.arel_table
end
|