Class: Queries::Person::Autocomplete

Inherits:
Query
  • Object
show all
Includes:
Concerns::AlternateValues, Concerns::Tags
Defined in:
lib/queries/person/autocomplete.rb

Instance Attribute Summary collapse

Attributes inherited from Query

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

Instance Method Summary collapse

Methods inherited from Query

#alphabetic_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, #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)


17
18
19
20
21
22
23
# File 'lib/queries/person/autocomplete.rb', line 17

def initialize(string, params = {})
  @limit_to_roles = params[:roles] # roles
  set_identifier(params)
  set_tags_params(params)
  set_alternate_value(params)
  super
end

Instance Attribute Details

#limit_to_rolesArray

Parameters:

  • limit_to_role (String)

    any Role class, like `TaxonNameAuthor`, `SourceAuthor`, `SourceEditor`, `Collector` … etc.

Returns:

  • (Array)


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

def limit_to_roles
  @limit_to_roles
end

Instance Method Details

#autocompleteArray

Returns:

  • (Array)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/queries/person/autocomplete.rb', line 96

def autocomplete
  return [] if query_string.blank?
  queries = [
    autocomplete_exact_match,
    autocomplete_exact_inverted,
    autocomplete_identifier_cached_exact,
    autocomplete_identifier_identifier_exact,
    autocomplete_exact_id,
    autocomplete_exact_last_name_match,
    autocomplete_alternate_values_last_name,
    autocomplete_alternate_values_first_name,
    autocomplete_ordered_wildcard_pieces_in_cached,
    autocomplete_cached_wildcard_anywhere, # in Queries::Query
    autocomplete_cached
  ]

  queries.compact!

  updated_queries = []
  queries.each_with_index do |q ,i|
    a = q.joins(:roles).where(role_match.to_sql) if roles_assigned?
    a ||= q
    updated_queries[i] = a
  end

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

#autocomplete_alternate_values_first_nameObject



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

def autocomplete_alternate_values_first_name
  matching_alternate_value_on(:first_name).limit(20) 
end

#autocomplete_alternate_values_last_nameObject



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

def autocomplete_alternate_values_last_name
  matching_alternate_value_on(:last_name).limit(20) 
end

#autocomplete_exact_invertedScope

Returns:

  • (Scope)


56
57
58
59
60
# File 'lib/queries/person/autocomplete.rb', line 56

def autocomplete_exact_inverted
  base_query.where(
    table[:cached].eq(invert_name).to_sql
  ).limit(20)
end

#autocomplete_exact_last_name_matchObject



49
50
51
52
53
# File 'lib/queries/person/autocomplete.rb', line 49

def autocomplete_exact_last_name_match
  base_query.where(
    table[:last_name].eq(query_string).to_sql
  ).limit(20)
end

#autocomplete_exact_matchScope

Returns:

  • (Scope)


43
44
45
46
47
# File 'lib/queries/person/autocomplete.rb', line 43

def autocomplete_exact_match
  base_query.where(
    table[:cached].eq(normalize_name).to_sql
  ).limit(20)
end

#base_queryScope

Returns:

  • (Scope)


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

def base_query
  ::Person.select('people.*')
end

#invert_nameString

Returns simple name inversion given `Sarah Smith` return `Smith, Sarah`.

Returns:

  • (String)

    simple name inversion given `Sarah Smith` return `Smith, Sarah`



79
80
81
82
83
# File 'lib/queries/person/autocomplete.rb', line 79

def invert_name
  normalize(
    query_string.split(/\s+/, 2).reverse.map(&:strip).join(', ')
  )
end

#normalize(string) ⇒ String

TODO: Use bibtex parser!!

Parameters:

  • string (String)

Returns:

  • (String)


73
74
75
# File 'lib/queries/person/autocomplete.rb', line 73

def normalize(string)
  string.strip.split(/\s*\,\s*/, 2).join(', ')
end

#normalize_nameString

Returns:

  • (String)


86
87
88
# File 'lib/queries/person/autocomplete.rb', line 86

def normalize_name
  normalize(query_string)
end

#role_matchArel::Nodes::Equatity

Returns:

  • (Arel::Nodes::Equatity)


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

def role_match
  a = roles_table[:type].eq_any(limit_to_roles)
  a = a.and(roles_table[:project_id].eq(project_id)) if !project_id.blank?
  a
end

#role_project_matchArel::Nodes::Equatity

Returns:

  • (Arel::Nodes::Equatity)


38
39
40
# File 'lib/queries/person/autocomplete.rb', line 38

def role_project_match
  roles_table[:project_id].eq(project_id)
end

#roles_assigned?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/queries/person/autocomplete.rb', line 91

def roles_assigned?
  limit_to_roles.kind_of?(Array) && limit_to_roles.any?
end

#roles_tableArel::Table

Returns:

  • (Arel::Table)


136
137
138
# File 'lib/queries/person/autocomplete.rb', line 136

def roles_table
  ::Role.arel_table
end

#tableArel::Table

Returns:

  • (Arel::Table)


131
132
133
# File 'lib/queries/person/autocomplete.rb', line 131

def table
  ::Person.arel_table
end