Chris@909
|
1 # ActsAsWatchable
|
Chris@909
|
2 module Redmine
|
Chris@909
|
3 module Acts
|
Chris@909
|
4 module Watchable
|
Chris@909
|
5 def self.included(base)
|
Chris@909
|
6 base.extend ClassMethods
|
Chris@909
|
7 end
|
Chris@909
|
8
|
Chris@909
|
9 module ClassMethods
|
Chris@909
|
10 def acts_as_watchable(options = {})
|
Chris@909
|
11 return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods)
|
Chris@909
|
12 send :include, Redmine::Acts::Watchable::InstanceMethods
|
Chris@909
|
13
|
Chris@909
|
14 class_eval do
|
Chris@909
|
15 has_many :watchers, :as => :watchable, :dependent => :delete_all
|
Chris@909
|
16 has_many :watcher_users, :through => :watchers, :source => :user, :validate => false
|
Chris@909
|
17
|
Chris@909
|
18 named_scope :watched_by, lambda { |user_id|
|
Chris@909
|
19 { :include => :watchers,
|
Chris@909
|
20 :conditions => ["#{Watcher.table_name}.user_id = ?", user_id] }
|
Chris@909
|
21 }
|
Chris@909
|
22 attr_protected :watcher_ids, :watcher_user_ids
|
Chris@909
|
23 end
|
Chris@909
|
24 end
|
Chris@909
|
25 end
|
Chris@909
|
26
|
Chris@909
|
27 module InstanceMethods
|
Chris@909
|
28 def self.included(base)
|
Chris@909
|
29 base.extend ClassMethods
|
Chris@909
|
30 end
|
Chris@909
|
31
|
Chris@909
|
32 # Returns an array of users that are proposed as watchers
|
Chris@909
|
33 def addable_watcher_users
|
Chris@909
|
34 users = self.project.users.sort - self.watcher_users
|
Chris@909
|
35 if respond_to?(:visible?)
|
Chris@909
|
36 users.reject! {|user| !visible?(user)}
|
Chris@909
|
37 end
|
Chris@909
|
38 users
|
Chris@909
|
39 end
|
Chris@909
|
40
|
Chris@909
|
41 # Adds user as a watcher
|
Chris@909
|
42 def add_watcher(user)
|
Chris@909
|
43 self.watchers << Watcher.new(:user => user)
|
Chris@909
|
44 end
|
Chris@909
|
45
|
Chris@909
|
46 # Removes user from the watchers list
|
Chris@909
|
47 def remove_watcher(user)
|
Chris@909
|
48 return nil unless user && user.is_a?(User)
|
Chris@909
|
49 Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}"
|
Chris@909
|
50 end
|
Chris@909
|
51
|
Chris@909
|
52 # Adds/removes watcher
|
Chris@909
|
53 def set_watcher(user, watching=true)
|
Chris@909
|
54 watching ? add_watcher(user) : remove_watcher(user)
|
Chris@909
|
55 end
|
Chris@909
|
56
|
Chris@909
|
57 # Returns true if object is watched by +user+
|
Chris@909
|
58 def watched_by?(user)
|
Chris@909
|
59 !!(user && self.watcher_user_ids.detect {|uid| uid == user.id })
|
Chris@909
|
60 end
|
Chris@909
|
61
|
Chris@909
|
62 # Returns an array of watchers' email addresses
|
Chris@909
|
63 def watcher_recipients
|
Chris@909
|
64 notified = watcher_users.active
|
Chris@909
|
65 notified.reject! {|user| user.mail_notification == 'none'}
|
Chris@909
|
66
|
Chris@909
|
67 if respond_to?(:visible?)
|
Chris@909
|
68 notified.reject! {|user| !visible?(user)}
|
Chris@909
|
69 end
|
Chris@909
|
70 notified.collect(&:mail).compact
|
Chris@909
|
71 end
|
Chris@909
|
72
|
Chris@909
|
73 module ClassMethods; end
|
Chris@909
|
74 end
|
Chris@909
|
75 end
|
Chris@909
|
76 end
|
Chris@909
|
77 end
|