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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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. respond_to do |format|
  33. format.js do
  34. render :update do |page|
  35. page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched}
  36. page << "showModal('ajax-modal', '400px');"
  37. page << "$('ajax-modal').addClassName('new-watcher');"
  38. end
  39. end
  40. end
  41. end
  42. def create
  43. if params[:watcher].is_a?(Hash) && request.post?
  44. user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
  45. user_ids.each do |user_id|
  46. Watcher.create(:watchable => @watched, :user_id => user_id)
  47. end
  48. end
  49. respond_to do |format|
  50. format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
  51. format.js do
  52. render :update do |page|
  53. page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched}
  54. page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched}
  55. end
  56. end
  57. end
  58. end
  59. def append
  60. if params[:watcher].is_a?(Hash)
  61. user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
  62. users = User.active.find_all_by_id(user_ids)
  63. respond_to do |format|
  64. format.js do
  65. render :update do |page|
  66. users.each do |user|
  67. page.select("#issue_watcher_user_ids_#{user.id}").each do |item|
  68. page.remove item
  69. end
  70. end
  71. page.insert_html :bottom, 'watchers_inputs', :text => watchers_checkboxes(nil, users, true)
  72. end
  73. end
  74. end
  75. end
  76. end
  77. def destroy
  78. @watched.set_watcher(User.find(params[:user_id]), false) if request.post?
  79. respond_to do |format|
  80. format.html { redirect_to :back }
  81. format.js do
  82. render :update do |page|
  83. page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched}
  84. end
  85. end
  86. end
  87. end
  88. def autocomplete_for_user
  89. @users = User.active.like(params[:q]).find(:all, :limit => 100)
  90. if @watched
  91. @users -= @watched.watcher_users
  92. end
  93. render :layout => false
  94. end
  95. private
  96. def find_project
  97. if params[:object_type] && params[:object_id]
  98. klass = Object.const_get(params[:object_type].camelcase)
  99. return false unless klass.respond_to?('watched_by')
  100. @watched = klass.find(params[:object_id])
  101. @project = @watched.project
  102. elsif params[:project_id]
  103. @project = Project.visible.find_by_param(params[:project_id])
  104. end
  105. rescue
  106. render_404
  107. end
  108. def set_watcher(user, watching)
  109. @watched.set_watcher(user, watching)
  110. respond_to do |format|
  111. format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
  112. format.js do
  113. render(:update) do |page|
  114. c = watcher_css(@watched)
  115. page.select(".#{c}").each do |item|
  116. page.replace_html item, watcher_link(@watched, user)
  117. end
  118. end
  119. end
  120. end
  121. end
  122. end