@@ -1,53 +1,53 | |||
|
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 | end |
|
17 | 17 | end |
|
18 | 18 | end |
|
19 | 19 | |
|
20 | 20 | module InstanceMethods |
|
21 | 21 | def self.included(base) |
|
22 | 22 | base.extend ClassMethods |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | def add_watcher(user) |
|
26 | 26 | self.watchers << Watcher.new(:user => user) |
|
27 | 27 | end |
|
28 | 28 | |
|
29 | 29 | def remove_watcher(user) |
|
30 | 30 | return nil unless user && user.is_a?(User) |
|
31 | 31 | Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}" |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def watched_by?(user) |
|
35 | 35 | !self.watchers.find(:first, |
|
36 | 36 | :conditions => ["#{Watcher.table_name}.user_id = ?", user.id]).nil? |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | def watcher_recipients |
|
40 |
self.watchers.collect { |w| w.user.mail |
|
|
40 | self.watchers.collect { |w| w.user.mail } | |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | module ClassMethods |
|
44 | 44 | def watched_by(user) |
|
45 | 45 | find(:all, |
|
46 | 46 | :include => :watchers, |
|
47 | 47 | :conditions => ["#{Watcher.table_name}.user_id = ?", user.id]) |
|
48 | 48 | end |
|
49 | 49 | end |
|
50 | 50 | end |
|
51 | 51 | end |
|
52 | 52 | end |
|
53 | 53 | end No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now