##// END OF EJS Templates
Cleaner way to handle the replacement of watch links (#8071)....
Jean-Philippe Lang -
r5200:e3dae9ddbf46
parent child
Show More
@@ -1,5 +1,5
1 # redMine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
@@ -76,21 +76,13 private
76
76
77 def set_watcher(user, watching)
77 def set_watcher(user, watching)
78 @watched.set_watcher(user, watching)
78 @watched.set_watcher(user, watching)
79 if params[:replace].present?
80 if params[:replace].is_a? Array
81 replace_ids = params[:replace]
82 else
83 replace_ids = [params[:replace]]
84 end
85 else
86 replace_ids = ['watcher']
87 end
88 respond_to do |format|
79 respond_to do |format|
89 format.html { redirect_to :back }
80 format.html { redirect_to :back }
90 format.js do
81 format.js do
91 render(:update) do |page|
82 render(:update) do |page|
92 replace_ids.each do |replace_id|
83 c = watcher_css(@watched)
93 page.replace_html replace_id, watcher_link(@watched, user, :replace => replace_ids)
84 page.select(".#{c}").each do |item|
85 page.replace_html item, watcher_link(@watched, user)
94 end
86 end
95 end
87 end
96 end
88 end
@@ -1,5 +1,5
1 # redMine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
@@ -17,27 +17,17
17
17
18 module WatchersHelper
18 module WatchersHelper
19
19
20 # Valid options
20 def watcher_tag(object, user, options={})
21 # * :id - the element id
21 content_tag("span", watcher_link(object, user), :class => watcher_css(object))
22 # * :replace - a string or array of element ids that will be
23 # replaced
24 def watcher_tag(object, user, options={:replace => 'watcher'})
25 id = options[:id]
26 id ||= options[:replace] if options[:replace].is_a? String
27 content_tag("span", watcher_link(object, user, options), :id => id)
28 end
22 end
29
23
30 # Valid options
24 def watcher_link(object, user)
31 # * :replace - a string or array of element ids that will be
32 # replaced
33 def watcher_link(object, user, options={:replace => 'watcher'})
34 return '' unless user && user.logged? && object.respond_to?('watched_by?')
25 return '' unless user && user.logged? && object.respond_to?('watched_by?')
35 watched = object.watched_by?(user)
26 watched = object.watched_by?(user)
36 url = {:controller => 'watchers',
27 url = {:controller => 'watchers',
37 :action => (watched ? 'unwatch' : 'watch'),
28 :action => (watched ? 'unwatch' : 'watch'),
38 :object_type => object.class.to_s.underscore,
29 :object_type => object.class.to_s.underscore,
39 :object_id => object.id,
30 :object_id => object.id}
40 :replace => options[:replace]}
41 link_to_remote((watched ? l(:button_unwatch) : l(:button_watch)),
31 link_to_remote((watched ? l(:button_unwatch) : l(:button_watch)),
42 {:url => url},
32 {:url => url},
43 :href => url_for(url),
33 :href => url_for(url),
@@ -45,6 +35,11 module WatchersHelper
45
35
46 end
36 end
47
37
38 # Returns the css class used to identify watch links for a given +object+
39 def watcher_css(object)
40 "#{object.class.to_s.underscore}-#{object.id}-watcher"
41 end
42
48 # Returns a comma separated list of users watching the given object
43 # Returns a comma separated list of users watching the given object
49 def watchers_list(object)
44 def watchers_list(object)
50 remove_allowed = User.current.allowed_to?("delete_#{object.class.name.underscore}_watchers".to_sym, object.project)
45 remove_allowed = User.current.allowed_to?("delete_#{object.class.name.underscore}_watchers".to_sym, object.project)
@@ -1,8 +1,7
1 <div class="contextual">
1 <div class="contextual">
2 <%= 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)) %>
2 <%= 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)) %>
3 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'new', :issue_id => @issue}, :class => 'icon icon-time-add' %>
3 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'new', :issue_id => @issue}, :class => 'icon icon-time-add' %>
4 <% replace_watcher ||= 'watcher' %>
4 <%= watcher_tag(@issue, User.current) %>
5 <%= watcher_tag(@issue, User.current, {:id => replace_watcher, :replace => ['watcher','watcher2']}) %>
6 <%= link_to_if_authorized l(:button_duplicate), {:controller => 'issues', :action => 'new', :project_id => @project, :copy_from => @issue }, :class => 'icon icon-duplicate' %>
5 <%= link_to_if_authorized l(:button_duplicate), {:controller => 'issues', :action => 'new', :project_id => @project, :copy_from => @issue }, :class => 'icon icon-duplicate' %>
7 <%= link_to_if_authorized l(:button_copy), {:controller => 'issue_moves', :action => 'new', :id => @issue, :copy_options => {:copy => 't'}}, :class => 'icon icon-copy' %>
6 <%= link_to_if_authorized l(:button_copy), {:controller => 'issue_moves', :action => 'new', :id => @issue, :copy_options => {:copy => 't'}}, :class => 'icon icon-copy' %>
8 <%= link_to_if_authorized l(:button_move), {:controller => 'issue_moves', :action => 'new', :id => @issue}, :class => 'icon icon-move' %>
7 <%= link_to_if_authorized l(:button_move), {:controller => 'issue_moves', :action => 'new', :id => @issue}, :class => 'icon icon-move' %>
@@ -98,7 +98,7
98
98
99
99
100 <div style="clear: both;"></div>
100 <div style="clear: both;"></div>
101 <%= render :partial => 'action_menu', :locals => {:replace_watcher => 'watcher2' } %>
101 <%= render :partial => 'action_menu' %>
102
102
103 <div style="clear: both;"></div>
103 <div style="clear: both;"></div>
104 <% if authorize_for('issues', 'edit') %>
104 <% if authorize_for('issues', 'edit') %>
@@ -1,5 +1,5
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
@@ -43,7 +43,7 class WatchersControllerTest < ActionController::TestCase
43 assert_difference('Watcher.count') do
43 assert_difference('Watcher.count') do
44 xhr :post, :watch, :object_type => 'issue', :object_id => '1'
44 xhr :post, :watch, :object_type => 'issue', :object_id => '1'
45 assert_response :success
45 assert_response :success
46 assert_select_rjs :replace_html, 'watcher'
46 assert @response.body.include?('$$(".issue-1-watcher")')
47 end
47 end
48 assert Issue.find(1).watched_by?(User.find(3))
48 assert Issue.find(1).watched_by?(User.find(3))
49 end
49 end
@@ -57,33 +57,12 class WatchersControllerTest < ActionController::TestCase
57 end
57 end
58 end
58 end
59
59
60 def test_watch_with_multiple_replacements
61 @request.session[:user_id] = 3
62 assert_difference('Watcher.count') do
63 xhr :post, :watch, :object_type => 'issue', :object_id => '1', :replace => ['watch_item_1','watch_item_2']
64 assert_response :success
65 assert_select_rjs :replace_html, 'watch_item_1'
66 assert_select_rjs :replace_html, 'watch_item_2'
67 end
68 end
69
70 def test_unwatch
60 def test_unwatch
71 @request.session[:user_id] = 3
61 @request.session[:user_id] = 3
72 assert_difference('Watcher.count', -1) do
62 assert_difference('Watcher.count', -1) do
73 xhr :post, :unwatch, :object_type => 'issue', :object_id => '2'
63 xhr :post, :unwatch, :object_type => 'issue', :object_id => '2'
74 assert_response :success
64 assert_response :success
75 assert_select_rjs :replace_html, 'watcher'
65 assert @response.body.include?('$$(".issue-2-watcher")')
76 end
77 assert !Issue.find(1).watched_by?(User.find(3))
78 end
79
80 def test_unwatch_with_multiple_replacements
81 @request.session[:user_id] = 3
82 assert_difference('Watcher.count', -1) do
83 xhr :post, :unwatch, :object_type => 'issue', :object_id => '2', :replace => ['watch_item_1', 'watch_item_2']
84 assert_response :success
85 assert_select_rjs :replace_html, 'watch_item_1'
86 assert_select_rjs :replace_html, 'watch_item_2'
87 end
66 end
88 assert !Issue.find(1).watched_by?(User.find(3))
67 assert !Issue.find(1).watched_by?(User.find(3))
89 end
68 end
General Comments 0
You need to be logged in to leave comments. Login now