Class: Otu::Autocomplete

Inherits:
Queries::Query show all
Defined in:
lib/queries/otu/autocomplete.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)

Instance Attribute Summary collapse

Attributes inherited from Queries::Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary collapse

Methods inherited from Queries::Query

#alphabetic_strings, #alphanumeric_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, #string_fragments, #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, having_taxon_name_only: false) ⇒ Autocomplete

Returns a new instance of Autocomplete.



19
20
21
22
# File 'lib/queries/otu/autocomplete.rb', line 19

def initialize(string, project_id: nil, having_taxon_name_only: false)
  super(string, project_id: project_id)
  @having_taxon_name_only = having_taxon_name_only&.to_s&.downcase == 'true'
end

Instance Attribute Details

#having_taxon_name_onlyObject

Returns the value of attribute having_taxon_name_only.



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

def having_taxon_name_only
  @having_taxon_name_only
end

Instance Method Details

#autocompleteArray

Returns:

  • (Array)


64
65
66
67
68
69
70
71
72
73
# File 'lib/queries/otu/autocomplete.rb', line 64

def autocomplete
  updated_queries = base_queries      
  result = []
  updated_queries.each do |q|
    result += q.to_a
    result.uniq!
    break if result.count > 39 
  end
  result[0..39]
end

#autocomplete_via_taxon_name_autocompleteScope

Returns:

  • (Scope)


76
77
78
79
80
81
82
83
84
85
# File 'lib/queries/otu/autocomplete.rb', line 76

def autocomplete_via_taxon_name_autocomplete
  taxon_names = Queries::TaxonName::Autocomplete.new(query_string, project_id: project_id).autocomplete
  (
    having_taxon_name_only ? ::Otu.where(name: nil) : ::Otu
  )
    .joins(:taxon_name)
    .where(taxon_name: taxon_names)
    .references(:taxon_names)
    .limit(40).order(Arel.sql('char_length(otus.name), char_length(taxon_names.cached), taxon_names.cached ASC'))
end

#base_queriesObject



33
34
35
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/otu/autocomplete.rb', line 33

def base_queries
  queries = []
  queries << autocomplete_exactly_named unless having_taxon_name_only
  queries = [
    autocomplete_exact_id,
    autocomplete_identifier_cached_exact,
    autocomplete_identifier_identifier_exact,
  ]
  queries << autocomplete_named unless having_taxon_name_only
  queries += [
    autocomplete_via_taxon_name_autocomplete,
    autocomplete_identifier_cached_like,
    autocomplete_common_name_exact,
    autocomplete_common_name_like
  ]

  queries.compact!

  return [] if queries.nil?
  updated_queries = []

  queries.each_with_index do |q ,i|
    a = q.where(project_id: project_id) if project_id
    a ||= q 
    updated_queries[i] = a
  end

  updated_queries
end

#base_queryObject



24
25
26
# File 'lib/queries/otu/autocomplete.rb', line 24

def base_query
  ::Otu.select('otus.*')
end

#tableArel::Table

Returns:

  • (Arel::Table)


29
30
31
# File 'lib/queries/otu/autocomplete.rb', line 29

def table
  ::Otu.arel_table
end