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)


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
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/queries/person/autocomplete.rb', line 98

def autocomplete
  return [] if query_string.blank?
  queries = [
    autocomplete_exact_match,
    autocomplete_exact_inverted,
    autocomplete_identifier_cached_exact, # do not use extended query
    autocomplete_identifier_identifier_exact, # do not use extended query
    autocomplete_exact_id, # do not use extended query
    autocomplete_exact_last_name_match,
    autocomplete_alternate_values_last_name,
    autocomplete_alternate_values_first_name,
    autocomplete_ordered_wildcard_pieces_in_cached&.limit(5),
    autocomplete_cached_wildcard_anywhere&.limit(20), # in Queries::Query
    autocomplete_cached
  ]

  queries.compact!

  updated_queries = []
  queries.each_with_index do |q ,i|
    if roles_assigned?
      a = q.joins(:roles).where(role_match.to_sql)
    elsif i != 2 && i != 3 && i != 4 # do not use extended query for identifiers
      a = q.left_outer_joins(:roles)
            .joins("LEFT OUTER JOIN sources ON roles.role_object_id = sources.id AND roles.role_object_type = 'Source'")
            .joins('LEFT OUTER JOIN project_sources ON sources.id = project_sources.source_id')
            .select("people.*, COUNT(roles.id) AS use_count, CASE WHEN MAX(roles.project_id) = #{Current.project_id} THEN MAX(roles.project_id) ELSE MAX(project_sources.project_id) END AS in_project_id")
            .where('roles.project_id = ? OR project_sources.project_id = ? OR (roles.project_id IS NULL AND project_sources.project_id IS NULL)', Current.project_id, Current.project_id)
            .group('people.id')
            .order('in_project_id, use_count DESC')
    end
    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 = 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(5)
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(5)
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`



81
82
83
84
85
# File 'lib/queries/person/autocomplete.rb', line 81

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
76
77
# File 'lib/queries/person/autocomplete.rb', line 73

def normalize(string)
  n = string.strip.split(/\s*\,\s*/, 2).join(', ')
  n = 'nothing to match' unless n.include?(' ')
  n
end

#normalize_nameString

Returns:

  • (String)


88
89
90
# File 'lib/queries/person/autocomplete.rb', line 88

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)


93
94
95
# File 'lib/queries/person/autocomplete.rb', line 93

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

#roles_tableArel::Table

Returns:

  • (Arel::Table)


147
148
149
# File 'lib/queries/person/autocomplete.rb', line 147

def roles_table
  ::Role.arel_table
end

#tableArel::Table

Returns:

  • (Arel::Table)


142
143
144
# File 'lib/queries/person/autocomplete.rb', line 142

def table
  ::Person.arel_table
end