From e3dae9ddbf46353df67a1cc155de6b05a58bfa35 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Tue, 5 Apr 2011 12:09:15 +0000 Subject: [PATCH] Cleaner way to handle the replacement of watch links (#8071). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5320 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/watchers_controller.rb | 18 ++++--------- app/helpers/watchers_helper.rb | 29 +++++++++------------ app/views/issues/_action_menu.rhtml | 3 +-- app/views/issues/show.rhtml | 2 +- test/functional/watchers_controller_test.rb | 27 +++---------------- 5 files changed, 22 insertions(+), 57 deletions(-) diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index ad8d8be22..7678d9cca 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -1,5 +1,5 @@ -# redMine - project management software -# Copyright (C) 2006-2007 Jean-Philippe Lang +# Redmine - project management software +# Copyright (C) 2006-2011 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -76,21 +76,13 @@ private def set_watcher(user, watching) @watched.set_watcher(user, watching) - if params[:replace].present? - if params[:replace].is_a? Array - replace_ids = params[:replace] - else - replace_ids = [params[:replace]] - end - else - replace_ids = ['watcher'] - end respond_to do |format| format.html { redirect_to :back } format.js do render(:update) do |page| - replace_ids.each do |replace_id| - page.replace_html replace_id, watcher_link(@watched, user, :replace => replace_ids) + c = watcher_css(@watched) + page.select(".#{c}").each do |item| + page.replace_html item, watcher_link(@watched, user) end end end diff --git a/app/helpers/watchers_helper.rb b/app/helpers/watchers_helper.rb index 2695ce9f6..e0010d834 100644 --- a/app/helpers/watchers_helper.rb +++ b/app/helpers/watchers_helper.rb @@ -1,5 +1,5 @@ -# redMine - project management software -# Copyright (C) 2006-2007 Jean-Philippe Lang +# Redmine - project management software +# Copyright (C) 2006-2011 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -16,28 +16,18 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. module WatchersHelper - - # Valid options - # * :id - the element id - # * :replace - a string or array of element ids that will be - # replaced - def watcher_tag(object, user, options={:replace => 'watcher'}) - id = options[:id] - id ||= options[:replace] if options[:replace].is_a? String - content_tag("span", watcher_link(object, user, options), :id => id) + + def watcher_tag(object, user, options={}) + content_tag("span", watcher_link(object, user), :class => watcher_css(object)) end - # Valid options - # * :replace - a string or array of element ids that will be - # replaced - def watcher_link(object, user, options={:replace => 'watcher'}) + def watcher_link(object, user) return '' unless user && user.logged? && object.respond_to?('watched_by?') watched = object.watched_by?(user) url = {:controller => 'watchers', :action => (watched ? 'unwatch' : 'watch'), :object_type => object.class.to_s.underscore, - :object_id => object.id, - :replace => options[:replace]} + :object_id => object.id} link_to_remote((watched ? l(:button_unwatch) : l(:button_watch)), {:url => url}, :href => url_for(url), @@ -45,6 +35,11 @@ module WatchersHelper end + # Returns the css class used to identify watch links for a given +object+ + def watcher_css(object) + "#{object.class.to_s.underscore}-#{object.id}-watcher" + end + # Returns a comma separated list of users watching the given object def watchers_list(object) remove_allowed = User.current.allowed_to?("delete_#{object.class.name.underscore}_watchers".to_sym, object.project) diff --git a/app/views/issues/_action_menu.rhtml b/app/views/issues/_action_menu.rhtml index 3f4d89e2c..67eec6c78 100644 --- a/app/views/issues/_action_menu.rhtml +++ b/app/views/issues/_action_menu.rhtml @@ -1,8 +1,7 @@
<%= link_to_if_authorized(l(:button_update), {:controller => 'issues', :action => 'edit', :id => @issue }, :onclick => 'showAndScrollTo("update", "notes"); return false;', :class => 'icon icon-edit', :accesskey => accesskey(:edit)) %> <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'new', :issue_id => @issue}, :class => 'icon icon-time-add' %> -<% replace_watcher ||= 'watcher' %> -<%= watcher_tag(@issue, User.current, {:id => replace_watcher, :replace => ['watcher','watcher2']}) %> +<%= watcher_tag(@issue, User.current) %> <%= link_to_if_authorized l(:button_duplicate), {:controller => 'issues', :action => 'new', :project_id => @project, :copy_from => @issue }, :class => 'icon icon-duplicate' %> <%= link_to_if_authorized l(:button_copy), {:controller => 'issue_moves', :action => 'new', :id => @issue, :copy_options => {:copy => 't'}}, :class => 'icon icon-copy' %> <%= link_to_if_authorized l(:button_move), {:controller => 'issue_moves', :action => 'new', :id => @issue}, :class => 'icon icon-move' %> diff --git a/app/views/issues/show.rhtml b/app/views/issues/show.rhtml index cfe0288ff..7d1871619 100644 --- a/app/views/issues/show.rhtml +++ b/app/views/issues/show.rhtml @@ -98,7 +98,7 @@
-<%= render :partial => 'action_menu', :locals => {:replace_watcher => 'watcher2' } %> +<%= render :partial => 'action_menu' %>
<% if authorize_for('issues', 'edit') %> diff --git a/test/functional/watchers_controller_test.rb b/test/functional/watchers_controller_test.rb index 41cc9fda3..b4f1af4f3 100644 --- a/test/functional/watchers_controller_test.rb +++ b/test/functional/watchers_controller_test.rb @@ -1,5 +1,5 @@ # Redmine - project management software -# Copyright (C) 2006-2008 Jean-Philippe Lang +# Copyright (C) 2006-2011 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -43,7 +43,7 @@ class WatchersControllerTest < ActionController::TestCase assert_difference('Watcher.count') do xhr :post, :watch, :object_type => 'issue', :object_id => '1' assert_response :success - assert_select_rjs :replace_html, 'watcher' + assert @response.body.include?('$$(".issue-1-watcher")') end assert Issue.find(1).watched_by?(User.find(3)) end @@ -56,34 +56,13 @@ class WatchersControllerTest < ActionController::TestCase assert_response 403 end end - - def test_watch_with_multiple_replacements - @request.session[:user_id] = 3 - assert_difference('Watcher.count') do - xhr :post, :watch, :object_type => 'issue', :object_id => '1', :replace => ['watch_item_1','watch_item_2'] - assert_response :success - assert_select_rjs :replace_html, 'watch_item_1' - assert_select_rjs :replace_html, 'watch_item_2' - end - end def test_unwatch @request.session[:user_id] = 3 assert_difference('Watcher.count', -1) do xhr :post, :unwatch, :object_type => 'issue', :object_id => '2' assert_response :success - assert_select_rjs :replace_html, 'watcher' - end - assert !Issue.find(1).watched_by?(User.find(3)) - end - - def test_unwatch_with_multiple_replacements - @request.session[:user_id] = 3 - assert_difference('Watcher.count', -1) do - xhr :post, :unwatch, :object_type => 'issue', :object_id => '2', :replace => ['watch_item_1', 'watch_item_2'] - assert_response :success - assert_select_rjs :replace_html, 'watch_item_1' - assert_select_rjs :replace_html, 'watch_item_2' + assert @response.body.include?('$$(".issue-2-watcher")') end assert !Issue.find(1).watched_by?(User.find(3)) end -- 2.39.5