Mercurial > hg > soundsoftware-site
comparison app/controllers/watchers_controller.rb @ 1298:4f746d8966dd redmine_2.3_integration
Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:28:30 +0100 |
parents | 622f24f53b42 |
children | e248c7af89ec |
comparison
equal
deleted
inserted
replaced
1297:0a574315af3e | 1298:4f746d8966dd |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2012 Jean-Philippe Lang | 2 # Copyright (C) 2006-2013 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 |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
14 # You should have received a copy of the GNU General Public License | 14 # You should have received a copy of the GNU General Public License |
15 # along with this program; if not, write to the Free Software | 15 # along with this program; if not, write to the Free Software |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | 17 |
18 class WatchersController < ApplicationController | 18 class WatchersController < ApplicationController |
19 before_filter :find_project | 19 before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] |
20 before_filter :require_login, :check_project_privacy, :only => [:watch, :unwatch] | |
21 before_filter :authorize, :only => [:new, :destroy] | |
22 | 20 |
23 def watch | 21 def watch |
24 if @watched.respond_to?(:visible?) && !@watched.visible?(User.current) | 22 set_watcher(@watchables, User.current, true) |
25 render_403 | |
26 else | |
27 set_watcher(User.current, true) | |
28 end | |
29 end | 23 end |
30 | 24 |
31 def unwatch | 25 def unwatch |
32 set_watcher(User.current, false) | 26 set_watcher(@watchables, User.current, false) |
33 end | 27 end |
28 | |
29 before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user] | |
30 accept_api_auth :create, :destroy | |
34 | 31 |
35 def new | 32 def new |
36 end | 33 end |
37 | 34 |
38 def create | 35 def create |
39 if params[:watcher].is_a?(Hash) && request.post? | 36 user_ids = [] |
40 user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] | 37 if params[:watcher].is_a?(Hash) |
41 user_ids.each do |user_id| | 38 user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id]) |
42 Watcher.create(:watchable => @watched, :user_id => user_id) | 39 else |
43 end | 40 user_ids << params[:user_id] |
41 end | |
42 user_ids.flatten.compact.uniq.each do |user_id| | |
43 Watcher.create(:watchable => @watched, :user_id => user_id) | |
44 end | 44 end |
45 respond_to do |format| | 45 respond_to do |format| |
46 format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} | 46 format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} |
47 format.js | 47 format.js |
48 format.api { render_api_ok } | |
48 end | 49 end |
49 end | 50 end |
50 | 51 |
51 def append | 52 def append |
52 if params[:watcher].is_a?(Hash) | 53 if params[:watcher].is_a?(Hash) |
54 @users = User.active.find_all_by_id(user_ids) | 55 @users = User.active.find_all_by_id(user_ids) |
55 end | 56 end |
56 end | 57 end |
57 | 58 |
58 def destroy | 59 def destroy |
59 @watched.set_watcher(User.find(params[:user_id]), false) if request.post? | 60 @watched.set_watcher(User.find(params[:user_id]), false) |
60 respond_to do |format| | 61 respond_to do |format| |
61 format.html { redirect_to :back } | 62 format.html { redirect_to :back } |
62 format.js | 63 format.js |
64 format.api { render_api_ok } | |
63 end | 65 end |
64 end | 66 end |
65 | 67 |
66 def autocomplete_for_user | 68 def autocomplete_for_user |
67 @users = User.active.like(params[:q]).find(:all, :limit => 100) | 69 @users = User.active.sorted.like(params[:q]).limit(100).all |
68 if @watched | 70 if @watched |
69 @users -= @watched.watcher_users | 71 @users -= @watched.watcher_users |
70 end | 72 end |
71 render :layout => false | 73 render :layout => false |
72 end | 74 end |
73 | 75 |
74 private | 76 private |
77 | |
75 def find_project | 78 def find_project |
76 if params[:object_type] && params[:object_id] | 79 if params[:object_type] && params[:object_id] |
77 klass = Object.const_get(params[:object_type].camelcase) | 80 klass = Object.const_get(params[:object_type].camelcase) |
78 return false unless klass.respond_to?('watched_by') | 81 return false unless klass.respond_to?('watched_by') |
79 @watched = klass.find(params[:object_id]) | 82 @watched = klass.find(params[:object_id]) |
83 end | 86 end |
84 rescue | 87 rescue |
85 render_404 | 88 render_404 |
86 end | 89 end |
87 | 90 |
88 def set_watcher(user, watching) | 91 def find_watchables |
89 @watched.set_watcher(user, watching) | 92 klass = Object.const_get(params[:object_type].camelcase) rescue nil |
93 if klass && klass.respond_to?('watched_by') | |
94 @watchables = klass.find_all_by_id(Array.wrap(params[:object_id])) | |
95 raise Unauthorized if @watchables.any? {|w| w.respond_to?(:visible?) && !w.visible?} | |
96 end | |
97 render_404 unless @watchables.present? | |
98 end | |
99 | |
100 def set_watcher(watchables, user, watching) | |
101 watchables.each do |watchable| | |
102 watchable.set_watcher(user, watching) | |
103 end | |
90 respond_to do |format| | 104 respond_to do |format| |
91 format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} | 105 format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} |
92 format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => @watched} } | 106 format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} } |
93 end | 107 end |
94 end | 108 end |
95 end | 109 end |