;]
[askyou.git] / app / helpers / watchers_helper.rb
blob2695ce9f63e6bc50dd17f7c3e88ab28e12e13d40
1 # redMine - project management software
2 # Copyright (C) 2006-2007  Jean-Philippe Lang
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.
18 module WatchersHelper
20   # Valid options
21   # * :id - the element id
22   # * :replace - a string or array of element ids that will be
23   #   replaced
24   def watcher_tag(object, user, options={:replace => 'watcher'})
25     id = options[:id]
26     id ||= options[:replace] if options[:replace].is_a? String
27     content_tag("span", watcher_link(object, user, options), :id => id)
28   end
29   
30   # Valid options
31   # * :replace - a string or array of element ids that will be
32   #   replaced
33   def watcher_link(object, user, options={:replace => 'watcher'})
34     return '' unless user && user.logged? && object.respond_to?('watched_by?')
35     watched = object.watched_by?(user)
36     url = {:controller => 'watchers',
37            :action => (watched ? 'unwatch' : 'watch'),
38            :object_type => object.class.to_s.underscore,
39            :object_id => object.id,
40            :replace => options[:replace]}
41     link_to_remote((watched ? l(:button_unwatch) : l(:button_watch)),
42                    {:url => url},
43                    :href => url_for(url),
44                    :class => (watched ? 'icon icon-fav' : 'icon icon-fav-off'))
45   
46   end
47   
48   # Returns a comma separated list of users watching the given object
49   def watchers_list(object)
50     remove_allowed = User.current.allowed_to?("delete_#{object.class.name.underscore}_watchers".to_sym, object.project)
51     lis = object.watcher_users.collect do |user|
52       s = avatar(user, :size => "16").to_s + link_to_user(user, :class => 'user').to_s
53       if remove_allowed
54         url = {:controller => 'watchers',
55                :action => 'destroy',
56                :object_type => object.class.to_s.underscore,
57                :object_id => object.id,
58                :user_id => user}
59         s += ' ' + link_to_remote(image_tag('delete.png'),
60                                   {:url => url},
61                                   :href => url_for(url),
62                                   :style => "vertical-align: middle",
63                                   :class => "delete")
64       end
65       "<li>#{ s }</li>"
66     end
67     lis.empty? ? "" : "<ul>#{ lis.join("\n") }</ul>"
68   end
69 end