comparison .svn/pristine/a9/a9efaf1639993585b08f796d4994f7cfff12bb4e.svn-base @ 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
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class WatchersController < ApplicationController
19 before_filter :require_login, :find_watchables, :only => [:watch, :unwatch]
20
21 def watch
22 set_watcher(@watchables, User.current, true)
23 end
24
25 def unwatch
26 set_watcher(@watchables, User.current, false)
27 end
28
29 before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user]
30 accept_api_auth :create, :destroy
31
32 def new
33 end
34
35 def create
36 user_ids = []
37 if params[:watcher].is_a?(Hash)
38 user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id])
39 else
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
45 respond_to do |format|
46 format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
47 format.js
48 format.api { render_api_ok }
49 end
50 end
51
52 def append
53 if params[:watcher].is_a?(Hash)
54 user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
55 @users = User.active.find_all_by_id(user_ids)
56 end
57 end
58
59 def destroy
60 @watched.set_watcher(User.find(params[:user_id]), false)
61 respond_to do |format|
62 format.html { redirect_to :back }
63 format.js
64 format.api { render_api_ok }
65 end
66 end
67
68 def autocomplete_for_user
69 @users = User.active.sorted.like(params[:q]).limit(100).all
70 if @watched
71 @users -= @watched.watcher_users
72 end
73 render :layout => false
74 end
75
76 private
77
78 def find_project
79 if params[:object_type] && params[:object_id]
80 klass = Object.const_get(params[:object_type].camelcase)
81 return false unless klass.respond_to?('watched_by')
82 @watched = klass.find(params[:object_id])
83 @project = @watched.project
84 elsif params[:project_id]
85 @project = Project.visible.find_by_param(params[:project_id])
86 end
87 rescue
88 render_404
89 end
90
91 def find_watchables
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
104 respond_to do |format|
105 format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
106 format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
107 end
108 end
109 end