To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / controllers / watchers_controller.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (3.44 KB)
| 1 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 1295:622f24f53b42 | Chris | before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] |
| 20 | 909:cbb26bc654de | Chris | |
| 21 | 0:513646585e45 | Chris | def watch |
| 22 | 1295:622f24f53b42 | Chris | set_watcher(@watchables, User.current, true) |
| 23 | 0:513646585e45 | Chris | end
|
| 24 | 909:cbb26bc654de | Chris | |
| 25 | 0:513646585e45 | Chris | def unwatch |
| 26 | 1295:622f24f53b42 | Chris | set_watcher(@watchables, User.current, false) |
| 27 | 0:513646585e45 | Chris | end
|
| 28 | 909:cbb26bc654de | Chris | |
| 29 | 1295:622f24f53b42 | Chris | before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user] |
| 30 | accept_api_auth :create, :destroy |
||
| 31 | |||
| 32 | 0:513646585e45 | Chris | def new |
| 33 | 1115:433d4f72a19b | Chris | end
|
| 34 | |||
| 35 | def create |
||
| 36 | 1295:622f24f53b42 | Chris | 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 | 0:513646585e45 | Chris | end
|
| 45 | 1115:433d4f72a19b | Chris | respond_to do |format|
|
| 46 | format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
|
||
| 47 | format.js |
||
| 48 | 1295:622f24f53b42 | Chris | format.api { render_api_ok }
|
| 49 | 1115:433d4f72a19b | Chris | 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 | 0:513646585e45 | Chris | end
|
| 58 | 909:cbb26bc654de | Chris | |
| 59 | 0:513646585e45 | Chris | def destroy |
| 60 | 1295:622f24f53b42 | Chris | @watched.set_watcher(User.find(params[:user_id]), false) |
| 61 | 0:513646585e45 | Chris | respond_to do |format|
|
| 62 | format.html { redirect_to :back }
|
||
| 63 | 1115:433d4f72a19b | Chris | format.js |
| 64 | 1295:622f24f53b42 | Chris | format.api { render_api_ok }
|
| 65 | 0:513646585e45 | Chris | end
|
| 66 | end
|
||
| 67 | 909:cbb26bc654de | Chris | |
| 68 | 1115:433d4f72a19b | Chris | def autocomplete_for_user |
| 69 | 1295:622f24f53b42 | Chris | @users = User.active.sorted.like(params[:q]).limit(100).all |
| 70 | 1115:433d4f72a19b | Chris | if @watched |
| 71 | @users -= @watched.watcher_users |
||
| 72 | end
|
||
| 73 | render :layout => false |
||
| 74 | end
|
||
| 75 | |||
| 76 | 1295:622f24f53b42 | Chris | private |
| 77 | |||
| 78 | 0:513646585e45 | Chris | def find_project |
| 79 | 1115:433d4f72a19b | Chris | 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 | 0:513646585e45 | Chris | rescue
|
| 88 | render_404 |
||
| 89 | end
|
||
| 90 | 909:cbb26bc654de | Chris | |
| 91 | 1295:622f24f53b42 | Chris | 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 | 0:513646585e45 | Chris | respond_to do |format|
|
| 105 | 1115:433d4f72a19b | Chris | format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
| 106 | 1295:622f24f53b42 | Chris | format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
|
| 107 | 0:513646585e45 | Chris | end
|
| 108 | end
|
||
| 109 | end |