diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index 3f080e96b6..ba64e33e41 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -120,6 +120,9 @@ class WatchersController < ApplicationController scope = nil if params[:q].blank? && @project.present? scope = @project.users + elsif params[:q].blank? && @projects.present? + user_ids = @projects.map { |p| p.users.map(&:id) }.flatten.uniq + scope = User.where(:id => user_ids) else scope = User.all.limit(100) end diff --git a/app/views/watchers/_new.html.erb b/app/views/watchers/_new.html.erb index 3bd7953ac1..e6cb1ca469 100644 --- a/app/views/watchers/_new.html.erb +++ b/app/views/watchers/_new.html.erb @@ -17,7 +17,7 @@ <%= javascript_tag "observeSearchfield('user_search', 'users_for_watcher', '#{ escape_javascript url_for(:controller => 'watchers', :action => 'autocomplete_for_user', :object_type => (watchables.present? ? watchables.first.class.name.underscore : nil), - :object_id => (watchables.present? && watchables.size == 1 ? watchables.first.id : nil), + :object_id => (watchables.present? ? watchables.pluck(:id) : nil), :project_id => @project) }')" %>
diff --git a/test/functional/watchers_controller_test.rb b/test/functional/watchers_controller_test.rb index fb96e99b1e..06dc5f7f10 100644 --- a/test/functional/watchers_controller_test.rb +++ b/test/functional/watchers_controller_test.rb @@ -244,6 +244,36 @@ class WatchersControllerTest < Redmine::ControllerTest assert_select 'input[name=?][value="9"]', 'watcher[user_ids][]' end + def test_autocomplete_for_user_without_keyword_with_multiple_watchables + @request.session[:user_id] = 2 + User.find(2).roles.each { |role| role.add_permission! :add_issue_watchers } + + get :autocomplete_for_user, :params => {:object_type => 'issue', :object_id => [1, 4]}, :xhr => true + + # Return the users of the project to which object_id belongs. + # issue 1: eCookbook, issue 4: OnlineStore + assert_response :success + assert_select 'input', :count => 3 + assert_select 'input[name=?][value="2"]', 'watcher[user_ids][]' + assert_select 'input[name=?][value="3"]', 'watcher[user_ids][]' + assert_select 'input[name=?][value="8"]', 'watcher[user_ids][]' + end + + def test_autocomplete_for_user_with_keyword_and_multiple_watchables + @request.session[:user_id] = 2 + User.find(2).roles.each { |role| role.add_permission! :add_issue_watchers } + + get :autocomplete_for_user, :params => {:object_type => 'issue', :object_id => [1, 4], :q => 'mi'}, :xhr => true + + # Ignore the project and return users matching "mi". + assert_response :success + assert_select 'input', :count => 4 + assert_select 'input[name=?][value="1"]', 'watcher[user_ids][]' + assert_select 'input[name=?][value="2"]', 'watcher[user_ids][]' + assert_select 'input[name=?][value="8"]', 'watcher[user_ids][]' + assert_select 'input[name=?][value="9"]', 'watcher[user_ids][]' + end + def test_search_non_member_on_create @request.session[:user_id] = 2 project = Project.find_by_name("ecookbook")