Commit 3758f751 authored by Sean McGivern's avatar Sean McGivern

Allow skipping users in autocomplete

Pass an array of user IDs in the `skip_users` param to have them
excluded from the results (unless they are explicitly included through
the `current_user` or `author_id` params).
parent 8bdf633f
......@@ -213,6 +213,7 @@ class @UsersSelect
@showCurrentUser = $(select).data('current-user')
@pushCodeToProtectedBranches = $(select).data('push-code-to-protected-branches')
@authorId = $(select).data('author-id')
@skipUsers = $(select).data('skip-users')
showNullUser = $(select).data('null-user')
showAnyUser = $(select).data('any-user')
showEmailUser = $(select).data('email-user')
......@@ -321,6 +322,7 @@ class @UsersSelect
current_user: @showCurrentUser
push_code_to_protected_branches: @pushCodeToProtectedBranches
author_id: @authorId
skip_users: @skipUsers
dataType: "json"
).done (users) ->
callback(users)
......
......@@ -5,6 +5,7 @@ class AutocompleteController < ApplicationController
def users
@users = @users.non_ldap if params[:skip_ldap] == 'true'
@users = @users.search(params[:search]) if params[:search].present?
@users = @users.where.not(id: params[:skip_users]) if params[:skip_users].present?
@users = @users.active
@users = @users.reorder(:name)
......
......@@ -5,21 +5,9 @@ module SelectsHelper
css_class << "skip_ldap " if opts[:skip_ldap]
css_class << (opts[:class] || '')
value = opts[:selected] || ''
first_user = opts[:first_user] && current_user ? current_user.username : false
html = {
class: css_class,
data: {
placeholder: opts[:placeholder] || 'Search for a user',
null_user: opts[:null_user] || false,
any_user: opts[:any_user] || false,
email_user: opts[:email_user] || false,
first_user: first_user,
current_user: opts[:current_user] || false,
"push-code-to-protected-branches" => opts[:push_code_to_protected_branches],
author_id: opts[:author_id] || ''
}
data: users_select_data_attributes(opts)
}
unless opts[:scope] == :all
......@@ -85,4 +73,20 @@ module SelectsHelper
hidden_field_tag(id, value, class: css_class)
end
private
def users_select_data_attributes(opts)
{
placeholder: opts[:placeholder] || 'Search for a user',
null_user: opts[:null_user] || false,
any_user: opts[:any_user] || false,
email_user: opts[:email_user] || false,
first_user: opts[:first_user] && current_user ? current_user.username : false,
current_user: opts[:current_user] || false,
"push-code-to-protected-branches" => opts[:push_code_to_protected_branches],
author_id: opts[:author_id] || '',
skip_users: opts[:skip_users] ? opts[:skip_users].map(&:id) : nil,
}
end
end
......@@ -176,4 +176,18 @@ describe AutocompleteController do
expect(body.collect { |u| u['id'] }).not_to include(99999)
end
end
context 'skip_users parameter included' do
before { sign_in(user) }
let(:response_user_ids) { JSON.parse(response.body).map { |user| user['id'] } }
it 'skips the user IDs passed' do
get(:users, skip_users: [user, user2].map(&:id))
expect(response_user_ids).not_to include(user.id)
expect(response_user_ids).not_to include(user2.id)
expect(response_user_ids).not_to be_empty
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment