You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watchers_helper.rb 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module WatchersHelper
  19. def watcher_link(objects, user)
  20. return '' unless user && user.logged?
  21. objects = Array.wrap(objects)
  22. return '' unless objects.any?
  23. watched = Watcher.any_watched?(objects, user)
  24. css = [watcher_css(objects), watched ? 'icon icon-fav' : 'icon icon-fav-off'].join(' ')
  25. text = watched ? l(:button_unwatch) : l(:button_watch)
  26. url = watch_path(
  27. :object_type => objects.first.class.to_s.underscore,
  28. :object_id => (objects.size == 1 ? objects.first.id : objects.map(&:id).sort)
  29. )
  30. method = watched ? 'delete' : 'post'
  31. link_to text, url, :remote => true, :method => method, :class => css
  32. end
  33. # Returns the css class used to identify watch links for a given +object+
  34. def watcher_css(objects)
  35. objects = Array.wrap(objects)
  36. id = (objects.size == 1 ? objects.first.id : 'bulk')
  37. "#{objects.first.class.to_s.underscore}-#{id}-watcher"
  38. end
  39. # Returns a comma separated list of users watching the given object
  40. def watchers_list(object)
  41. remove_allowed = User.current.allowed_to?("delete_#{object.class.name.underscore}_watchers".to_sym, object.project)
  42. content = ''.html_safe
  43. lis = object.watcher_users.preload(:email_address).collect do |user|
  44. s = ''.html_safe
  45. s << avatar(user, :size => "16").to_s
  46. s << link_to_user(user, :class => 'user')
  47. if remove_allowed
  48. url = {:controller => 'watchers',
  49. :action => 'destroy',
  50. :object_type => object.class.to_s.underscore,
  51. :object_id => object.id,
  52. :user_id => user}
  53. s << ' '
  54. s << link_to(l(:button_delete), url,
  55. :remote => true, :method => 'delete',
  56. :class => "delete icon-only icon-del",
  57. :title => l(:button_delete))
  58. end
  59. content << content_tag('li', s, :class => "user-#{user.id}")
  60. end
  61. content.present? ? content_tag('ul', content, :class => 'watchers') : content
  62. end
  63. def watchers_checkboxes(object, users, checked=nil)
  64. users.map do |user|
  65. c = checked.nil? ? object.watched_by?(user) : checked
  66. tag = check_box_tag 'issue[watcher_user_ids][]', user.id, c, :id => nil
  67. content_tag 'label', "#{tag} #{h(user)}".html_safe,
  68. :id => "issue_watcher_user_ids_#{user.id}",
  69. :class => "floating"
  70. end.join.html_safe
  71. end
  72. end