##// END OF EJS Templates
Adds Watcher.any_watched? to check if at least one object of a collection is watched....
Jean-Philippe Lang -
r11729:e0c7eb25a42e
parent child
Show More
@@ -28,7 +28,7 module WatchersHelper
28 return '' unless user && user.logged?
28 return '' unless user && user.logged?
29 objects = Array.wrap(objects)
29 objects = Array.wrap(objects)
30
30
31 watched = objects.any? {|object| object.watched_by?(user)}
31 watched = Watcher.any_watched?(objects, user)
32 css = [watcher_css(objects), watched ? 'icon icon-fav' : 'icon icon-fav-off'].join(' ')
32 css = [watcher_css(objects), watched ? 'icon icon-fav' : 'icon icon-fav-off'].join(' ')
33 text = watched ? l(:button_unwatch) : l(:button_watch)
33 text = watched ? l(:button_unwatch) : l(:button_watch)
34 url = watch_path(
34 url = watch_path(
@@ -23,6 +23,19 class Watcher < ActiveRecord::Base
23 validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id]
23 validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id]
24 validate :validate_user
24 validate :validate_user
25
25
26 # Returns true if at least one object among objects is watched by user
27 def self.any_watched?(objects, user)
28 objects = objects.reject(&:new_record?)
29 if objects.any?
30 objects.group_by {|object| object.class.base_class}.each do |base_class, objects|
31 if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists?
32 return true
33 end
34 end
35 end
36 false
37 end
38
26 # Unwatch things that users are no longer allowed to view
39 # Unwatch things that users are no longer allowed to view
27 def self.prune(options={})
40 def self.prune(options={})
28 if options.has_key?(:user)
41 if options.has_key?(:user)
@@ -99,6 +99,23 class WatcherTest < ActiveSupport::TestCase
99 assert_nil issue.addable_watcher_users.detect {|user| !issue.visible?(user)}
99 assert_nil issue.addable_watcher_users.detect {|user| !issue.visible?(user)}
100 end
100 end
101
101
102 def test_any_watched_should_return_false_if_no_object_is_watched
103 objects = (0..2).map {Issue.generate!}
104
105 assert_equal false, Watcher.any_watched?(objects, @user)
106 end
107
108 def test_any_watched_should_return_true_if_one_object_is_watched
109 objects = (0..2).map {Issue.generate!}
110 objects.last.add_watcher(@user)
111
112 assert_equal true, Watcher.any_watched?(objects, @user)
113 end
114
115 def test_any_watched_should_return_false_with_no_object
116 assert_equal false, Watcher.any_watched?([], @user)
117 end
118
102 def test_recipients
119 def test_recipients
103 @issue.watchers.delete_all
120 @issue.watchers.delete_all
104 @issue.reload
121 @issue.reload
General Comments 0
You need to be logged in to leave comments. Login now