Class: Queries::Organization::Autocomplete

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

Instance Attribute Summary

Attributes inherited from Query::Autocomplete

#dynamic_limit, #project_id, #query_string

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Autocomplete

#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, #exactly_named, #fragments, #initialize, #integers, #least_levenshtein, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #only_ids, #only_integers?, #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

This class inherits a constructor from Queries::Query::Autocomplete

Instance Method Details

#autocompleteArray

Returns:

  • (Array)


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
83
84
# File 'lib/queries/organization/autocomplete.rb', line 58

def autocomplete
  return [] if query_string.blank?

  queries = [
    autocomplete_exact_id,
    autocomplete_exactly_named,
    autocomplete_identifier_identifier_exact,
    autocomplete_identifier_cached_exact,
    autocomplete_name_wildcard_end,
    autocomplete_legal_name_wildcard_end,
    autocomplete_alternate_name_wildcard_end,
    autocomplete_ordered_wildcard_pieces_in_name,
    autocomplete_wildcard_in_name,
    autocomplete_geographic_area_name,
    autocomplete_identifier_cached_like.limit(20)
  ]

  queries.compact!

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

#autocomplete_alternate_name_wildcard_endActiveRecord::Relation?

Returns alternate_name starts with the query string.

Returns:

  • (ActiveRecord::Relation, nil)

    alternate_name starts with the query string



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

def autocomplete_alternate_name_wildcard_end
  return nil if query_string.length < 2
  base_query.where(table[:alternate_name].matches(end_wildcard).to_sql).limit(20)
end

#autocomplete_geographic_area_nameActiveRecord::Relation?

Returns the Organization is in a geographic area whose name matches all query pieces in order, e.g. an Organization in "Illinois" matched by "illin".

Returns:

  • (ActiveRecord::Relation, nil)

    the Organization is in a geographic area whose name matches all query pieces in order, e.g. an Organization in "Illinois" matched by "illin"



50
51
52
53
54
55
# File 'lib/queries/organization/autocomplete.rb', line 50

def autocomplete_geographic_area_name
  return nil if query_string.length < 3
  base_query.joins(:geographic_area).where(
    ::GeographicArea.arel_table[:name].matches(wildcard_pieces).to_sql
  ).limit(20)
end

Returns legal_name starts with the query string.

Returns:

  • (ActiveRecord::Relation, nil)

    legal_name starts with the query string



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

def autocomplete_legal_name_wildcard_end
  return nil if query_string.length < 2
  base_query.where(table[:legal_name].matches(end_wildcard).to_sql).limit(20)
end

#autocomplete_name_wildcard_endActiveRecord::Relation?

Returns name starts with the query string.

Returns:

  • (ActiveRecord::Relation, nil)

    name starts with the query string



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

def autocomplete_name_wildcard_end
  return nil if query_string.length < 2
  base_query.where(table[:name].matches(end_wildcard).to_sql).limit(20)
end

#autocomplete_ordered_wildcard_pieces_in_nameActiveRecord::Relation

Returns name, legal_name or alternate_name matches all query pieces in order, e.g. "nat hist" matches "Natural History Museum".

Returns:

  • (ActiveRecord::Relation)

    name, legal_name or alternate_name matches all query pieces in order, e.g. "nat hist" matches "Natural History Museum"



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

def autocomplete_ordered_wildcard_pieces_in_name
  base_query.where(
    table[:name].matches(wildcard_pieces)
      .or(table[:legal_name].matches(wildcard_pieces))
      .or(table[:alternate_name].matches(wildcard_pieces))
      .to_sql
  ).limit(20)
end

#autocomplete_wildcard_in_nameActiveRecord::Relation?

Returns name matches all query fragments, unordered, e.g. "hist nat" matches "Natural History Museum".

Returns:

  • (ActiveRecord::Relation, nil)

    name matches all query fragments, unordered, e.g. "hist nat" matches "Natural History Museum"



41
42
43
44
45
# File 'lib/queries/organization/autocomplete.rb', line 41

def autocomplete_wildcard_in_name
  b = fragments
  return nil if b.empty?
  base_query.where(table[:name].matches_all(b).to_sql).limit(20)
end