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.
108
109
110
|
# File 'lib/queries/query.rb', line 108
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
160
161
162
163
164
|
# File 'lib/queries/query.rb', line 160
def cached_facet
return nil if no_terms?
(table[:cached].matches_any(terms)).or(match_ordered_wildcard_pieces_in_cached)
end
|
#end_wildcard ⇒ String
122
123
124
|
# File 'lib/queries/query.rb', line 122
def end_wildcard
query_string + '%'
end
|
#levenshtein_distance(attribute, value) ⇒ Object
141
142
143
144
145
|
# File 'lib/queries/query.rb', line 141
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
153
154
155
|
# File 'lib/queries/query.rb', line 153
def match_ordered_wildcard_pieces_in_cached
table[:cached].matches(wildcard_pieces)
end
|
#no_terms? ⇒ Boolean
112
113
114
|
# File 'lib/queries/query.rb', line 112
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
80
81
82
83
84
85
86
87
|
# File 'lib/queries/query.rb', line 80
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_intersection(queries) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/queries/query.rb', line 62
def referenced_klass_intersection(queries)
q = queries.compact
z = referenced_klass.from("( #{q.collect{|i| '(' + i.to_sql + ')' }.join(' INTERSECT ')}) as #{table.name}")
s = Utilities::Strings.random_string(5)
a = table.name + s
referenced_klass.joins("INNER JOIN ( #{z.to_sql} ) AS #{a} ON #{a}.id = #{table.name}.id")
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
127
128
129
|
# File 'lib/queries/query.rb', line 127
def start_and_end_wildcard
'%' + query_string + '%'
end
|
#start_wildcard ⇒ String
117
118
119
|
# File 'lib/queries/query.rb', line 117
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’
169
170
171
172
173
|
# File 'lib/queries/query.rb', line 169
def wildcard_pieces
a = '%' + query_string.gsub(/[^[[:word:]]]+/, '%') + '%' a = 'NothingToMatch' if a.gsub('%','').gsub(' ', '').blank?
a
end
|