Class: Queries::Query

Inherits:
Object
  • Object
show all
Includes:
Arel::Nodes
Defined in:
lib/queries/query.rb

Overview

See

http://www.slideshare.net/camerondutro/advanced-arel-when-activerecord-just-isnt-enough
https://github.com/rails/arel
http://robots.thoughtbot.com/using-arel-to-compose-sql-queries
https://github.com/rails/arel/blob/master/lib/arel/predications.rb
And this:
  http://blog.arkency.com/2013/12/rails4-preloading/
  User.includes(:addresses).where("addresses.country = ?", "Poland").references(:addresses)

TODO: Define #all as a stub (Array or AR)

Direct Known Subclasses

BiologicalCollectionObjectAutocompleteQuery, CollectionObjectFilterQuery, ControlledVocabularyTermAutocompleteQuery, DocumentAutocompleteQuery, GeographicAreaAutocompleteQuery, OtuAutocompleteQuery, RepositoryAutocompleteQuery, SourceFilterQuery, TagAutocompleteQuery, TaxonNameAutocompleteQuery, TaxonNameRelationshipsFilterQuery, TypeMaterialAutocompleteQuery

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Query) initialize(string, project_id: nil, **keyword_args)

Returns a new instance of Query



22
23
24
25
26
# File 'lib/queries/query.rb', line 22

def initialize(string, project_id: nil, **keyword_args)
  @query_string = string
  @project_id = project_id
  build_terms
end

Instance Attribute Details

- (Object) dynamic_limit

limit based on size and potentially properties of terms



20
21
22
# File 'lib/queries/query.rb', line 20

def dynamic_limit
  @dynamic_limit
end

- (Object) project_id

Returns the value of attribute project_id



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

def project_id
  @project_id
end

- (Object) query_string

Returns the value of attribute query_string



15
16
17
# File 'lib/queries/query.rb', line 15

def query_string
  @query_string
end

- (Object) terms

Returns the value of attribute terms



16
17
18
# File 'lib/queries/query.rb', line 16

def terms
  @terms
end

Instance Method Details

- (Array) autocomplete

Returns default the autocomplete result to all

Returns:

  • (Array)

    default the autocomplete result to all



206
207
208
# File 'lib/queries/query.rb', line 206

def autocomplete
  all.to_a
end

- (Object) build_terms

Replace with a full text indexing approach



96
97
98
# File 'lib/queries/query.rb', line 96

def build_terms
  @terms = [end_wildcard, start_and_end_wildcard]  # query_string.split(/\s+/).compact.collect{|t| [t, "#{t}%", "%#{t}%"]}.flatten
end

- (ActiveRecord::Relation?) cached

cached matches full query string wildcarded

Returns:

  • (ActiveRecord::Relation, nil)


186
187
188
189
190
191
192
# File 'lib/queries/query.rb', line 186

def cached
  if !terms.empty?
    table[:cached].matches_any(terms)
  else
    nil
  end
end

- (Object) combine_or_clauses(clauses)

Raises:

  • (TaxonWorks::Error)


194
195
196
197
198
199
200
201
202
# File 'lib/queries/query.rb', line 194

def combine_or_clauses(clauses)
  clauses.compact!
  raise TaxonWorks::Error, 'combine_or_clauses called without a clause, ensure at least one exists' unless !clauses.empty?
  a = clauses.shift
  clauses.each do |b|
    a = a.or(b)
  end
  a
end

- (Object) end_wildcard



48
49
50
# File 'lib/queries/query.rb', line 48

def end_wildcard
  query_string + '%'
end

- (Object) exactly_named



152
153
154
# File 'lib/queries/query.rb', line 152

def exactly_named
  table[:name].eq(query_string)
end

- (Array) fragments

Returns if 5 or fewer strings, those strings wrapped in wildcards, else none

Returns:

  • (Array)

    if 5 or fewer strings, those strings wrapped in wildcards, else none



87
88
89
90
91
92
93
# File 'lib/queries/query.rb', line 87

def fragments
  if strings && strings.count < 6
    strings.collect{|a| "%#{a}%"}
  else
    []
  end
end

- (Object) identifier_table



169
170
171
# File 'lib/queries/query.rb', line 169

def identifier_table
  Identifier.arel_table
end

- (Object) integers



61
62
63
# File 'lib/queries/query.rb', line 61

def integers
  query_string.split(/\s+/).select{|t| Utilities::Strings.is_i?(t)}
end

- (Object) named



148
149
150
# File 'lib/queries/query.rb', line 148

def named
  table[:name].matches_any(terms)
end

- (Object) no_digits



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

def no_digits 
  query_string.gsub(/\d/, '').strip
end

- (Query?) only_ids

Returns used in or_clauses, match on id only if integers alone provided.

Returns:

  • (Query, nil)

    used in or_clauses, match on id only if integers alone provided.



140
141
142
143
144
145
146
# File 'lib/queries/query.rb', line 140

def only_ids
  if only_integers?
    with_id
  else
    nil
  end 
end

- (Boolean) only_integers?

Returns true if the query string only contains integers

Returns:

  • (Boolean)

    true if the query string only contains integers



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

def only_integers?
  !(query_string =~ /[^\d\s]/i) && !integers.empty? 
end

- (Object) parent



156
157
158
# File 'lib/queries/query.rb', line 156

def parent 
  table.alias 
end

- (Object) parent_child_join

generic multi-use bits



117
118
119
# File 'lib/queries/query.rb', line 117

def parent_child_join
  table.join(parent).on(table[:parent_id].eq(parent[:id])).join_sources # !! join_sources ftw
end

- (Object) parent_child_where

Match at two levels, for example, 'wa te“ will match ”Washington Co., Texas“



122
123
124
125
126
# File 'lib/queries/query.rb', line 122

def parent_child_where
  a,b = query_string.split(/\s+/, 2)
  return table[:id].eq(-1) if a.nil? || b.nil?
  table[:name].matches("#{a}%").and(parent[:name].matches("#{b}%"))
end

- (Array) result

Returns the results of the query as an array

Returns:

  • (Array)

    the results of the query as an array



30
31
32
# File 'lib/queries/query.rb', line 30

def result
  []
end

- (Object) scope



34
35
36
# File 'lib/queries/query.rb', line 34

def scope
  where('1 = 2') 
end

- (String) start_and_end_wildcard

Returns:

  • (String)


53
54
55
# File 'lib/queries/query.rb', line 53

def start_and_end_wildcard
  '%' + query_string + '%'
end

- (Object) start_wildcard



44
45
46
# File 'lib/queries/query.rb', line 44

def start_wildcard
  '%' + query_string 
end

- (Object) strings



70
71
72
73
# File 'lib/queries/query.rb', line 70

def strings
  a = query_string.split(/\s+/).select{|t| !(t =~ /\d+/)} 
  a.empty? ? [ query_string] : a
end

- (Array) wildcard_wrapped_integers

Returns:

  • (Array)


66
67
68
# File 'lib/queries/query.rb', line 66

def wildcard_wrapped_integers
  integers.collect{|i| "%#{i}%"}
end

- (Query?) with_id

Returns used in or_clauses

Returns:

  • (Query, nil)

    used in or_clauses



130
131
132
133
134
135
136
# File 'lib/queries/query.rb', line 130

def with_id
  if integers.any?
    table[:id].eq_any(integers)
  else
    nil
  end
end

- (Object) with_identifier



180
181
182
# File 'lib/queries/query.rb', line 180

def with_identifier
  identifier_table[:cached].eq(query_string)
end

- (Arel::Nodes::Grouping) with_identifier_like

Returns:

  • (Arel::Nodes::Grouping)


174
175
176
177
178
# File 'lib/queries/query.rb', line 174

def with_identifier_like
  a = [ start_and_end_wildcard ]
  a = a + wildcard_wrapped_integers
  identifier_table[:cached].matches_any(a)
end

- (Object) with_project_id

TODO: nil/or clause this



161
162
163
164
165
166
167
# File 'lib/queries/query.rb', line 161

def with_project_id
  if project_id 
    table[:project_id].eq(project_id)
  else
    nil      
  end
end

- (Object) years



75
76
77
# File 'lib/queries/query.rb', line 75

def years
  integers.select{|a| a =~ /\b\d{4}\b/}.map(&:to_s).compact
end