@@ -1,71 +1,72 | |||
|
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 | named_scope :watched_by, lambda { |user_id| |
|
19 | 19 | { :include => :watchers, |
|
20 | 20 | :conditions => ["#{Watcher.table_name}.user_id = ?", user_id] } |
|
21 | 21 | } |
|
22 | 22 | attr_protected :watcher_ids, :watcher_user_ids |
|
23 | 23 | end |
|
24 | 24 | end |
|
25 | 25 | end |
|
26 | 26 | |
|
27 | 27 | module InstanceMethods |
|
28 | 28 | def self.included(base) |
|
29 | 29 | base.extend ClassMethods |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | # Returns an array of users that are proposed as watchers |
|
33 | 33 | def addable_watcher_users |
|
34 | 34 | self.project.users.sort - self.watcher_users |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | # Adds user as a watcher |
|
38 | 38 | def add_watcher(user) |
|
39 | 39 | self.watchers << Watcher.new(:user => user) |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | # Removes user from the watchers list |
|
43 | 43 | def remove_watcher(user) |
|
44 | 44 | return nil unless user && user.is_a?(User) |
|
45 | 45 | Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}" |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | # Adds/removes watcher |
|
49 | 49 | def set_watcher(user, watching=true) |
|
50 | 50 | watching ? add_watcher(user) : remove_watcher(user) |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | # Returns true if object is watched by +user+ |
|
54 | 54 | def watched_by?(user) |
|
55 | 55 | !!(user && self.watcher_user_ids.detect {|uid| uid == user.id }) |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | # Returns an array of watchers' email addresses |
|
59 | 59 | def watcher_recipients |
|
60 |
notified = watchers. |
|
|
60 | notified = watcher_users.active | |
|
61 | ||
|
61 | 62 | if respond_to?(:visible?) |
|
62 | 63 | notified.reject! {|user| !visible?(user)} |
|
63 | 64 | end |
|
64 | 65 | notified.collect(&:mail).compact |
|
65 | 66 | end |
|
66 | 67 | |
|
67 | 68 | module ClassMethods; end |
|
68 | 69 | end |
|
69 | 70 | end |
|
70 | 71 | end |
|
71 | 72 | end |
General Comments 0
You need to be logged in to leave comments.
Login now