]> source.dussan.org Git - redmine.git/commitdiff
Cleaner way to handle the replacement of watch links (#8071).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 5 Apr 2011 12:09:15 +0000 (12:09 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 5 Apr 2011 12:09:15 +0000 (12:09 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5320 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/watchers_controller.rb
app/helpers/watchers_helper.rb
app/views/issues/_action_menu.rhtml
app/views/issues/show.rhtml
test/functional/watchers_controller_test.rb

index ad8d8be22cc127563f9b24eb7fddd9bc6845d210..7678d9ccae11ee2a14b5bc0ce752c6a13c05fa83 100644 (file)
@@ -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
index 2695ce9f63e6bc50dd17f7c3e88ab28e12e13d40..e0010d8343743bb0ab6f1298e1e02566aa87a5e1 100644 (file)
@@ -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
 # 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)
index 3f4d89e2cd0c5f99d4a086a3fd6940e45f076ab2..67eec6c7828ee604b167ef408d177f9fcb9bc652 100644 (file)
@@ -1,8 +1,7 @@
 <div class="contextual">
 <%= 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' %>
index cfe0288ffbd4dee308f1ba54b6d716bd1bbaeca6..7d187161953605d1a9bc6177408bac9bf75b79df 100644 (file)
@@ -98,7 +98,7 @@
 
 
 <div style="clear: both;"></div>
-<%= render :partial => 'action_menu', :locals => {:replace_watcher => 'watcher2' } %>
+<%= render :partial => 'action_menu' %>
 
 <div style="clear: both;"></div>
 <% if authorize_for('issues', 'edit') %>
index 41cc9fda312eb10f7e23b8686d21a5b2a60e758e..b4f1af4f3f8d7ccd6d23660ee7dfc771ee2e0f53 100644 (file)
@@ -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