comparison app/controllers/watchers_controller.rb @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents e248c7af89ec
children
comparison
equal deleted inserted replaced
1516:b450a9d58aed 1517:dffacf8a6908
28 28
29 before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user] 29 before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user]
30 accept_api_auth :create, :destroy 30 accept_api_auth :create, :destroy
31 31
32 def new 32 def new
33 @users = users_for_new_watcher
33 end 34 end
34 35
35 def create 36 def create
36 user_ids = [] 37 user_ids = []
37 if params[:watcher].is_a?(Hash) 38 if params[:watcher].is_a?(Hash)
42 user_ids.flatten.compact.uniq.each do |user_id| 43 user_ids.flatten.compact.uniq.each do |user_id|
43 Watcher.create(:watchable => @watched, :user_id => user_id) 44 Watcher.create(:watchable => @watched, :user_id => user_id)
44 end 45 end
45 respond_to do |format| 46 respond_to do |format|
46 format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} 47 format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
47 format.js 48 format.js { @users = users_for_new_watcher }
48 format.api { render_api_ok } 49 format.api { render_api_ok }
49 end 50 end
50 end 51 end
51 52
52 def append 53 def append
53 if params[:watcher].is_a?(Hash) 54 if params[:watcher].is_a?(Hash)
54 user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]] 55 user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
55 @users = User.active.find_all_by_id(user_ids) 56 @users = User.active.where(:id => user_ids).all
57 end
58 if @users.blank?
59 render :nothing => true
56 end 60 end
57 end 61 end
58 62
59 def destroy 63 def destroy
60 @watched.set_watcher(User.find(params[:user_id]), false) 64 @watched.set_watcher(User.find(params[:user_id]), false)
64 format.api { render_api_ok } 68 format.api { render_api_ok }
65 end 69 end
66 end 70 end
67 71
68 def autocomplete_for_user 72 def autocomplete_for_user
69 @users = User.active.sorted.like(params[:q]).limit(100).all 73 @users = users_for_new_watcher
70 if @watched
71 @users -= @watched.watcher_users
72 end
73 render :layout => false 74 render :layout => false
74 end 75 end
75 76
76 private 77 private
77 78
89 end 90 end
90 91
91 def find_watchables 92 def find_watchables
92 klass = Object.const_get(params[:object_type].camelcase) rescue nil 93 klass = Object.const_get(params[:object_type].camelcase) rescue nil
93 if klass && klass.respond_to?('watched_by') 94 if klass && klass.respond_to?('watched_by')
94 @watchables = klass.find_all_by_id(Array.wrap(params[:object_id])) 95 @watchables = klass.where(:id => Array.wrap(params[:object_id])).all
95 raise Unauthorized if @watchables.any? {|w| w.respond_to?(:visible?) && !w.visible?} 96 raise Unauthorized if @watchables.any? {|w|
97 if w.respond_to?(:visible?)
98 !w.visible?
99 elsif w.respond_to?(:project) && w.project
100 !w.project.visible?
101 end
102 }
96 end 103 end
97 render_404 unless @watchables.present? 104 render_404 unless @watchables.present?
98 end 105 end
99 106
100 def set_watcher(watchables, user, watching) 107 def set_watcher(watchables, user, watching)
104 respond_to do |format| 111 respond_to do |format|
105 format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} 112 format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
106 format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} } 113 format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
107 end 114 end
108 end 115 end
116
117 def users_for_new_watcher
118 users = []
119 if params[:q].blank? && @project.present?
120 users = @project.users.sorted
121 else
122 users = User.active.sorted.like(params[:q]).limit(100)
123 end
124 if @watched
125 users -= @watched.watcher_users
126 end
127 users
128 end
109 end 129 end