Class: Queries::TaxonNameAutocompleteQuery

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/taxon_name_autocomplete_query.rb

Instance Attribute Summary (collapse)

Attributes inherited from Query

#dynamic_limit, #project_id, #query_string, #terms

Instance Method Summary (collapse)

Methods inherited from Query

#build_terms, #cached, #end_wildcard, #exactly_named, #fragments, #identifier_table, #integers, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #result, #scope, #start_and_end_wildcard, #start_wildcard, #strings, #wildcard_wrapped_integers, #with_id, #with_identifier_like, #with_project_id, #years

Constructor Details

- (TaxonNameAutocompleteQuery) initialize(string, project_id: nil, valid: nil, nomenclature_group: [], type: [], parent_id: [], exact: false)

Returns a new instance of TaxonNameAutocompleteQuery



31
32
33
34
35
36
37
38
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 31

def initialize(string, project_id: nil, valid: nil, nomenclature_group: [], type: [], parent_id: [], exact: false)
  @nomenclature_group = nomenclature_group
  @valid = valid == 'true' ? true : (valid == 'false' ? false : nil) 
  @type = type 
  @parent_id = parent_id 
  @exact = exact == 'true' ? true : (exact == 'false' ? false : nil) 
  super
end

Instance Attribute Details

- (Boolean) exact

Returns &exact=<“true”|“false”> if 'true' then only #name = query_string results are returned (no fuzzy matching)

Returns:

  • (Boolean)

    &exact=<“true”|“false”> if 'true' then only #name = query_string results are returned (no fuzzy matching)



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

def exact
  @exact
end

- (Object) nomenclature_group

and_clause



8
9
10
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 8

def nomenclature_group
  @nomenclature_group
end

- (Array) parent_id

Returns &parent_id[]=<int>&parent_id=<other_int> etc.

Returns:

  • (Array)

    &parent_id[]=<int>&parent_id=<other_int> etc.



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

def parent_id
  @parent_id
end

- (Array) type

Returns &type[]=<Protonym, Combination, Hybrid, etc.>&type[]=<other type> etc.

Returns:

  • (Array)

    &type[]=<Protonym, Combination, Hybrid, etc.>&type[]=<other type> etc.



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

def type
  @type
end

- (Boolean?) valid

Returns &valid=<“true”|“false”>

if 'true'  then id == cached_valid_taxon_name_id
if 'false' then id != cached_valid_taxon_name
if nil   then no check made, i.e. all names

string is converted to Boolean here

Returns:

  • (Boolean, nil)

    &valid=<“true”|“false”>

    if 'true'  then id == cached_valid_taxon_name_id
    if 'false' then id != cached_valid_taxon_name
    if nil   then no check made, i.e. all names

    string is converted to Boolean here



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

def valid
  @valid
end

Instance Method Details

- (Object) all



123
124
125
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 123

def all
  TaxonName.select('taxon_names.*, length("cached")').includes(:ancestor_hierarchies).where(where_sql).references(:taxon_name_hierarchies).limit(dynamic_limit).order('length("cached"), "cached"').uniq.all
end

- (Object) and_clauses



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 60

def and_clauses
  clauses = [
    valid_state,
    is_type,
    with_parent_id,
    with_nomenclature_group
  ].compact

  return nil if clauses.nil?
  
  a = clauses.shift
  clauses.each do |b|
    a = a.and(b)
  end
  a
end

- (Object) is_type

and clause



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

def is_type
  return nil if type.empty?
  table[:type].eq_any(type)
end

- (Object) or_and



77
78
79
80
81
82
83
84
85
86
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 77

def or_and
  a = or_clauses
  b = and_clauses

  if a && b
    a.and(b)
  else
    a
  end
end

- (Object) or_clauses



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 40

def or_clauses
  clauses = []

  clauses.push exactly_named if exact

  clauses += [
    only_ids,
    cached,
    with_cached_author_year,
  ] unless exact

  clauses.compact!

  a = clauses.shift
  clauses.each do |b|
    a = a.or(b)
  end
  a
end

- (Object) table



127
128
129
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 127

def table
  TaxonName.arel_table
end

- (Object) taxon_name_hierarchies_table



131
132
133
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 131

def taxon_name_hierarchies_table
  Arel::Table.new('taxon_name_hierarchies')
end

- (Arel::Nodes::<>?) valid_state

and clause

Returns:

  • (Arel::Nodes::<>, nil)


94
95
96
97
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 94

def valid_state
  return nil if @valid.nil?
  valid ? table[:id].eq(table[:cached_valid_taxon_name_id]) : table[:id].not_eq(table[:cached_valid_taxon_name_id])
end

- (Object) where_sql



88
89
90
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 88

def where_sql
  with_project_id.and(or_and).to_sql
end

- (Object) with_cached_author_year



135
136
137
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 135

def with_cached_author_year
  table[:cached_author_year].matches_any(terms)
end

- (Arel::Nodes::Grouping?) with_nomenclature_group

Returns and clause

Returns:

  • (Arel::Nodes::Grouping, nil)

    and clause



113
114
115
116
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 113

def with_nomenclature_group
  return nil if nomenclature_group.empty?
  table[:rank_class].matches_any(nomenclature_group)
end

- (Object) with_parent_id

and clause, limit to ancestors or [ids]



106
107
108
109
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 106

def with_parent_id 
  return nil if parent_id.empty?
  taxon_name_hierarchies_table[:ancestor_id].eq_any(parent_id)
end