2011-04-05 16:09:15 +04:00
|
|
|
# Redmine - project management software
|
2012-05-05 16:56:53 +04:00
|
|
|
# Copyright (C) 2006-2012 Jean-Philippe Lang
|
2007-04-21 16:08:31 +04:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-30 18:09:58 +04:00
|
|
|
#
|
2007-04-21 16:08:31 +04:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-08-30 18:09:58 +04:00
|
|
|
#
|
2007-04-21 16:08:31 +04:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
class WatchersController < ApplicationController
|
2008-08-03 13:14:43 +04:00
|
|
|
before_filter :find_project
|
|
|
|
before_filter :require_login, :check_project_privacy, :only => [:watch, :unwatch]
|
2009-10-25 15:11:53 +03:00
|
|
|
before_filter :authorize, :only => [:new, :destroy]
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
def watch
|
2009-12-13 17:48:28 +03:00
|
|
|
if @watched.respond_to?(:visible?) && !@watched.visible?(User.current)
|
|
|
|
render_403
|
|
|
|
else
|
|
|
|
set_watcher(User.current, true)
|
|
|
|
end
|
2008-08-03 13:14:43 +04:00
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
def unwatch
|
|
|
|
set_watcher(User.current, false)
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
def new
|
2012-01-09 22:37:16 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
if params[:watcher].is_a?(Hash) && request.post?
|
|
|
|
user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
|
|
|
|
user_ids.each do |user_id|
|
|
|
|
Watcher.create(:watchable => @watched, :user_id => user_id)
|
|
|
|
end
|
|
|
|
end
|
2007-05-13 23:43:35 +04:00
|
|
|
respond_to do |format|
|
2012-04-06 20:51:10 +04:00
|
|
|
format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
|
2012-07-19 21:17:10 +04:00
|
|
|
format.js
|
2007-05-13 23:43:35 +04:00
|
|
|
end
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2012-03-24 16:57:28 +04:00
|
|
|
def append
|
|
|
|
if params[:watcher].is_a?(Hash)
|
|
|
|
user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
|
2012-07-19 21:17:10 +04:00
|
|
|
@users = User.active.find_all_by_id(user_ids)
|
2012-03-24 16:57:28 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-25 15:11:53 +03:00
|
|
|
def destroy
|
|
|
|
@watched.set_watcher(User.find(params[:user_id]), false) if request.post?
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to :back }
|
2012-07-19 21:17:10 +04:00
|
|
|
format.js
|
2009-10-25 15:11:53 +03:00
|
|
|
end
|
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2012-01-09 22:37:16 +04:00
|
|
|
def autocomplete_for_user
|
2012-03-24 16:57:28 +04:00
|
|
|
@users = User.active.like(params[:q]).find(:all, :limit => 100)
|
|
|
|
if @watched
|
2012-04-05 22:10:54 +04:00
|
|
|
@users -= @watched.watcher_users
|
2012-03-24 16:57:28 +04:00
|
|
|
end
|
2012-01-09 22:37:16 +04:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
2007-04-21 16:08:31 +04:00
|
|
|
private
|
|
|
|
def find_project
|
2012-03-24 16:57:28 +04:00
|
|
|
if params[:object_type] && params[:object_id]
|
|
|
|
klass = Object.const_get(params[:object_type].camelcase)
|
|
|
|
return false unless klass.respond_to?('watched_by')
|
|
|
|
@watched = klass.find(params[:object_id])
|
|
|
|
@project = @watched.project
|
|
|
|
elsif params[:project_id]
|
2012-04-25 21:17:49 +04:00
|
|
|
@project = Project.visible.find_by_param(params[:project_id])
|
2012-03-24 16:57:28 +04:00
|
|
|
end
|
2007-05-13 23:43:35 +04:00
|
|
|
rescue
|
|
|
|
render_404
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
def set_watcher(user, watching)
|
|
|
|
@watched.set_watcher(user, watching)
|
|
|
|
respond_to do |format|
|
2012-04-06 20:51:10 +04:00
|
|
|
format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
2012-07-19 21:17:10 +04:00
|
|
|
format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => @watched} }
|
2008-08-03 13:14:43 +04:00
|
|
|
end
|
|
|
|
end
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|