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, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #exactly_named, #fragments, #integers, #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_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces
Constructor Details
#initialize(string, **params) ⇒ Autocomplete
Returns a new instance of Autocomplete.
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/queries/person/autocomplete.rb', line 22
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/queries/person/autocomplete.rb', line 102
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_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, 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
72
73
74
|
# File 'lib/queries/person/autocomplete.rb', line 72
def autocomplete_alternate_values_first_name
matching_alternate_value_on(:first_name)
end
|
#autocomplete_alternate_values_last_name ⇒ Object
68
69
70
|
# File 'lib/queries/person/autocomplete.rb', line 68
def autocomplete_alternate_values_last_name
matching_alternate_value_on(:last_name)
end
|
#autocomplete_exact_inverted ⇒ Scope
60
61
62
63
64
65
66
|
# File 'lib/queries/person/autocomplete.rb', line 60
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
53
54
55
56
57
|
# File 'lib/queries/person/autocomplete.rb', line 53
def autocomplete_exact_last_name_match
base_query.where(
table[:last_name].eq(query_string).to_sql
)
end
|
#autocomplete_exact_match ⇒ Scope
45
46
47
48
49
50
51
|
# File 'lib/queries/person/autocomplete.rb', line 45
def autocomplete_exact_match
a = normalize_name
return nil if a.nil?
base_query.where(
table[:cached].eq(normalize_name).to_sql
)
end
|
#invert_name ⇒ String
88
89
90
91
92
|
# File 'lib/queries/person/autocomplete.rb', line 88
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!!
79
80
81
82
83
|
# File 'lib/queries/person/autocomplete.rb', line 79
def normalize(string)
n = string.strip.split(/\s*\,\s*/, 2).join(', ')
return nil unless n.include?(' ')
n
end
|
#normalize_name ⇒ String
95
96
97
98
99
|
# File 'lib/queries/person/autocomplete.rb', line 95
def normalize_name
a = normalize(query_string)
return nil if a.nil?
a
end
|
#role_match ⇒ Arel::Nodes::Equatity
38
39
40
41
42
|
# File 'lib/queries/person/autocomplete.rb', line 38
def role_match
a = roles_table[:type].eq_any(role_type)
a = a.and(roles_table[:project_id].eq_any(project_id)) if in_project
a
end
|
#roles_table ⇒ Arel::Table
163
164
165
|
# File 'lib/queries/person/autocomplete.rb', line 163
def roles_table
::Role.arel_table
end
|