Class: Queries::Query
- Inherits:
-
Object
show all
- Includes:
- Arel::Nodes, Concerns::Identifiers
- Defined in:
- lib/queries/query.rb
Defined Under Namespace
Classes: Autocomplete, Filter
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#query_string ⇒ String
25
26
27
|
# File 'lib/queries/query.rb', line 25
def query_string
@query_string
end
|
#terms ⇒ Array
Returns an expanded search target arary, splitting query_string into a number of wild-carded values.
22
23
24
|
# File 'lib/queries/query.rb', line 22
def terms
@terms
end
|
Class Method Details
.base_name ⇒ Object
Returns String like “otu” or “taxon_name”.
35
36
37
|
# File 'lib/queries/query.rb', line 35
def self.base_name
referenced_klass.name.tableize.singularize end
|
.referenced_klass ⇒ Object
Returns ApplicationRecord subclass e.g. Otu, TaxonName, the class this filter acts on.
29
30
31
|
# File 'lib/queries/query.rb', line 29
def self.referenced_klass
('::' + name.split('::').second).safe_constantize
end
|
Instance Method Details
#alphabetic_strings ⇒ Array
#alphanumeric_strings ⇒ Array
#base_name ⇒ Object
39
40
41
|
# File 'lib/queries/query.rb', line 39
def base_name
self.class.base_name
end
|
#base_query ⇒ Object
47
48
49
|
# File 'lib/queries/query.rb', line 47
def base_query
referenced_klass.select( referenced_klass.name.tableize + '.*' )
end
|
#build_terms ⇒ Array
Ultimately we should replace this concept with full text indexing.
91
92
93
|
# File 'lib/queries/query.rb', line 91
def build_terms
@terms = @query_string.blank? ? [] : [end_wildcard, start_and_end_wildcard]
end
|
#cached_facet ⇒ ActiveRecord::Relation?
TODO: Used in taxon_name, source, identifier
143
144
145
146
147
|
# File 'lib/queries/query.rb', line 143
def cached_facet
return nil if no_terms?
(table[:cached].matches_any(terms)).or(match_ordered_wildcard_pieces_in_cached)
end
|
#end_wildcard ⇒ String
105
106
107
|
# File 'lib/queries/query.rb', line 105
def end_wildcard
query_string + '%'
end
|
#levenshtein_distance(attribute, value) ⇒ Object
124
125
126
127
128
|
# File 'lib/queries/query.rb', line 124
def levenshtein_distance(attribute, value)
value = "'" + value.gsub(/'/, "''") + "'"
a = ApplicationRecord.sanitize_sql(value)
Arel::Nodes::NamedFunction.new("levenshtein", [table[attribute], Arel::Nodes::SqlLiteral.new(a) ] )
end
|
#match_ordered_wildcard_pieces_in_cached ⇒ Arel::Nodes::Matches
136
137
138
|
# File 'lib/queries/query.rb', line 136
def match_ordered_wildcard_pieces_in_cached
table[:cached].matches(wildcard_pieces)
end
|
#no_terms? ⇒ Boolean
95
96
97
|
# File 'lib/queries/query.rb', line 95
def no_terms?
terms.blank?
end
|
#referenced_klass ⇒ Object
51
52
53
|
# File 'lib/queries/query.rb', line 51
def referenced_klass
self.class.referenced_klass
end
|
#referenced_klass_except(query) ⇒ Object
This is an exists equivalent to saying all referenced_klass except those
in the related query
65
66
67
68
69
70
|
# File 'lib/queries/query.rb', line 65
def referenced_klass_except(query)
t = "q_#{table.name}"
s = "with #{t} AS (" + query.to_sql + ')' +
referenced_klass.joins("LEFT JOIN #{t} AS #{t}1 on #{t}1.id = #{table.name}.id").to_sql
referenced_klass.from("(#{s}) as #{table.name}")
end
|
#referenced_klass_union(queries) ⇒ Object
56
57
58
59
|
# File 'lib/queries/query.rb', line 56
def referenced_klass_union(queries)
q = queries.compact
referenced_klass.from("( #{q.collect{|i| '(' + i.to_sql + ')' }.join(' UNION ')}) as #{table.name}")
end
|
#start_and_end_wildcard ⇒ String
110
111
112
|
# File 'lib/queries/query.rb', line 110
def start_and_end_wildcard
'%' + query_string + '%'
end
|
#start_wildcard ⇒ String
100
101
102
|
# File 'lib/queries/query.rb', line 100
def start_wildcard
'%' + query_string
end
|
#table ⇒ Object
43
44
45
|
# File 'lib/queries/query.rb', line 43
def table
referenced_klass.arel_table
end
|
#wildcard_pieces ⇒ String, TODO: nil
TODO: This is bad, it should return nil, not 'NothingToMatch'
152
153
154
155
156
|
# File 'lib/queries/query.rb', line 152
def wildcard_pieces
a = '%' + query_string.gsub(/[^[[:word:]]]+/, '%') + '%' a = 'NothingToMatch' if a.gsub('%','').gsub(' ', '').blank?
a
end
|