summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McGivern <sean@gitlab.com>2016-07-13 15:38:21 +0100
committerSean McGivern <sean@gitlab.com>2016-07-27 14:10:49 +0100
commitdc4de2d8e2a42640cc24816fd28112c2f2d0bc91 (patch)
tree31b0b71bbf9cc0130fb3f7577af3d6fc9df8c152
parentaed644ccc084223cb1f9c5d955569c5ff9eeaf88 (diff)
downloadgitlab-ce-dc4de2d8e2a42640cc24816fd28112c2f2d0bc91.tar.gz
Allow skipping users in autocompleteallow-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).
-rw-r--r--app/assets/javascripts/users_select.js4
-rw-r--r--app/controllers/autocomplete_controller.rb1
-rw-r--r--app/helpers/selects_helper.rb30
-rw-r--r--spec/controllers/autocomplete_controller_spec.rb13
4 files changed, 34 insertions, 14 deletions
diff --git a/app/assets/javascripts/users_select.js b/app/assets/javascripts/users_select.js
index 64a29d36cdf..4af2a214e12 100644
--- a/app/assets/javascripts/users_select.js
+++ b/app/assets/javascripts/users_select.js
@@ -189,6 +189,7 @@
_this.groupId = $(select).data('group-id');
_this.showCurrentUser = $(select).data('current-user');
_this.authorId = $(select).data('author-id');
+ _this.skipUsers = $(select).data('skip-users');
showNullUser = $(select).data('null-user');
showAnyUser = $(select).data('any-user');
showEmailUser = $(select).data('email-user');
@@ -320,7 +321,8 @@
project_id: this.projectId,
group_id: this.groupId,
current_user: this.showCurrentUser,
- author_id: this.authorId
+ author_id: this.authorId,
+ skip_users: this.skipUsers
},
dataType: "json"
}).done(function(users) {
diff --git a/app/controllers/autocomplete_controller.rb b/app/controllers/autocomplete_controller.rb
index c89678cf2d8..d828d163c28 100644
--- a/app/controllers/autocomplete_controller.rb
+++ b/app/controllers/autocomplete_controller.rb
@@ -5,6 +5,7 @@ class AutocompleteController < ApplicationController
def users
@users ||= User.none
@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)
@users = @users.page(params[:page])
diff --git a/app/helpers/selects_helper.rb b/app/helpers/selects_helper.rb
index bb395e37884..5f27e33c6ad 100644
--- a/app/helpers/selects_helper.rb
+++ b/app/helpers/selects_helper.rb
@@ -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
@@ -68,4 +56,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
diff --git a/spec/controllers/autocomplete_controller_spec.rb b/spec/controllers/autocomplete_controller_spec.rb
index 60c654f622d..ed0b7f9e240 100644
--- a/spec/controllers/autocomplete_controller_spec.rb
+++ b/spec/controllers/autocomplete_controller_spec.rb
@@ -163,4 +163,17 @@ 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) }
+
+ it 'skips the user IDs passed' do
+ get(:users, skip_users: [user, user2].map(&:id))
+
+ other_user_ids = [non_member, project.owner, project.creator].map(&:id)
+ response_user_ids = JSON.parse(response.body).map { |user| user['id'] }
+
+ expect(response_user_ids).to contain_exactly(*other_user_ids)
+ end
+ end
end