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 @ 418:a9921f3e9088

History | View | Annotate | Download (3.16 KB)

1
# redMine - project management software
2
# Copyright (C) 2006-2007  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
    if params[:replace].present?
80
      if params[:replace].is_a? Array
81
        replace_ids = params[:replace]
82
      else
83
        replace_ids = [params[:replace]]
84
      end
85
    else
86
      replace_ids = ['watcher']
87
    end
88
    respond_to do |format|
89
      format.html { redirect_to :back }
90
      format.js do
91
        render(:update) do |page|
92
          replace_ids.each do |replace_id|
93
            page.replace_html replace_id, watcher_link(@watched, user, :replace => replace_ids)
94
          end
95
        end
96
      end
97
    end
98
  rescue ::ActionController::RedirectBackError
99
    render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true
100
  end
101
end