##// END OF EJS Templates
Merged r3705 from trunk....
Jean-Philippe Lang -
r3601:b26d0fe0411c
parent child
Show More
@@ -1,105 +1,111
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class WatcherTest < ActiveSupport::TestCase
21 21 fixtures :projects, :users, :members, :member_roles, :roles, :enabled_modules,
22 22 :issues,
23 23 :boards, :messages,
24 24 :wikis, :wiki_pages,
25 25 :watchers
26 26
27 27 def setup
28 28 @user = User.find(1)
29 29 @issue = Issue.find(1)
30 30 end
31 31
32 32 def test_watch
33 33 assert @issue.add_watcher(@user)
34 34 @issue.reload
35 35 assert @issue.watchers.detect { |w| w.user == @user }
36 36 end
37 37
38 38 def test_cant_watch_twice
39 39 assert @issue.add_watcher(@user)
40 40 assert !@issue.add_watcher(@user)
41 41 end
42 42
43 43 def test_watched_by
44 44 assert @issue.add_watcher(@user)
45 45 @issue.reload
46 46 assert @issue.watched_by?(@user)
47 47 assert Issue.watched_by(@user).include?(@issue)
48 48 end
49 49
50 def test_watcher_user_ids
51 issue = Issue.new
52 issue.watcher_user_ids = ['1', '3']
53 assert issue.watched_by?(User.find(1))
54 end
55
50 56 def test_recipients
51 57 @issue.watchers.delete_all
52 58 @issue.reload
53 59
54 60 assert @issue.watcher_recipients.empty?
55 61 assert @issue.add_watcher(@user)
56 62
57 63 @user.mail_notification = true
58 64 @user.save
59 65 @issue.reload
60 66 assert @issue.watcher_recipients.include?(@user.mail)
61 67
62 68 @user.mail_notification = false
63 69 @user.save
64 70 @issue.reload
65 71 assert @issue.watcher_recipients.include?(@user.mail)
66 72 end
67 73
68 74 def test_unwatch
69 75 assert @issue.add_watcher(@user)
70 76 @issue.reload
71 77 assert_equal 1, @issue.remove_watcher(@user)
72 78 end
73 79
74 80 def test_prune
75 81 Watcher.delete_all("user_id = 9")
76 82 user = User.find(9)
77 83
78 84 # public
79 85 Watcher.create!(:watchable => Issue.find(1), :user => user)
80 86 Watcher.create!(:watchable => Issue.find(2), :user => user)
81 87 Watcher.create!(:watchable => Message.find(1), :user => user)
82 88 Watcher.create!(:watchable => Wiki.find(1), :user => user)
83 89 Watcher.create!(:watchable => WikiPage.find(2), :user => user)
84 90
85 91 # private project (id: 2)
86 92 Member.create!(:project => Project.find(2), :principal => user, :role_ids => [1])
87 93 Watcher.create!(:watchable => Issue.find(4), :user => user)
88 94 Watcher.create!(:watchable => Message.find(7), :user => user)
89 95 Watcher.create!(:watchable => Wiki.find(2), :user => user)
90 96 Watcher.create!(:watchable => WikiPage.find(3), :user => user)
91 97
92 98 assert_no_difference 'Watcher.count' do
93 99 Watcher.prune(:user => User.find(9))
94 100 end
95 101
96 102 Member.delete_all
97 103
98 104 assert_difference 'Watcher.count', -4 do
99 105 Watcher.prune(:user => User.find(9))
100 106 end
101 107
102 108 assert Issue.find(1).watched_by?(user)
103 109 assert !Issue.find(4).watched_by?(user)
104 110 end
105 111 end
@@ -1,74 +1,74
1 1 # ActsAsWatchable
2 2 module Redmine
3 3 module Acts
4 4 module Watchable
5 5 def self.included(base)
6 6 base.extend ClassMethods
7 7 end
8 8
9 9 module ClassMethods
10 10 def acts_as_watchable(options = {})
11 11 return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods)
12 12 send :include, Redmine::Acts::Watchable::InstanceMethods
13 13
14 14 class_eval do
15 15 has_many :watchers, :as => :watchable, :dependent => :delete_all
16 16 has_many :watcher_users, :through => :watchers, :source => :user
17 17
18 18 attr_protected :watcher_ids, :watcher_user_ids
19 19 end
20 20 end
21 21 end
22 22
23 23 module InstanceMethods
24 24 def self.included(base)
25 25 base.extend ClassMethods
26 26 end
27 27
28 28 # Returns an array of users that are proposed as watchers
29 29 def addable_watcher_users
30 30 self.project.users.sort - self.watcher_users
31 31 end
32 32
33 33 # Adds user as a watcher
34 34 def add_watcher(user)
35 35 self.watchers << Watcher.new(:user => user)
36 36 end
37 37
38 38 # Removes user from the watchers list
39 39 def remove_watcher(user)
40 40 return nil unless user && user.is_a?(User)
41 41 Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}"
42 42 end
43 43
44 44 # Adds/removes watcher
45 45 def set_watcher(user, watching=true)
46 46 watching ? add_watcher(user) : remove_watcher(user)
47 47 end
48 48
49 # Returns true if object is watched by user
49 # Returns true if object is watched by +user+
50 50 def watched_by?(user)
51 !!(user && self.watchers.detect {|w| w.user_id == user.id })
51 !!(user && self.watcher_user_ids.detect {|uid| uid == user.id })
52 52 end
53 53
54 54 # Returns an array of watchers' email addresses
55 55 def watcher_recipients
56 56 notified = watchers.collect(&:user).select(&:active?)
57 57 if respond_to?(:visible?)
58 58 notified.reject! {|user| !visible?(user)}
59 59 end
60 60 notified.collect(&:mail).compact
61 61 end
62 62
63 63 module ClassMethods
64 64 # Returns the objects that are watched by user
65 65 def watched_by(user)
66 66 find(:all,
67 67 :include => :watchers,
68 68 :conditions => ["#{Watcher.table_name}.user_id = ?", user.id])
69 69 end
70 70 end
71 71 end
72 72 end
73 73 end
74 74 end
General Comments 0
You need to be logged in to leave comments. Login now