To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / watchers_controller.rb @ 1361:7c0909052511

History | View | Annotate | Download (2.95 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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

    
18
class WatchersController < ApplicationController
19
  before_filter :find_project
20
  before_filter :require_login, :check_project_privacy, :only => [:watch, :unwatch]
21
  before_filter :authorize, :only => [:new, :destroy]
22

    
23
  def watch
24
    if @watched.respond_to?(:visible?) && !@watched.visible?(User.current)
25
      render_403
26
    else
27
      set_watcher(User.current, true)
28
    end
29
  end
30

    
31
  def unwatch
32
    set_watcher(User.current, false)
33
  end
34

    
35
  def new
36
  end
37

    
38
  def create
39
    if params[:watcher].is_a?(Hash) && request.post?
40
      user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
41
      user_ids.each do |user_id|
42
        Watcher.create(:watchable => @watched, :user_id => user_id)
43
      end
44
    end
45
    respond_to do |format|
46
      format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
47
      format.js
48
    end
49
  end
50

    
51
  def append
52
    if params[:watcher].is_a?(Hash)
53
      user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
54
      @users = User.active.find_all_by_id(user_ids)
55
    end
56
  end
57

    
58
  def destroy
59
    @watched.set_watcher(User.find(params[:user_id]), false) if request.post?
60
    respond_to do |format|
61
      format.html { redirect_to :back }
62
      format.js
63
    end
64
  end
65

    
66
  def autocomplete_for_user
67
    @users = User.active.like(params[:q]).find(:all, :limit => 100)
68
    if @watched
69
      @users -= @watched.watcher_users
70
    end
71
    render :layout => false
72
  end
73

    
74
private
75
  def find_project
76
    if params[:object_type] && params[:object_id]
77
      klass = Object.const_get(params[:object_type].camelcase)
78
      return false unless klass.respond_to?('watched_by')
79
      @watched = klass.find(params[:object_id])
80
      @project = @watched.project
81
    elsif params[:project_id]
82
      @project = Project.visible.find_by_param(params[:project_id])
83
    end
84
  rescue
85
    render_404
86
  end
87

    
88
  def set_watcher(user, watching)
89
    @watched.set_watcher(user, watching)
90
    respond_to do |format|
91
      format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
92
      format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => @watched} }
93
    end
94
  end
95
end