Class: Queries::Person::Autocomplete
  
  
  
  
  
    - Inherits:
- 
      Query::Autocomplete
      
        
        show all
      
    
      - Includes:
- Concerns::AlternateValues, Concerns::Tags
    - Defined in:
- lib/queries/person/autocomplete.rb
 
  Instance Attribute Summary collapse
  
  
  
  
  #dynamic_limit, #project_id, #query_string
  
  
  Attributes inherited from Query
  #query_string, #terms
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  
  
  
  #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, #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
  
    
  
  
    #initialize(string, **params)  ⇒ Autocomplete 
  
  
  
  
    
Returns a new instance of Autocomplete.
   
 
  
    | 
21
22
23
24
25
26
27
28 | # File 'lib/queries/person/autocomplete.rb', line 21
def initialize(string, **params)
  @role_type = params[:role_type]
  @in_project = boolean_param(params, :in_project)
  set_tags_params(params)
  set_alternate_value(params)
  super
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #in_project  ⇒ Boolean? 
  
  
  
  
    
Returns true - restrict to only people used in this project nil, false - ignored.
   
 
  
    | 
17
18
19 | # File 'lib/queries/person/autocomplete.rb', line 17
def in_project
  @in_project
end | 
 
    
      
      
      
  
  
    #role_type  ⇒ Array 
  
  
  
  
    | 
12
13
14 | # File 'lib/queries/person/autocomplete.rb', line 12
def role_type
  @role_type
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #autocomplete  ⇒ Array 
  
  
  
  
    | 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 | # File 'lib/queries/person/autocomplete.rb', line 114
def autocomplete
  return [] if query_string.blank? || project_id.empty?
  queries = [
    [ autocomplete_exact_id, false ],
    [ autocomplete_exact_match&.limit(5), true ],
    [ autocomplete_exact_inverted&.limit(5), true ],
    [ autocomplete_identifier_cached_exact, false ],
    [ autocomplete_identifier_identifier_exact, false ],
    [ autocomplete_identifier_cached_suffix_exact, false ],
    [ autocomplete_exact_last_name_match.limit(20), true ],
    [ autocomplete_alternate_values_last_name.limit(20), true ],
    [ autocomplete_alternate_values_first_name.limit(20), true ],
    [ autocomplete_ordered_wildcard_pieces_in_cached&.limit(5), true ],
    [ autocomplete_cached_wildcard_anywhere&.limit(20), true ],     [ autocomplete_cached.limit(20), true ]
  ]
  queries.compact!
  updated_queries = []
  pr_id = project_id.join(',') if project_id
  queries.each_with_index do |q, i|
    a = q[0]
    if !a.nil?
      if role_type.present?
        a = a.joins(:roles).where(role_match.to_sql)
      end
      if q[1] && query_string.length > 1         if project_id.present?
          a = a.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 roles.project_id IN (#{pr_id}) THEN roles.project_id WHEN project_sources.project_id IN (#{pr_id}) THEN project_sources.project_id ELSE NULL END AS in_project")
          .group('people.id, roles.project_id, project_sources.project_id')
          .order('in_project, use_count DESC')
                                                                              end
      end
    end
    updated_queries[i] = a
  end
  result = []
  updated_queries.compact!
  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_name  ⇒ Object 
  
  
  
  
    | 
69
70
71 | # File 'lib/queries/person/autocomplete.rb', line 69
def autocomplete_alternate_values_first_name
  matching_alternate_value_on(:first_name)
end | 
 
    
      
  
  
    #autocomplete_alternate_values_last_name  ⇒ Object 
  
  
  
  
    | 
65
66
67 | # File 'lib/queries/person/autocomplete.rb', line 65
def autocomplete_alternate_values_last_name
  matching_alternate_value_on(:last_name)
end | 
 
    
      
  
  
    #autocomplete_exact_inverted  ⇒ Scope 
  
  
  
  
    | 
57
58
59
60
61
62
63 | # File 'lib/queries/person/autocomplete.rb', line 57
def autocomplete_exact_inverted
  a = invert_name
  return nil if a.nil?
  base_query.where(
    table[:cached].eq(a).to_sql
  )
end | 
 
    
      
  
  
    #autocomplete_exact_last_name_match  ⇒ Object 
  
  
  
  
    | 
50
51
52
53
54 | # File 'lib/queries/person/autocomplete.rb', line 50
def autocomplete_exact_last_name_match
  base_query.where(
    table[:last_name].eq(query_string).to_sql
  )
end | 
 
    
      
  
  
    #autocomplete_exact_match  ⇒ Scope 
  
  
  
  
    | 
42
43
44
45
46
47
48 | # File 'lib/queries/person/autocomplete.rb', line 42
def autocomplete_exact_match
  a = normalize_name
  return nil if a.nil?
  base_query.where(
    table[:cached].eq(normalize_name).to_sql
  )
end | 
 
    
      
  
  
    #autocomplete_identifier_cached_suffix_exact  ⇒ Object 
  
  
  
  
    | 
73
74
75
76
77
78
79
80
81
82
83
84
85
86 | # File 'lib/queries/person/autocomplete.rb', line 73
def autocomplete_identifier_cached_suffix_exact
    a = base_query.joins(:identifiers)
    .where("identifiers.type = ? AND (identifiers.cached = ? OR identifiers.cached = ?)",
      'Identifier::Global::Orcid', "https://orcid.org/#{query_string}",
      "http://orcid.org/#{query_string}")
    b = base_query.joins(:identifiers)
    .where("identifiers.type = ? AND identifiers.cached = ?",
      'Identifier::Global::Wikidata', "Q#{query_string}")
  Queries::union(::Person, [a,b])
end | 
 
    
      
  
  
    #invert_name  ⇒ String 
  
  
  
  
    | 
100
101
102
103
104 | # File 'lib/queries/person/autocomplete.rb', line 100
def invert_name
  a = normalize( query_string.split(/\s+/, 2).reverse.map(&:strip).join(', ') )
  return nil if a.nil?
  a
end | 
 
    
      
  
  
    #normalize(string)  ⇒ String? 
  
  
  
  
    
TODO: Use bibtex parser!!
   
 
  
    | 
91
92
93
94
95 | # File 'lib/queries/person/autocomplete.rb', line 91
def normalize(string)
  n = string.strip.split(/\s*\,\s*/, 2).join(', ')
  return nil unless n.include?(' ')
  n
end | 
 
    
      
  
  
    #normalize_name  ⇒ String 
  
  
  
  
    | 
107
108
109
110
111 | # File 'lib/queries/person/autocomplete.rb', line 107
def normalize_name
  a = normalize(query_string)
  return nil if a.nil?
  a
end | 
 
    
      
  
  
    #role_match  ⇒ Arel::Nodes::Equatity 
  
  
  
  
    | 
35
36
37
38
39 | # File 'lib/queries/person/autocomplete.rb', line 35
def role_match
  a = roles_table[:type].in(role_type)
  a = a.and(roles_table[:project_id].in(project_id)) if in_project
  a
end | 
 
    
      
  
  
    #roles_table  ⇒ Arel::Table 
  
  
  
  
    | 
176
177
178 | # File 'lib/queries/person/autocomplete.rb', line 176
def roles_table
  ::Role.arel_table
end |