Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: class WatchersController < ApplicationController Chris@909: before_filter :find_project Chris@909: before_filter :require_login, :check_project_privacy, :only => [:watch, :unwatch] Chris@909: before_filter :authorize, :only => [:new, :destroy] Chris@909: Chris@909: verify :method => :post, Chris@909: :only => [ :watch, :unwatch ], Chris@909: :render => { :nothing => true, :status => :method_not_allowed } Chris@909: Chris@909: def watch Chris@909: if @watched.respond_to?(:visible?) && !@watched.visible?(User.current) Chris@909: render_403 Chris@909: else Chris@909: set_watcher(User.current, true) Chris@909: end Chris@909: end Chris@909: Chris@909: def unwatch Chris@909: set_watcher(User.current, false) Chris@909: end Chris@909: Chris@909: def new Chris@909: @watcher = Watcher.new(params[:watcher]) Chris@909: @watcher.watchable = @watched Chris@909: @watcher.save if request.post? Chris@909: respond_to do |format| Chris@909: format.html { redirect_to :back } Chris@909: format.js do Chris@909: render :update do |page| Chris@909: page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} Chris@909: end Chris@909: end Chris@909: end Chris@909: rescue ::ActionController::RedirectBackError Chris@909: render :text => 'Watcher added.', :layout => true Chris@909: end Chris@909: Chris@909: def destroy Chris@909: @watched.set_watcher(User.find(params[:user_id]), false) if request.post? Chris@909: respond_to do |format| Chris@909: format.html { redirect_to :back } Chris@909: format.js do Chris@909: render :update do |page| Chris@909: page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched} Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: private Chris@909: def find_project Chris@909: klass = Object.const_get(params[:object_type].camelcase) Chris@909: return false unless klass.respond_to?('watched_by') Chris@909: @watched = klass.find(params[:object_id]) Chris@909: @project = @watched.project Chris@909: rescue Chris@909: render_404 Chris@909: end Chris@909: Chris@909: def set_watcher(user, watching) Chris@909: @watched.set_watcher(user, watching) Chris@909: respond_to do |format| Chris@909: format.html { redirect_to :back } Chris@909: format.js do Chris@909: render(:update) do |page| Chris@909: c = watcher_css(@watched) Chris@909: page.select(".#{c}").each do |item| Chris@909: page.replace_html item, watcher_link(@watched, user) Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: rescue ::ActionController::RedirectBackError Chris@909: render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true Chris@909: end Chris@909: end