@@ -1,92 +1,91 | |||
|
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 | class_eval do |
|
13 | 13 | has_many :watchers, :as => :watchable, :dependent => :delete_all |
|
14 | 14 | has_many :watcher_users, :through => :watchers, :source => :user, :validate => false |
|
15 | 15 | |
|
16 | 16 | scope :watched_by, lambda { |user_id| |
|
17 | 17 | joins(:watchers). |
|
18 | 18 | where("#{Watcher.table_name}.user_id = ?", user_id) |
|
19 | 19 | } |
|
20 | 20 | attr_protected :watcher_ids, :watcher_user_ids |
|
21 | 21 | end |
|
22 | 22 | send :include, Redmine::Acts::Watchable::InstanceMethods |
|
23 | alias_method_chain :watcher_user_ids=, :uniq_ids | |
|
24 | 23 | end |
|
25 | 24 | end |
|
26 | 25 | |
|
27 | 26 | module InstanceMethods |
|
28 | 27 | def self.included(base) |
|
29 | 28 | base.extend ClassMethods |
|
30 | 29 | end |
|
31 | 30 | |
|
32 | 31 | # Returns an array of users that are proposed as watchers |
|
33 | 32 | def addable_watcher_users |
|
34 | 33 | users = self.project.users.sort - self.watcher_users |
|
35 | 34 | if respond_to?(:visible?) |
|
36 | 35 | users.reject! {|user| !visible?(user)} |
|
37 | 36 | end |
|
38 | 37 | users |
|
39 | 38 | end |
|
40 | 39 | |
|
41 | 40 | # Adds user as a watcher |
|
42 | 41 | def add_watcher(user) |
|
43 | 42 | # Rails does not reset the has_many :through association |
|
44 | 43 | watcher_users.reset |
|
45 | 44 | self.watchers << Watcher.new(:user => user) |
|
46 | 45 | end |
|
47 | 46 | |
|
48 | 47 | # Removes user from the watchers list |
|
49 | 48 | def remove_watcher(user) |
|
50 | 49 | return nil unless user && user.is_a?(User) |
|
51 | 50 | # Rails does not reset the has_many :through association |
|
52 | 51 | watcher_users.reset |
|
53 | 52 | watchers.where(:user_id => user.id).delete_all |
|
54 | 53 | end |
|
55 | 54 | |
|
56 | 55 | # Adds/removes watcher |
|
57 | 56 | def set_watcher(user, watching=true) |
|
58 | 57 | watching ? add_watcher(user) : remove_watcher(user) |
|
59 | 58 | end |
|
60 | 59 | |
|
61 | 60 | # Overrides watcher_user_ids= to make user_ids uniq |
|
62 |
def watcher_user_ids |
|
|
61 | def watcher_user_ids=(user_ids) | |
|
63 | 62 | if user_ids.is_a?(Array) |
|
64 | 63 | user_ids = user_ids.uniq |
|
65 | 64 | end |
|
66 | send :watcher_user_ids_without_uniq_ids=, user_ids | |
|
65 | super user_ids | |
|
67 | 66 | end |
|
68 | 67 | |
|
69 | 68 | # Returns true if object is watched by +user+ |
|
70 | 69 | def watched_by?(user) |
|
71 | 70 | !!(user && self.watcher_user_ids.detect {|uid| uid == user.id }) |
|
72 | 71 | end |
|
73 | 72 | |
|
74 | 73 | def notified_watchers |
|
75 | 74 | notified = watcher_users.active.to_a |
|
76 | 75 | notified.reject! {|user| user.mail.blank? || user.mail_notification == 'none'} |
|
77 | 76 | if respond_to?(:visible?) |
|
78 | 77 | notified.reject! {|user| !visible?(user)} |
|
79 | 78 | end |
|
80 | 79 | notified |
|
81 | 80 | end |
|
82 | 81 | |
|
83 | 82 | # Returns an array of watchers' email addresses |
|
84 | 83 | def watcher_recipients |
|
85 | 84 | notified_watchers.collect(&:mail) |
|
86 | 85 | end |
|
87 | 86 | |
|
88 | 87 | module ClassMethods; end |
|
89 | 88 | end |
|
90 | 89 | end |
|
91 | 90 | end |
|
92 | 91 | end |
General Comments 0
You need to be logged in to leave comments.
Login now