Class: Queries::CollectingEvent::Autocomplete

Inherits:
Query
  • Object
show all
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, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_exact_id, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #simple_date, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

- (Autocomplete) initialize(string, project_id: nil)

Returns a new instance of Autocomplete



7
8
9
# File 'lib/queries/collecting_event/autocomplete.rb', line 7

def initialize(string, project_id: nil)
  super
end

Instance Method Details

- (Array) autocomplete

Returns TODO: optimize limits

Returns:

  • (Array)

    TODO: optimize limits



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/queries/collecting_event/autocomplete.rb', line 43

def autocomplete
  queries = [
    autocomplete_verbatim_trip_identifier_match,
    autocomplete_identifier_cached_exact,
    autocomplete_identifier_identifier_exact,
    autocomplete_start_date_wild_card(:verbatim_locality), 
    autocomplete_start_date_wild_card(:cached),

    autocomplete_start_date,

    autocomplete_verbatim_locality_wildcard_end,
    autocomplete_verbatim_locality_wildcard_end_starting_year,
    autocomplete_cached_wildcard_anywhere,
    autocomplete_identifier_cached_like,

    # others?
    # - verbatim locality exact match (?) start date
    # - ce_cached wildcard wrapped autocomplete start date
    # - date (exact date)
  ]

  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

- (Object) autocomplete_start_date_wild_card(field = :verbatim_locality)



15
16
17
18
19
20
# File 'lib/queries/collecting_event/autocomplete.rb', line 15

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

- (Object) autocomplete_verbatim_locality_wildcard_end



26
27
28
29
# File 'lib/queries/collecting_event/autocomplete.rb', line 26

def autocomplete_verbatim_locality_wildcard_end
  return nil if query_string.length < 7
  base_query.where( table[:verbatim_locality].matches(query_string + '%').to_sql).limit(20)
end

- (Object) autocomplete_verbatim_locality_wildcard_end_starting_year



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

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

- (Object) autocomplete_verbatim_trip_identifier_match



22
23
24
# File 'lib/queries/collecting_event/autocomplete.rb', line 22

def autocomplete_verbatim_trip_identifier_match
  base_query.where( table[:verbatim_trip_identifier].matches(query_string + '%').to_sql).limit(20)
end

- (Object) base_query



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

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

- (Arel::Table) table

Returns:

  • (Arel::Table)


85
86
87
# File 'lib/queries/collecting_event/autocomplete.rb', line 85

def table
  ::CollectingEvent.arel_table
end