Skip to content

Commit 944c4ec

Browse files
authored
Merge pull request #1334 from ElixirTeSS/fix-person-autocomplete
Also query profiles in people autocomplete
2 parents 5d75192 + f81bda9 commit 944c4ec

4 files changed

Lines changed: 90 additions & 10 deletions

File tree

app/controllers/autocomplete_controller.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,31 @@ def suggestions
77
end
88

99
def people_suggestions
10-
people = Person.query(params[:query])
11-
suggestions = people.map do |p|
12-
{ value: p.name,
13-
data: {
14-
orcid: p.orcid,
15-
profile_id: p.profile_id
10+
people = Person.query(params[:query], 20)
11+
profiles = Profile.query(params[:query], 20)
12+
unique_map = {}
13+
people.each do |p|
14+
unique_map[p.profile_id || p.orcid || p.name] =
15+
{ value: p.name,
16+
data: {
17+
orcid: p.orcid,
18+
profile_id: p.profile_id
19+
}
1620
}
17-
}
1821
end
22+
profiles.each do |p|
23+
orcid = p.orcid_authenticated? ? p.orcid : nil
24+
unique_map.delete(orcid) if orcid
25+
unique_map[p.id] =
26+
{ value: p.full_name,
27+
data: {
28+
orcid: orcid,
29+
profile_id: p.id
30+
}
31+
}
32+
end
33+
34+
suggestions = unique_map.values.sort_by { |s| [s[:value].downcase, s[:data][:orcid] || 'z'] }
1935
respond_with({ suggestions: suggestions })
2036
end
2137
end

app/models/profile.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ def authenticate_orcid(orcid)
6262
out
6363
end
6464

65+
def self.visible
66+
joins(:user).merge(User.visible)
67+
end
68+
69+
# For autocomplete
70+
def self.starting_with(query)
71+
where('lower(firstname) LIKE ?', "#{query.downcase}%").or(where('lower(surname) LIKE ?', "#{query.downcase}%"))
72+
end
73+
74+
def self.query(query, limit = nil)
75+
q = visible.where.not(orcid: nil).where(orcid_authenticated: true)
76+
.select(:id, :firstname, :surname, :orcid, :orcid_authenticated)
77+
.starting_with(query).distinct
78+
q = q.limit(limit) if limit
79+
q.order(firstname: :asc, surname: :asc, orcid: :asc)
80+
end
81+
6582
private
6683

6784
def check_public

test/controllers/autocomplete_controller_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ class AutocompleteControllerTest < ActionController::TestCase
5151
res = JSON.parse(response.body)
5252
suggestions = res['suggestions']
5353
assert_equal 3, suggestions.length, "Should be 3 - 2 with ORCIDs and 1 without. Should not include duplicates."
54-
assert_equal ['0000-0002-1694-233X', '0000-0002-1825-0097', nil], suggestions.map { |s| s['data']['orcid'] }
55-
assert_equal ['John Doe', 'John Doe', 'John Doe'], suggestions.map { |s| s['value'] }
54+
assert_equal ['0000-0002-1694-233X', nil, '0000-0002-1825-0097'], suggestions.map { |s| s['data']['orcid'] }
55+
assert_equal ['John Doe', 'John Doe', 'Josiah Carberry'], suggestions.map { |s| s['value'] }, "Should use Josiah Carberry's name from his profile rather than John Doe from the author"
5656

5757
get :people_suggestions, params: { query: 'j' }, format: :json
5858
assert_response :success
5959
res = JSON.parse(response.body)
6060
suggestions = res['suggestions']
61-
assert_equal ['jane Doe', 'John Doe', 'John Doe', 'John Doe'], suggestions.map { |s| s['value'] }
61+
assert_equal ['jane Doe', 'John Doe', 'John Doe', 'Josiah Carberry'], suggestions.map { |s| s['value'] }
6262

6363
get :people_suggestions, params: { query: 'FRED' }, format: :json
6464
assert_response :success

test/models/profile_test.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,51 @@ class ProfileTest < ActiveSupport::TestCase
154154
refute Profile.new.authenticate_orcid('0009-0006-0987-5702')
155155
end
156156
end
157+
158+
test 'visible' do
159+
visible = Profile.visible
160+
assert_includes visible, profiles(:one)
161+
assert_includes visible, profiles(:two)
162+
refute_includes visible, profiles(:basic_user_profile)
163+
refute_includes visible, profiles(:banned_user_profile)
164+
end
165+
166+
test 'starting_with' do
167+
regi = Profile.starting_with('regi')
168+
assert_includes regi, profiles(:one)
169+
refute_includes regi, profiles(:two)
170+
171+
user = Profile.starting_with('user')
172+
assert_includes user, profiles(:one)
173+
refute_includes user, profiles(:two)
174+
175+
bla = Profile.starting_with('bla')
176+
refute_includes bla, profiles(:one)
177+
refute_includes bla, profiles(:two)
178+
179+
ad = Profile.starting_with('ad')
180+
refute_includes ad, profiles(:two)
181+
assert_includes ad, profiles(:three)
182+
assert_includes ad, profiles(:admin_trainer_profile)
183+
end
184+
185+
test 'query' do
186+
josi = Profile.query('josi')
187+
assert_equal 1, josi.length
188+
assert_includes josi, profiles(:trainer_one_profile).becomes(Profile) # Need to cast back to Profile because it is a Trainer otherwise
189+
190+
# Excludes unauthenticated orcids
191+
unauth = profiles(:trainer_two_profile)
192+
assert_includes Profile.starting_with('luci'), unauth
193+
unauth_q = Profile.query('luci')
194+
assert_equal 0, unauth_q.length
195+
196+
# Excludes non-visible:
197+
banned = profiles(:banned_user_profile)
198+
banned.update!(firstname: 'Banned')
199+
assert banned.authenticate_orcid('0009-0006-0987-5702')
200+
assert_includes Profile.starting_with('banned'), banned
201+
banned_q = Profile.query('Banned')
202+
assert_equal 0, banned_q.length
203+
end
157204
end

0 commit comments

Comments
 (0)