2007-04-21 16:08:31 +04:00
module WatchersHelper
2009-12-03 21:41:00 +03:00
2011-05-17 21:00:38 +04:00
# Deprecated method. Use watcher_link instead
#
# This method will be removed in ChiliProject 3.0 or later
def watcher_tag ( object , user , options = { :replace = > 'watcher' } )
ActiveSupport :: Deprecation . warn " The WatchersHelper # watcher_tag is deprecated and will be removed in ChiliProject 3.0. Please use WatchersHelper # watcher_link instead. Please also note the differences between the APIs. " , caller
options [ :id ] || = options [ :replace ] if options [ :replace ] . is_a? String
options [ :replace ] = Array ( options [ :replace ] ) . map { | id | " # #{ id } " }
watcher_link ( object , user , options )
end
2011-05-17 20:15:34 +04:00
# Create a link to watch/unwatch object
#
# * :replace - a string or array of strings with css selectors that will be updated, whenever the watcher status is changed
def watcher_link ( object , user , options = { :replace = > '.watcher_link' , :class = > 'watcher_link' } )
options = options . with_indifferent_access
raise ArgumentError , 'Missing :replace option in options hash' if options [ 'replace' ] . blank?
2007-08-29 20:52:35 +04:00
return '' unless user && user . logged? && object . respond_to? ( 'watched_by?' )
2011-05-17 20:15:34 +04:00
2007-05-13 23:43:35 +04:00
watched = object . watched_by? ( user )
url = { :controller = > 'watchers' ,
2008-08-03 13:14:43 +04:00
:action = > ( watched ? 'unwatch' : 'watch' ) ,
2007-05-13 23:43:35 +04:00
:object_type = > object . class . to_s . underscore ,
2009-12-03 21:41:00 +03:00
:object_id = > object . id ,
2011-05-17 20:15:34 +04:00
:replace = > options . delete ( 'replace' ) }
url_options = { :url = > url }
html_options = options . merge ( :href = > url_for ( url ) )
2011-05-17 21:03:20 +04:00
html_options [ :class ] = html_options [ :class ] . to_s + ( watched ? ' icon icon-fav' : ' icon icon-fav-off' )
2011-05-17 20:15:34 +04:00
link_to_remote ( ( watched ? l ( :button_unwatch ) : l ( :button_watch ) ) , url_options , html_options )
2007-05-13 23:43:35 +04:00
end
2008-08-03 13:14:43 +04:00
# Returns a comma separated list of users watching the given object
def watchers_list ( object )
2009-10-25 15:11:53 +03:00
remove_allowed = User . current . allowed_to? ( " delete_ #{ object . class . name . underscore } _watchers " . to_sym , object . project )
2010-03-13 20:45:41 +03:00
lis = object . watcher_users . collect do | user |
s = avatar ( user , :size = > " 16 " ) . to_s + link_to_user ( user , :class = > 'user' ) . to_s
2009-10-25 15:11:53 +03:00
if remove_allowed
url = { :controller = > 'watchers' ,
:action = > 'destroy' ,
:object_type = > object . class . to_s . underscore ,
:object_id = > object . id ,
:user_id = > user }
s += ' ' + link_to_remote ( image_tag ( 'delete.png' ) ,
{ :url = > url } ,
:href = > url_for ( url ) ,
2010-03-13 20:45:41 +03:00
:style = > " vertical-align: middle " ,
:class = > " delete " )
2009-10-25 15:11:53 +03:00
end
2010-03-13 20:45:41 +03:00
" <li> #{ s } </li> "
end
lis . empty? ? " " : " <ul> #{ lis . join ( " \n " ) } </ul> "
2008-08-03 13:14:43 +04:00
end
2007-04-21 16:08:31 +04:00
end