Class: Queries::ControlledVocabularyTerm::Autocomplete

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

Instance Attribute Summary collapse

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

Returns a new instance of Autocomplete.



9
10
11
12
13
# File 'lib/queries/controlled_vocabulary_term/autocomplete.rb', line 9

def initialize(string, **keyword_args)
  @query_string = string
  @project_id = keyword_args[:project_id]
  @controlled_vocabulary_term_type = keyword_args[:controlled_vocabulary_term_type] || []
end

Instance Attribute Details

#controlled_vocabulary_term_typeArray

Returns controlled_vocabulary_term_type.

Returns:

  • (Array)

    controlled_vocabulary_term_type



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

def controlled_vocabulary_term_type
  @controlled_vocabulary_term_type
end

Instance Method Details

#allScope

Returns:

  • (Scope)


52
53
54
55
56
57
58
59
60
# File 'lib/queries/controlled_vocabulary_term/autocomplete.rb', line 52

def all
  a = [or_clauses, and_clauses].compact
  b = a.shift
  b = b.and(a.shift) if !a.empty?

  b.nil? ?
    ::ControlledVocabularyTerm.all.order(:type, :name).limit(40) :
    ::ControlledVocabularyTerm.where(b).order(:type, :name).limit(40)
end

#and_clausesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/queries/controlled_vocabulary_term/autocomplete.rb', line 19

def and_clauses
  clauses = [
    with_project_id,
    with_type
  ].compact

  return nil if clauses.empty?

  a = clauses.shift
  clauses.each do |b|
    a = a.and(b)
  end
  a
end

#definition_matchesObject



74
75
76
# File 'lib/queries/controlled_vocabulary_term/autocomplete.rb', line 74

def definition_matches
  table[:definition].matches_any(terms) if terms.any?
end

#keyword_namedObject



62
63
64
# File 'lib/queries/controlled_vocabulary_term/autocomplete.rb', line 62

def keyword_named
  table[:name].matches_any(terms) if terms.any?
end

#or_clausesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/queries/controlled_vocabulary_term/autocomplete.rb', line 34

def or_clauses
  clauses = [
    named,
    definition_matches,
    uri_equal_to,
    with_id
  ].compact

  return nil if clauses.empty?

  a = clauses.shift
  clauses.each do |b|
    a = a.or(b)
  end
  a
end

#uri_equal_toObject



70
71
72
# File 'lib/queries/controlled_vocabulary_term/autocomplete.rb', line 70

def uri_equal_to
  table[:uri].eq(query_string) if query_string.present?
end

#with_typeObject



66
67
68
# File 'lib/queries/controlled_vocabulary_term/autocomplete.rb', line 66

def with_type
  table[:type].in(controlled_vocabulary_term_type) if controlled_vocabulary_term_type.any?
end