You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watchers_controller.rb 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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. class WatchersController < ApplicationController
  18. before_filter :find_project
  19. before_filter :require_login, :check_project_privacy, :only => [:watch, :unwatch]
  20. before_filter :authorize, :only => [:new, :destroy]
  21. def watch
  22. if @watched.respond_to?(:visible?) && !@watched.visible?(User.current)
  23. render_403
  24. else
  25. set_watcher(User.current, true)
  26. end
  27. end
  28. def unwatch
  29. set_watcher(User.current, false)
  30. end
  31. def new
  32. end
  33. def create
  34. if params[:watcher].is_a?(Hash) && request.post?
  35. user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
  36. user_ids.each do |user_id|
  37. Watcher.create(:watchable => @watched, :user_id => user_id)
  38. end
  39. end
  40. respond_to do |format|
  41. format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
  42. format.js
  43. end
  44. end
  45. def append
  46. if params[:watcher].is_a?(Hash)
  47. user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
  48. @users = User.active.find_all_by_id(user_ids)
  49. end
  50. end
  51. def destroy
  52. @watched.set_watcher(User.find(params[:user_id]), false) if request.post?
  53. respond_to do |format|
  54. format.html { redirect_to :back }
  55. format.js
  56. end
  57. end
  58. def autocomplete_for_user
  59. @users = User.active.like(params[:q]).limit(100).all
  60. if @watched
  61. @users -= @watched.watcher_users
  62. end
  63. render :layout => false
  64. end
  65. private
  66. def find_project
  67. if params[:object_type] && params[:object_id]
  68. klass = Object.const_get(params[:object_type].camelcase)
  69. return false unless klass.respond_to?('watched_by')
  70. @watched = klass.find(params[:object_id])
  71. @project = @watched.project
  72. elsif params[:project_id]
  73. @project = Project.visible.find_by_param(params[:project_id])
  74. end
  75. rescue
  76. render_404
  77. end
  78. def set_watcher(user, watching)
  79. @watched.set_watcher(user, watching)
  80. respond_to do |format|
  81. format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
  82. format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => @watched} }
  83. end
  84. end
  85. end