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 @ 1592:72d9219f2f19
History | View | Annotate | Download (3.88 KB)
| 1 |
# Redmine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2014 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 |
@users = users_for_new_watcher
|
| 34 |
end
|
| 35 |
|
| 36 |
def create |
| 37 |
user_ids = [] |
| 38 |
if params[:watcher].is_a?(Hash) |
| 39 |
user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id]) |
| 40 |
else
|
| 41 |
user_ids << params[:user_id]
|
| 42 |
end
|
| 43 |
user_ids.flatten.compact.uniq.each do |user_id|
|
| 44 |
Watcher.create(:watchable => @watched, :user_id => user_id) |
| 45 |
end
|
| 46 |
respond_to do |format|
|
| 47 |
format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
|
| 48 |
format.js { @users = users_for_new_watcher }
|
| 49 |
format.api { render_api_ok }
|
| 50 |
end
|
| 51 |
end
|
| 52 |
|
| 53 |
def append |
| 54 |
if params[:watcher].is_a?(Hash) |
| 55 |
user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] |
| 56 |
@users = User.active.where(:id => user_ids).all |
| 57 |
end
|
| 58 |
if @users.blank? |
| 59 |
render :nothing => true |
| 60 |
end
|
| 61 |
end
|
| 62 |
|
| 63 |
def destroy |
| 64 |
@watched.set_watcher(User.find(params[:user_id]), false) |
| 65 |
respond_to do |format|
|
| 66 |
format.html { redirect_to :back }
|
| 67 |
format.js |
| 68 |
format.api { render_api_ok }
|
| 69 |
end
|
| 70 |
end
|
| 71 |
|
| 72 |
def autocomplete_for_user |
| 73 |
@users = users_for_new_watcher
|
| 74 |
render :layout => false |
| 75 |
end
|
| 76 |
|
| 77 |
private |
| 78 |
|
| 79 |
def find_project |
| 80 |
if params[:object_type] && params[:object_id] |
| 81 |
klass = Object.const_get(params[:object_type].camelcase) |
| 82 |
return false unless klass.respond_to?('watched_by') |
| 83 |
@watched = klass.find(params[:object_id]) |
| 84 |
@project = @watched.project |
| 85 |
elsif params[:project_id] |
| 86 |
@project = Project.visible.find_by_param(params[:project_id]) |
| 87 |
end
|
| 88 |
rescue
|
| 89 |
render_404 |
| 90 |
end
|
| 91 |
|
| 92 |
def find_watchables |
| 93 |
klass = Object.const_get(params[:object_type].camelcase) rescue nil |
| 94 |
if klass && klass.respond_to?('watched_by') |
| 95 |
@watchables = klass.where(:id => Array.wrap(params[:object_id])).all |
| 96 |
raise Unauthorized if @watchables.any? {|w| |
| 97 |
if w.respond_to?(:visible?) |
| 98 |
!w.visible? |
| 99 |
elsif w.respond_to?(:project) && w.project |
| 100 |
!w.project.visible? |
| 101 |
end
|
| 102 |
} |
| 103 |
end
|
| 104 |
render_404 unless @watchables.present? |
| 105 |
end
|
| 106 |
|
| 107 |
def set_watcher(watchables, user, watching) |
| 108 |
watchables.each do |watchable|
|
| 109 |
watchable.set_watcher(user, watching) |
| 110 |
end
|
| 111 |
respond_to do |format|
|
| 112 |
format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
| 113 |
format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
|
| 114 |
end
|
| 115 |
end
|
| 116 |
|
| 117 |
def users_for_new_watcher |
| 118 |
users = [] |
| 119 |
if params[:q].blank? && @project.present? |
| 120 |
users = @project.users.sorted
|
| 121 |
else
|
| 122 |
users = User.active.sorted.like(params[:q]).limit(100) |
| 123 |
end
|
| 124 |
if @watched |
| 125 |
users -= @watched.watcher_users
|
| 126 |
end
|
| 127 |
users |
| 128 |
end
|
| 129 |
end
|