From 8160cd02bdd96f1169be3c31ce9380f29d70f392 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Fri, 3 Dec 2010 14:43:03 -0800 Subject: [PATCH] [#800 #801] Add Javascript search to bulk add watchers to issues. --- app/controllers/watchers_controller.rb | 9 ++++--- app/helpers/application_helper.rb | 2 +- app/views/watchers/_watchers.rhtml | 27 +++++++++++++-------- test/functional/watchers_controller_test.rb | 13 +++++++++- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index e9479f43..5d5d2d50 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -34,9 +34,12 @@ class WatchersController < ApplicationController end def new - @watcher = Watcher.new(params[:watcher]) - @watcher.watchable = @watched - @watcher.save if request.post? + params[:user_ids].each do |user_id| + @watcher = Watcher.new((params[:watcher] || {}).merge({:user_id => user_id})) + @watcher.watchable = @watched + @watcher.save if request.post? + end if params[:user_ids].present? + respond_to do |format| format.html { redirect_to :back } format.js do diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 83007e15..f4396b56 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -285,7 +285,7 @@ module ApplicationHelper def principals_check_box_tags(name, principals) s = '' principals.sort.each do |principal| - s << "\n" + s << "\n" end s end diff --git a/app/views/watchers/_watchers.rhtml b/app/views/watchers/_watchers.rhtml index 4da8c0dd..f05e551d 100644 --- a/app/views/watchers/_watchers.rhtml +++ b/app/views/watchers/_watchers.rhtml @@ -1,24 +1,31 @@
-<%= link_to_remote l(:button_add), - :url => {:controller => 'watchers', - :action => 'new', - :object_type => watched.class.name.underscore, - :object_id => watched} if User.current.allowed_to?(:add_issue_watchers, @project) %> +<%= link_to_function(l(:button_add), "$('new-watcher-form').toggle();") if User.current.allowed_to?(:add_issue_watchers, @project) %>

<%= l(:label_issue_watchers) %> (<%= watched.watcher_users.size %>)

-<% unless @watcher.nil? %> - <% remote_form_for(:watcher, @watcher, +<% if User.current.allowed_to?(:add_issue_watchers, @project) %> + <% remote_form_for(:watcher, @watcher, :url => {:controller => 'watchers', :action => 'new', :object_type => watched.class.name.underscore, :object_id => watched}, :method => :post, - :html => {:id => 'new-watcher-form'}) do |f| %> -

<%= f.select :user_id, (watched.addable_watcher_users.collect {|m| [m.name, m.id]}), :prompt => "--- #{l(:actionview_instancetag_blank_option)} ---" %> + :html => {:id => 'new-watcher-form', :style => 'display:none;'}) do |f| %> + <% users = User.active.find(:all, :limit => 10) %> +

<%= label_tag "user_search", l(:label_user_search) %><%= text_field_tag 'user_search', nil, :style => "width:98%;" %>

+ <%= observe_field(:user_search, + :frequency => 0.5, + :update => :users, + :url => auto_complete_users_path, + :with => 'q') + %> + +
+ <%= principals_check_box_tags 'user_ids[]', users %> +
- <%= submit_tag l(:button_add) %> +

<%= submit_tag l(:button_add) %> <%= toggle_link l(:button_cancel), 'new-watcher-form'%>

<% end %> <% end %> diff --git a/test/functional/watchers_controller_test.rb b/test/functional/watchers_controller_test.rb index 7023908f..858e807e 100644 --- a/test/functional/watchers_controller_test.rb +++ b/test/functional/watchers_controller_test.rb @@ -114,13 +114,24 @@ class WatchersControllerTest < ActionController::TestCase def test_new_watcher @request.session[:user_id] = 2 assert_difference('Watcher.count') do - xhr :post, :new, :object_type => 'issue', :object_id => '2', :watcher => {:user_id => '4'} + xhr :post, :new, :object_type => 'issue', :object_id => '2', :user_ids => ['4'] assert_response :success assert_select_rjs :replace_html, 'watchers' end assert Issue.find(2).watched_by?(User.find(4)) end + def test_new_multiple_users + @request.session[:user_id] = 2 + assert_difference('Watcher.count', 2) do + xhr :post, :new, :object_type => 'issue', :object_id => '2', :user_ids => ['4','7'] + assert_response :success + assert_select_rjs :replace_html, 'watchers' + end + assert Issue.find(2).watched_by?(User.find(4)) + assert Issue.find(2).watched_by?(User.find(7)) + end + def test_remove_watcher @request.session[:user_id] = 2 assert_difference('Watcher.count', -1) do