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