Class: Repository::Autocomplete

Inherits:
Queries::Query show all
Includes:
Queries::Concerns::AlternateValues
Defined in:
lib/queries/repository/autocomplete.rb

Instance Attribute Summary

Attributes inherited from Queries::Query

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

Instance Method Summary collapse

Methods inherited from Queries::Query

#alphabetic_strings, #alphanumeric_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, #levenshtein_distance, #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, #string_fragments, #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, **params) ⇒ Autocomplete

Returns a new instance of Autocomplete.

Parameters:

  • args (Hash)


8
9
10
11
12
# File 'lib/queries/repository/autocomplete.rb', line 8

def initialize(string, **params)
  set_identifier(params)
  set_alternate_value(params)
  super
end

Instance Method Details

#autocompleteArray

Returns:

  • (Array)


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

def autocomplete
  return [] if query_string.blank?
  queries = [
    [autocomplete_exact_id, nil ],
    [autocomplete_exactly_named, nil],
    [autocomplete_exact_acronym, nil],
    [autocomplete_identifier_identifier_exact, nil],
    [autocomplete_acronym_match.limit(5), nil],
    [autocomplete_named, true ],
    [autocomplete_alternate_values_acronym.limit(20), true ],
    [autocomplete_alternate_values_name.limit(20), true ]
  ]

  queries.delete_if{|a,b| a.nil?} # Note this pattern differs because [[]] so we don't use compact. /lib/queries/source/autocomplete.rb follows same pattern

  result = []

  queries.each do |q, scope|
    a = q
    if project_id && scope
      # TODO: there are now 2 repository references, see also current_repository_id
      a = a.select('repositories.*, COUNT(collection_objects.id) AS use_count, NULLIF(collection_objects.project_id, NULL) as in_project')
        .left_outer_joins(:collection_objects)
        .where('collection_objects.project_id = ? OR collection_objects.project_id IS DISTINCT FROM ?', project_id, project_id)
        .group('repositories.id, collection_objects.project_id')
        .order('use_count DESC')
    end

    a ||= q

    result += a.to_a
    result.uniq!
    break if result.count > 40
  end
  result[0..40]
end

#autocomplete_acronym_matchScope

Returns:

  • (Scope)


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

def autocomplete_acronym_match
  base_query.where(
    table[:acronym].matches_any(terms).to_sql
  )
end

#autocomplete_alternate_values_acronymObject



37
38
39
# File 'lib/queries/repository/autocomplete.rb', line 37

def autocomplete_alternate_values_acronym
  matching_alternate_value_on(:acronym)
end

#autocomplete_alternate_values_nameObject



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

def autocomplete_alternate_values_name
  matching_alternate_value_on(:name)
end

#autocomplete_exact_acronymScope

Returns:

  • (Scope)


31
32
33
34
35
# File 'lib/queries/repository/autocomplete.rb', line 31

def autocomplete_exact_acronym
  base_query.where(
    table[:acronym].eq(query_string).to_sql
  )
end

#base_queryObject



19
20
21
# File 'lib/queries/repository/autocomplete.rb', line 19

def base_query
  ::Repository.select('repositories.*')
end

#tableArel::Table

Returns:

  • (Arel::Table)


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

def table
  ::Repository.arel_table
end