Class: Queries::AssertedEnvironment::Autocomplete

Inherits:
Query::Autocomplete show all
Defined in:
lib/queries/asserted_environment/autocomplete.rb

Constant Summary collapse

DEFAULT_LIMIT =
20

Constants inherited from Query::Autocomplete

Query::Autocomplete::DELEGATED_DEPTH, Query::Autocomplete::DELEGATED_MINIMUM_LENGTH, Query::Autocomplete::LITERAL_RESTRICTION_MAX

Instance Attribute Summary

Attributes inherited from Query::Autocomplete

#delegated_results, #dynamic_limit, #limit, #project_id, #query_string, #restrict_to

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Autocomplete

#apply_restriction, #asserted_object_autocomplete, #asserted_object_restriction, #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, #cached_facet, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #delegated_autocomplete, #exactly_named, #fragments, #integers, #least_levenshtein, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #only_ids, #only_integers?, #order_by_id_rank, #parent, #parent_child_join, #parent_child_where, #pieces, #safe_integers, #scope, #string_fragments, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(string, project_id: nil, restrict_to: nil, limit: nil) ⇒ Autocomplete

Returns a new instance of Autocomplete.



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

def initialize(string, project_id: nil, restrict_to: nil, limit: nil)
  super(string, project_id:, restrict_to:, limit:)
end

Instance Method Details

#autocomplete ⇒ Array

Returns:

  • (Array)


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
# File 'lib/queries/asserted_environment/autocomplete.rb', line 52

def autocomplete
  # Method names, built in turn, so that later queries (e.g. the object
  # ones, which run the object autocompletes) don't run once the
  # results are filled
  queries = %i[
    autocomplete_exact_id
    autocomplete_uri_label_contains_match
    autocomplete_uri_exact
    autocomplete_uri_ends_with
    autocomplete_collecting_event_object
    autocomplete_otu_object
    autocomplete_gazetteer_object
  ]

  result = []
  queries.each do |m|
    q = send(m)
    next if q.nil?

    q = q.where(project_id:) if project_id.present?
    result += apply_restriction(q).to_a
    result.uniq!
    break if result.count >= limit
  end

  result.first(limit)
end

#autocomplete_collecting_event_object ⇒ Object



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

def autocomplete_collecting_event_object
  autocomplete_object('CollectingEvent', ::Queries::CollectingEvent::Autocomplete)
end

#autocomplete_gazetteer_object ⇒ Object



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

def autocomplete_gazetteer_object
  autocomplete_object('Gazetteer', ::Queries::Gazetteer::Autocomplete)
end

#autocomplete_object(object_type, object_autocomplete_class) ⇒ Scope?

Parameters:

  • object_type (String)
  • object_autocomplete_class (Class)

Returns:

  • (Scope, nil)


41
42
43
44
45
46
47
48
49
# File 'lib/queries/asserted_environment/autocomplete.rb', line 41

def autocomplete_object(object_type, object_autocomplete_class)
  return nil if query_string.length < 3

  asserted_object_autocomplete(
    object_type:,
    object_autocomplete_class:,
    object_association: :asserted_environment_object
  )
end

#autocomplete_otu_object ⇒ Object



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

def autocomplete_otu_object
  autocomplete_object('Otu', ::Queries::Otu::Autocomplete)
end

#autocomplete_uri_ends_with ⇒ Object



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

def autocomplete_uri_ends_with
  return nil if query_string.length < 3
  base_query.where('asserted_environments.uri ILIKE ?', '%' + query_string).limit(20)
end

#autocomplete_uri_exact ⇒ Object



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

def autocomplete_uri_exact
  return nil if query_string.length < 10
  base_query.where(uri: query_string).limit(20)
end

#autocomplete_uri_label_contains_match ⇒ Object



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

def autocomplete_uri_label_contains_match
  return nil if query_string.length < 3
  base_query.where('asserted_environments.uri_label ILIKE ?', '%' + query_string + '%').limit(20)
end