annotate .svn/pristine/e3/e3a1bc2418b9405de90074a0fbc8955237ac8898.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 class WatchersController < ApplicationController
Chris@1494 19 before_filter :require_login, :find_watchables, :only => [:watch, :unwatch]
Chris@1494 20
Chris@1494 21 def watch
Chris@1494 22 set_watcher(@watchables, User.current, true)
Chris@1494 23 end
Chris@1494 24
Chris@1494 25 def unwatch
Chris@1494 26 set_watcher(@watchables, User.current, false)
Chris@1494 27 end
Chris@1494 28
Chris@1494 29 before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user]
Chris@1494 30 accept_api_auth :create, :destroy
Chris@1494 31
Chris@1494 32 def new
Chris@1494 33 end
Chris@1494 34
Chris@1494 35 def create
Chris@1494 36 user_ids = []
Chris@1494 37 if params[:watcher].is_a?(Hash)
Chris@1494 38 user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id])
Chris@1494 39 else
Chris@1494 40 user_ids << params[:user_id]
Chris@1494 41 end
Chris@1494 42 user_ids.flatten.compact.uniq.each do |user_id|
Chris@1494 43 Watcher.create(:watchable => @watched, :user_id => user_id)
Chris@1494 44 end
Chris@1494 45 respond_to do |format|
Chris@1494 46 format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
Chris@1494 47 format.js
Chris@1494 48 format.api { render_api_ok }
Chris@1494 49 end
Chris@1494 50 end
Chris@1494 51
Chris@1494 52 def append
Chris@1494 53 if params[:watcher].is_a?(Hash)
Chris@1494 54 user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
Chris@1494 55 @users = User.active.find_all_by_id(user_ids)
Chris@1494 56 end
Chris@1494 57 end
Chris@1494 58
Chris@1494 59 def destroy
Chris@1494 60 @watched.set_watcher(User.find(params[:user_id]), false)
Chris@1494 61 respond_to do |format|
Chris@1494 62 format.html { redirect_to :back }
Chris@1494 63 format.js
Chris@1494 64 format.api { render_api_ok }
Chris@1494 65 end
Chris@1494 66 end
Chris@1494 67
Chris@1494 68 def autocomplete_for_user
Chris@1494 69 @users = User.active.sorted.like(params[:q]).limit(100).all
Chris@1494 70 if @watched
Chris@1494 71 @users -= @watched.watcher_users
Chris@1494 72 end
Chris@1494 73 render :layout => false
Chris@1494 74 end
Chris@1494 75
Chris@1494 76 private
Chris@1494 77
Chris@1494 78 def find_project
Chris@1494 79 if params[:object_type] && params[:object_id]
Chris@1494 80 klass = Object.const_get(params[:object_type].camelcase)
Chris@1494 81 return false unless klass.respond_to?('watched_by')
Chris@1494 82 @watched = klass.find(params[:object_id])
Chris@1494 83 @project = @watched.project
Chris@1494 84 elsif params[:project_id]
Chris@1494 85 @project = Project.visible.find_by_param(params[:project_id])
Chris@1494 86 end
Chris@1494 87 rescue
Chris@1494 88 render_404
Chris@1494 89 end
Chris@1494 90
Chris@1494 91 def find_watchables
Chris@1494 92 klass = Object.const_get(params[:object_type].camelcase) rescue nil
Chris@1494 93 if klass && klass.respond_to?('watched_by')
Chris@1494 94 @watchables = klass.find_all_by_id(Array.wrap(params[:object_id]))
Chris@1494 95 raise Unauthorized if @watchables.any? {|w| w.respond_to?(:visible?) && !w.visible?}
Chris@1494 96 end
Chris@1494 97 render_404 unless @watchables.present?
Chris@1494 98 end
Chris@1494 99
Chris@1494 100 def set_watcher(watchables, user, watching)
Chris@1494 101 watchables.each do |watchable|
Chris@1494 102 watchable.set_watcher(user, watching)
Chris@1494 103 end
Chris@1494 104 respond_to do |format|
Chris@1494 105 format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
Chris@1494 106 format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
Chris@1494 107 end
Chris@1494 108 end
Chris@1494 109 end