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 @ 767:dd33798e514d

History | View | Annotate | Download (2.95 KB)

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

    
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
  verify :method => :post,
24
         :only => [ :watch, :unwatch ],
25
         :render => { :nothing => true, :status => :method_not_allowed }
26
  
27
  def watch
28
    if @watched.respond_to?(:visible?) && !@watched.visible?(User.current)
29
      render_403
30
    else
31
      set_watcher(User.current, true)
32
    end
33
  end
34
  
35
  def unwatch
36
    set_watcher(User.current, false)
37
  end
38
  
39
  def new
40
    @watcher = Watcher.new(params[:watcher])
41
    @watcher.watchable = @watched
42
    @watcher.save if request.post?
43
    respond_to do |format|
44
      format.html { redirect_to :back }
45
      format.js do
46
        render :update do |page|
47
          page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched}
48
        end
49
      end
50
    end
51
  rescue ::ActionController::RedirectBackError
52
    render :text => 'Watcher added.', :layout => true
53
  end
54
  
55
  def destroy
56
    @watched.set_watcher(User.find(params[:user_id]), false) if request.post?
57
    respond_to do |format|
58
      format.html { redirect_to :back }
59
      format.js do
60
        render :update do |page|
61
          page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched}
62
        end
63
      end
64
    end
65
  end
66
  
67
private
68
  def find_project
69
    klass = Object.const_get(params[:object_type].camelcase)
70
    return false unless klass.respond_to?('watched_by')
71
    @watched = klass.find(params[:object_id])
72
    @project = @watched.project
73
  rescue
74
    render_404
75
  end
76
  
77
  def set_watcher(user, watching)
78
    @watched.set_watcher(user, watching)
79
    respond_to do |format|
80
      format.html { redirect_to :back }
81
      format.js do
82
        render(:update) do |page|
83
          c = watcher_css(@watched)
84
          page.select(".#{c}").each do |item|
85
            page.replace_html item, watcher_link(@watched, user)
86
          end
87
        end
88
      end
89
    end
90
  rescue ::ActionController::RedirectBackError
91
    render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true
92
  end
93
end