##// END OF EJS Templates
Don't compare with Hash....
Jean-Philippe Lang -
r15288:17aa2f68c260
parent child
Show More
@@ -1,149 +1,149
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class WatchersController < ApplicationController
19 19 before_action :require_login, :find_watchables, :only => [:watch, :unwatch]
20 20
21 21 def watch
22 22 set_watcher(@watchables, User.current, true)
23 23 end
24 24
25 25 def unwatch
26 26 set_watcher(@watchables, User.current, false)
27 27 end
28 28
29 29 before_action :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user]
30 30 accept_api_auth :create, :destroy
31 31
32 32 def new
33 33 @users = users_for_new_watcher
34 34 end
35 35
36 36 def create
37 37 user_ids = []
38 if params[:watcher].is_a?(Hash)
38 if params[:watcher]
39 39 user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id])
40 40 else
41 41 user_ids << params[:user_id]
42 42 end
43 43 users = User.active.visible.where(:id => user_ids.flatten.compact.uniq)
44 44 users.each do |user|
45 45 @watchables.each do |watchable|
46 46 Watcher.create(:watchable => watchable, :user => user)
47 47 end
48 48 end
49 49 respond_to do |format|
50 50 format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
51 51 format.js { @users = users_for_new_watcher }
52 52 format.api { render_api_ok }
53 53 end
54 54 end
55 55
56 56 def append
57 if params[:watcher].is_a?(Hash)
57 if params[:watcher]
58 58 user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
59 59 @users = User.active.visible.where(:id => user_ids).to_a
60 60 end
61 61 if @users.blank?
62 62 render :nothing => true
63 63 end
64 64 end
65 65
66 66 def destroy
67 67 user = User.find(params[:user_id])
68 68 @watchables.each do |watchable|
69 69 watchable.set_watcher(user, false)
70 70 end
71 71 respond_to do |format|
72 72 format.html { redirect_to :back }
73 73 format.js
74 74 format.api { render_api_ok }
75 75 end
76 76 rescue ActiveRecord::RecordNotFound
77 77 render_404
78 78 end
79 79
80 80 def autocomplete_for_user
81 81 @users = users_for_new_watcher
82 82 render :layout => false
83 83 end
84 84
85 85 private
86 86
87 87 def find_project
88 88 if params[:object_type] && params[:object_id]
89 89 @watchables = find_objets_from_params
90 90 @projects = @watchables.map(&:project).uniq
91 91 if @projects.size == 1
92 92 @project = @projects.first
93 93 end
94 94 elsif params[:project_id]
95 95 @project = Project.visible.find_by_param(params[:project_id])
96 96 end
97 97 end
98 98
99 99 def find_watchables
100 100 @watchables = find_objets_from_params
101 101 unless @watchables.present?
102 102 render_404
103 103 end
104 104 end
105 105
106 106 def set_watcher(watchables, user, watching)
107 107 watchables.each do |watchable|
108 108 watchable.set_watcher(user, watching)
109 109 end
110 110 respond_to do |format|
111 111 format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
112 112 format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
113 113 end
114 114 end
115 115
116 116 def users_for_new_watcher
117 117 scope = nil
118 118 if params[:q].blank? && @project.present?
119 119 scope = @project.users
120 120 else
121 121 scope = User.all.limit(100)
122 122 end
123 123 users = scope.active.visible.sorted.like(params[:q]).to_a
124 124 if @watchables && @watchables.size == 1
125 125 users -= @watchables.first.watcher_users
126 126 end
127 127 users
128 128 end
129 129
130 130 def find_objets_from_params
131 131 klass = Object.const_get(params[:object_type].camelcase) rescue nil
132 132 return unless klass && klass.respond_to?('watched_by')
133 133
134 134 scope = klass.where(:id => Array.wrap(params[:object_id]))
135 135 if klass.reflect_on_association(:project)
136 136 scope = scope.preload(:project => :enabled_modules)
137 137 end
138 138 objects = scope.to_a
139 139
140 140 raise Unauthorized if objects.any? do |w|
141 141 if w.respond_to?(:visible?)
142 142 !w.visible?
143 143 elsif w.respond_to?(:project) && w.project
144 144 !w.project.visible?
145 145 end
146 146 end
147 147 objects
148 148 end
149 149 end
General Comments 0
You need to be logged in to leave comments. Login now