Class: Queries::CollectingEvent::Autocomplete

Inherits:
Query
  • Object
show all
Includes:
Queries::Concerns::DateRanges, Queries::Concerns::Roles
Defined in:
lib/queries/collecting_event/autocomplete.rb

Instance Attribute Summary

Attributes inherited from Query

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

Instance Method Summary collapse

Methods inherited from Query

#alphabetic_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, #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, #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) ⇒ Autocomplete

Returns a new instance of Autocomplete



10
11
12
# File 'lib/queries/collecting_event/autocomplete.rb', line 10

def initialize(string, project_id: nil)
  super
end

Instance Method Details

#autocompleteArray

Returns TODO: optimize limits

Returns:

  • (Array)

    TODO: optimize limits



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/queries/collecting_event/autocomplete.rb', line 80

def autocomplete
  queries = [
    autocomplete_exact_id,
    autocomplete_verbatim_label_md5,
    autocomplete_identifier_identifier_exact,
    autocomplete_identifier_cached_exact,
    autocomplete_identifier_cached_like.limit(4),
    autocomplete_verbatim_trip_identifier_match,
    autocomplete_start_or_end_date,
    autocomplete_start_date_wild_card(:verbatim_locality), 
    autocomplete_start_date_wild_card(:cached),
    autocomplete_matching_collectors,
    autocomplete_verbatim_latitude_or_longitude,
    autocomplete_verbatim_collectors_wildcard,
    autocomplete_verbatim_date,
    autocomplete_verbatim_habitat,
    autocomplete_verbatim_locality_wildcard_end,
    autocomplete_verbatim_locality_wildcard_end_starting_year,
    
    autocomplete_cached_wildcard_anywhere,
  ]

  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

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

#autocomplete_matching_collectorsObject



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

def autocomplete_matching_collectors
  return nil if no_terms?
  matching_person_cached(:collector).limit(20)
end

#autocomplete_start_date_wild_card(field = :verbatim_locality) ⇒ Object



29
30
31
32
33
34
# File 'lib/queries/collecting_event/autocomplete.rb', line 29

def autocomplete_start_date_wild_card(field = :verbatim_locality) 
  a = with_start_date 
  b = fragments
  return nil if a.nil? || b.empty? || field.nil?
  base_query.where( a.and(table[field].matches(b.join)).to_sql).limit(20)
end

#autocomplete_verbatim_collectors_wildcardObject



40
41
42
43
# File 'lib/queries/collecting_event/autocomplete.rb', line 40

def autocomplete_verbatim_collectors_wildcard
  return nil if query_string.length < 4
  base_query.where( table[:verbatim_collectors].matches(start_and_end_wildcard).to_sql).limit(20)
end

#autocomplete_verbatim_dateObject



50
51
52
53
# File 'lib/queries/collecting_event/autocomplete.rb', line 50

def autocomplete_verbatim_date
  return nil if query_string.length < 4
  base_query.where( table[:verbatim_date].matches(start_and_end_wildcard).to_sql).limit(20)
end

#autocomplete_verbatim_habitatObject



55
56
57
58
# File 'lib/queries/collecting_event/autocomplete.rb', line 55

def autocomplete_verbatim_habitat
  return nil if query_string.length < 4
  base_query.where( table[:verbatim_habitat].matches(start_and_end_wildcard).to_sql).limit(20)
end

#autocomplete_verbatim_label_md5Object



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

def autocomplete_verbatim_label_md5
  return nil if query_string.length < 4
  md5 = Utilities::Strings.generate_md5(query_string) 
  base_query.where( table[:md5_of_verbatim_label].eq(md5)).limit(3)
end

#autocomplete_verbatim_latitude_or_longitudeObject



60
61
62
63
64
65
66
# File 'lib/queries/collecting_event/autocomplete.rb', line 60

def autocomplete_verbatim_latitude_or_longitude
  return nil if query_string.nil?
  base_query.where(
    table[:verbatim_latitude].matches(start_and_end_wildcard)
    .or(table[:verbatim_longitude].matches(start_and_end_wildcard)).to_sql
  ).limit(20)
end

#autocomplete_verbatim_locality_wildcard_endObject



45
46
47
48
# File 'lib/queries/collecting_event/autocomplete.rb', line 45

def autocomplete_verbatim_locality_wildcard_end
  return nil if query_string.length < 4
  base_query.where( table[:verbatim_locality].matches(end_wildcard).to_sql).limit(20)
end

#autocomplete_verbatim_locality_wildcard_end_starting_yearObject



68
69
70
71
72
73
74
75
76
# File 'lib/queries/collecting_event/autocomplete.rb', line 68

def autocomplete_verbatim_locality_wildcard_end_starting_year
  a = years 
  return nil if query_string.length < 7 || a.empty?
  base_query.where(
    table[:start_date_year].eq_any(a).
    and( table[:verbatim_locality].matches(fragments.join))
    .to_sql)
    .limit(20)
end

#autocomplete_verbatim_trip_identifier_matchObject



36
37
38
# File 'lib/queries/collecting_event/autocomplete.rb', line 36

def autocomplete_verbatim_trip_identifier_match
  base_query.where( table[:verbatim_trip_identifier].matches(end_wildcard).to_sql).limit(20)
end

#base_queryObject



14
15
16
# File 'lib/queries/collecting_event/autocomplete.rb', line 14

def base_query
  ::CollectingEvent.select('collecting_events.*')
end

#tableArel::Table

Returns:

  • (Arel::Table)


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

def table
  ::CollectingEvent.arel_table
end