@@ -0,0 +1,2 | |||
|
1 | <% selector = ".#{watcher_css(watched)}" %> | |
|
2 | $$("<%= selector %>").each(function(el){el.update("<%= escape_javascript watcher_link(watched, user) %>")}); |
@@ -0,0 +1,4 | |||
|
1 | <% @users.each do |user| %> | |
|
2 | $$("#issue_watcher_user_ids_<%= user.id %>").each(function(el){el.remove();}); | |
|
3 | <% end %> | |
|
4 | Element.insert('watchers_inputs', '<%= escape_javascript(watchers_checkboxes(nil, @users, true)) %>'); |
@@ -0,0 +1,2 | |||
|
1 | Element.update('ajax-modal', '<%= escape_javascript(render(:partial => 'watchers/new', :locals => {:watched => @watched})) %>'); | |
|
2 | Element.update('watchers', '<%= escape_javascript(render(:partial => 'watchers/watchers', :locals => {:watched => @watched})) %>'); |
@@ -0,0 +1,1 | |||
|
1 | Element.update('watchers', '<%= escape_javascript(render(:partial => 'watchers/watchers', :locals => {:watched => @watched})) %>'); |
@@ -0,0 +1,3 | |||
|
1 | Element.update('ajax-modal', '<%= escape_javascript(render :partial => 'watchers/new', :locals => {:watched => @watched}) %>'); | |
|
2 | showModal('ajax-modal', '400px'); | |
|
3 | $('ajax-modal').addClassName('new-watcher'); |
@@ -1,128 +1,95 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class WatchersController < ApplicationController |
|
19 | 19 | before_filter :find_project |
|
20 | 20 | before_filter :require_login, :check_project_privacy, :only => [:watch, :unwatch] |
|
21 | 21 | before_filter :authorize, :only => [:new, :destroy] |
|
22 | 22 | |
|
23 | 23 | def watch |
|
24 | 24 | if @watched.respond_to?(:visible?) && !@watched.visible?(User.current) |
|
25 | 25 | render_403 |
|
26 | 26 | else |
|
27 | 27 | set_watcher(User.current, true) |
|
28 | 28 | end |
|
29 | 29 | end |
|
30 | 30 | |
|
31 | 31 | def unwatch |
|
32 | 32 | set_watcher(User.current, false) |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def new |
|
36 | respond_to do |format| | |
|
37 | format.js do | |
|
38 | render :update do |page| | |
|
39 | page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched} | |
|
40 | page << "showModal('ajax-modal', '400px');" | |
|
41 | page << "$('ajax-modal').addClassName('new-watcher');" | |
|
42 | end | |
|
43 | end | |
|
44 | end | |
|
45 | 36 | end |
|
46 | 37 | |
|
47 | 38 | def create |
|
48 | 39 | if params[:watcher].is_a?(Hash) && request.post? |
|
49 | 40 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] |
|
50 | 41 | user_ids.each do |user_id| |
|
51 | 42 | Watcher.create(:watchable => @watched, :user_id => user_id) |
|
52 | 43 | end |
|
53 | 44 | end |
|
54 | 45 | respond_to do |format| |
|
55 | 46 | format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} |
|
56 |
format.js |
|
|
57 | render :update do |page| | |
|
58 | page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched} | |
|
59 | page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} | |
|
60 | end | |
|
61 | end | |
|
47 | format.js | |
|
62 | 48 | end |
|
63 | 49 | end |
|
64 | 50 | |
|
65 | 51 | def append |
|
66 | 52 | if params[:watcher].is_a?(Hash) |
|
67 | 53 | user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] |
|
68 | users = User.active.find_all_by_id(user_ids) | |
|
69 | respond_to do |format| | |
|
70 | format.js do | |
|
71 | render :update do |page| | |
|
72 | users.each do |user| | |
|
73 | page << %|$$("#issue_watcher_user_ids_#{user.id}").each(function(el){el.remove();});| | |
|
74 | end | |
|
75 | page.insert_html :bottom, 'watchers_inputs', :text => watchers_checkboxes(nil, users, true) | |
|
76 | end | |
|
77 | end | |
|
78 | end | |
|
54 | @users = User.active.find_all_by_id(user_ids) | |
|
79 | 55 | end |
|
80 | 56 | end |
|
81 | 57 | |
|
82 | 58 | def destroy |
|
83 | 59 | @watched.set_watcher(User.find(params[:user_id]), false) if request.post? |
|
84 | 60 | respond_to do |format| |
|
85 | 61 | format.html { redirect_to :back } |
|
86 |
format.js |
|
|
87 | render :update do |page| | |
|
88 | page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} | |
|
89 | end | |
|
90 | end | |
|
62 | format.js | |
|
91 | 63 | end |
|
92 | 64 | end |
|
93 | 65 | |
|
94 | 66 | def autocomplete_for_user |
|
95 | 67 | @users = User.active.like(params[:q]).find(:all, :limit => 100) |
|
96 | 68 | if @watched |
|
97 | 69 | @users -= @watched.watcher_users |
|
98 | 70 | end |
|
99 | 71 | render :layout => false |
|
100 | 72 | end |
|
101 | 73 | |
|
102 | 74 | private |
|
103 | 75 | def find_project |
|
104 | 76 | if params[:object_type] && params[:object_id] |
|
105 | 77 | klass = Object.const_get(params[:object_type].camelcase) |
|
106 | 78 | return false unless klass.respond_to?('watched_by') |
|
107 | 79 | @watched = klass.find(params[:object_id]) |
|
108 | 80 | @project = @watched.project |
|
109 | 81 | elsif params[:project_id] |
|
110 | 82 | @project = Project.visible.find_by_param(params[:project_id]) |
|
111 | 83 | end |
|
112 | 84 | rescue |
|
113 | 85 | render_404 |
|
114 | 86 | end |
|
115 | 87 | |
|
116 | 88 | def set_watcher(user, watching) |
|
117 | 89 | @watched.set_watcher(user, watching) |
|
118 | 90 | respond_to do |format| |
|
119 | 91 | format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} |
|
120 | format.js do | |
|
121 | render(:update) do |page| | |
|
122 | c = watcher_css(@watched) | |
|
123 | page << %|$$(".#{c}").each(function(el){el.innerHTML="#{escape_javascript watcher_link(@watched, user)}"});| | |
|
124 | end | |
|
125 | end | |
|
92 | format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => @watched} } | |
|
126 | 93 | end |
|
127 | 94 | end |
|
128 | 95 | end |
@@ -1,167 +1,165 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | require 'watchers_controller' |
|
20 | 20 | |
|
21 | 21 | # Re-raise errors caught by the controller. |
|
22 | 22 | class WatchersController; def rescue_action(e) raise e end; end |
|
23 | 23 | |
|
24 | 24 | class WatchersControllerTest < ActionController::TestCase |
|
25 | 25 | fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, |
|
26 | 26 | :issues, :trackers, :projects_trackers, :issue_statuses, :enumerations, :watchers |
|
27 | 27 | |
|
28 | 28 | def setup |
|
29 | 29 | @controller = WatchersController.new |
|
30 | 30 | @request = ActionController::TestRequest.new |
|
31 | 31 | @response = ActionController::TestResponse.new |
|
32 | 32 | User.current = nil |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def test_watch |
|
36 | 36 | @request.session[:user_id] = 3 |
|
37 | 37 | assert_difference('Watcher.count') do |
|
38 | 38 | xhr :post, :watch, :object_type => 'issue', :object_id => '1' |
|
39 | 39 | assert_response :success |
|
40 | 40 | assert @response.body.include?('$$(".issue-1-watcher")') |
|
41 | 41 | end |
|
42 | 42 | assert Issue.find(1).watched_by?(User.find(3)) |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def test_watch_should_be_denied_without_permission |
|
46 | 46 | Role.find(2).remove_permission! :view_issues |
|
47 | 47 | @request.session[:user_id] = 3 |
|
48 | 48 | assert_no_difference('Watcher.count') do |
|
49 | 49 | xhr :post, :watch, :object_type => 'issue', :object_id => '1' |
|
50 | 50 | assert_response 403 |
|
51 | 51 | end |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | def test_watch_invalid_class_should_respond_with_404 |
|
55 | 55 | @request.session[:user_id] = 3 |
|
56 | 56 | assert_no_difference('Watcher.count') do |
|
57 | 57 | xhr :post, :watch, :object_type => 'foo', :object_id => '1' |
|
58 | 58 | assert_response 404 |
|
59 | 59 | end |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | def test_watch_invalid_object_should_respond_with_404 |
|
63 | 63 | @request.session[:user_id] = 3 |
|
64 | 64 | assert_no_difference('Watcher.count') do |
|
65 | 65 | xhr :post, :watch, :object_type => 'issue', :object_id => '999' |
|
66 | 66 | assert_response 404 |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | def test_unwatch |
|
71 | 71 | @request.session[:user_id] = 3 |
|
72 | 72 | assert_difference('Watcher.count', -1) do |
|
73 | 73 | xhr :post, :unwatch, :object_type => 'issue', :object_id => '2' |
|
74 | 74 | assert_response :success |
|
75 | 75 | assert @response.body.include?('$$(".issue-2-watcher")') |
|
76 | 76 | end |
|
77 | 77 | assert !Issue.find(1).watched_by?(User.find(3)) |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def test_new |
|
81 | 81 | @request.session[:user_id] = 2 |
|
82 | 82 | xhr :get, :new, :object_type => 'issue', :object_id => '2' |
|
83 | 83 | assert_response :success |
|
84 | assert_select_rjs :replace_html, 'ajax-modal' | |
|
84 | assert_match /ajax-modal/, response.body | |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | def test_new_for_new_record_with_id |
|
88 | 88 | @request.session[:user_id] = 2 |
|
89 | 89 | xhr :get, :new, :project_id => 1 |
|
90 | 90 | assert_response :success |
|
91 | 91 | assert_equal Project.find(1), assigns(:project) |
|
92 | assert_select_rjs :replace_html, 'ajax-modal' | |
|
92 | assert_match /ajax-modal/, response.body | |
|
93 | 93 | end |
|
94 | 94 | |
|
95 | 95 | def test_new_for_new_record_with_identifier |
|
96 | 96 | @request.session[:user_id] = 2 |
|
97 | 97 | xhr :get, :new, :project_id => 'ecookbook' |
|
98 | 98 | assert_response :success |
|
99 | 99 | assert_equal Project.find(1), assigns(:project) |
|
100 | assert_select_rjs :replace_html, 'ajax-modal' | |
|
100 | assert_match /ajax-modal/, response.body | |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def test_create |
|
104 | 104 | @request.session[:user_id] = 2 |
|
105 | 105 | assert_difference('Watcher.count') do |
|
106 | 106 | xhr :post, :create, :object_type => 'issue', :object_id => '2', :watcher => {:user_id => '4'} |
|
107 | 107 | assert_response :success |
|
108 | assert_select_rjs :replace_html, 'watchers' | |
|
109 | assert_select_rjs :replace_html, 'ajax-modal' | |
|
108 | assert_match /watchers/, response.body | |
|
109 | assert_match /ajax-modal/, response.body | |
|
110 | 110 | end |
|
111 | 111 | assert Issue.find(2).watched_by?(User.find(4)) |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | def test_create_multiple |
|
115 | 115 | @request.session[:user_id] = 2 |
|
116 | 116 | assert_difference('Watcher.count', 2) do |
|
117 | 117 | xhr :post, :create, :object_type => 'issue', :object_id => '2', :watcher => {:user_ids => ['4', '7']} |
|
118 | 118 | assert_response :success |
|
119 | assert_select_rjs :replace_html, 'watchers' | |
|
120 | assert_select_rjs :replace_html, 'ajax-modal' | |
|
119 | assert_match /watchers/, response.body | |
|
120 | assert_match /ajax-modal/, response.body | |
|
121 | 121 | end |
|
122 | 122 | assert Issue.find(2).watched_by?(User.find(4)) |
|
123 | 123 | assert Issue.find(2).watched_by?(User.find(7)) |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def test_autocomplete_on_watchable_creation |
|
127 | 127 | xhr :get, :autocomplete_for_user, :q => 'mi' |
|
128 | 128 | assert_response :success |
|
129 | 129 | assert_select 'input', :count => 4 |
|
130 | 130 | assert_select 'input[name=?][value=1]', 'watcher[user_ids][]' |
|
131 | 131 | assert_select 'input[name=?][value=2]', 'watcher[user_ids][]' |
|
132 | 132 | assert_select 'input[name=?][value=8]', 'watcher[user_ids][]' |
|
133 | 133 | assert_select 'input[name=?][value=9]', 'watcher[user_ids][]' |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | 136 | def test_autocomplete_on_watchable_update |
|
137 | 137 | xhr :get, :autocomplete_for_user, :q => 'mi', :object_id => '2' , :object_type => 'issue' |
|
138 | 138 | assert_response :success |
|
139 | 139 | assert_select 'input', :count => 3 |
|
140 | 140 | assert_select 'input[name=?][value=2]', 'watcher[user_ids][]' |
|
141 | 141 | assert_select 'input[name=?][value=8]', 'watcher[user_ids][]' |
|
142 | 142 | assert_select 'input[name=?][value=9]', 'watcher[user_ids][]' |
|
143 | 143 | |
|
144 | 144 | end |
|
145 | 145 | |
|
146 | 146 | def test_append |
|
147 | 147 | @request.session[:user_id] = 2 |
|
148 | 148 | assert_no_difference 'Watcher.count' do |
|
149 | 149 | xhr :post, :append, :watcher => {:user_ids => ['4', '7']} |
|
150 | 150 | assert_response :success |
|
151 |
assert_ |
|
|
152 |
|
|
|
153 | assert_select 'input[name=?][value=7]', 'issue[watcher_user_ids][]' | |
|
154 | end | |
|
151 | assert_include 'watchers_inputs', response.body | |
|
152 | assert_include 'issue[watcher_user_ids][]', response.body | |
|
155 | 153 | end |
|
156 | 154 | end |
|
157 | 155 | |
|
158 | 156 | def test_remove_watcher |
|
159 | 157 | @request.session[:user_id] = 2 |
|
160 | 158 | assert_difference('Watcher.count', -1) do |
|
161 | 159 | xhr :post, :destroy, :object_type => 'issue', :object_id => '2', :user_id => '3' |
|
162 | 160 | assert_response :success |
|
163 | assert_select_rjs :replace_html, 'watchers' | |
|
161 | assert_match /watchers/, response.body | |
|
164 | 162 | end |
|
165 | 163 | assert !Issue.find(2).watched_by?(User.find(3)) |
|
166 | 164 | end |
|
167 | 165 | end |
General Comments 0
You need to be logged in to leave comments.
Login now