@@ -1,53 +1,53 | |||||
1 | # ActsAsWatchable |
|
1 | # ActsAsWatchable | |
2 | module Redmine |
|
2 | module Redmine | |
3 | module Acts |
|
3 | module Acts | |
4 | module Watchable |
|
4 | module Watchable | |
5 | def self.included(base) |
|
5 | def self.included(base) | |
6 | base.extend ClassMethods |
|
6 | base.extend ClassMethods | |
7 | end |
|
7 | end | |
8 |
|
8 | |||
9 | module ClassMethods |
|
9 | module ClassMethods | |
10 | def acts_as_watchable(options = {}) |
|
10 | def acts_as_watchable(options = {}) | |
11 | return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods) |
|
11 | return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods) | |
12 | send :include, Redmine::Acts::Watchable::InstanceMethods |
|
12 | send :include, Redmine::Acts::Watchable::InstanceMethods | |
13 |
|
13 | |||
14 | class_eval do |
|
14 | class_eval do | |
15 | has_many :watchers, :as => :watchable, :dependent => :delete_all |
|
15 | has_many :watchers, :as => :watchable, :dependent => :delete_all | |
16 | end |
|
16 | end | |
17 | end |
|
17 | end | |
18 | end |
|
18 | end | |
19 |
|
19 | |||
20 | module InstanceMethods |
|
20 | module InstanceMethods | |
21 | def self.included(base) |
|
21 | def self.included(base) | |
22 | base.extend ClassMethods |
|
22 | base.extend ClassMethods | |
23 | end |
|
23 | end | |
24 |
|
24 | |||
25 | def add_watcher(user) |
|
25 | def add_watcher(user) | |
26 | self.watchers << Watcher.new(:user => user) |
|
26 | self.watchers << Watcher.new(:user => user) | |
27 | end |
|
27 | end | |
28 |
|
28 | |||
29 | def remove_watcher(user) |
|
29 | def remove_watcher(user) | |
30 | return nil unless user && user.is_a?(User) |
|
30 | return nil unless user && user.is_a?(User) | |
31 | Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}" |
|
31 | Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}" | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | def watched_by?(user) |
|
34 | def watched_by?(user) | |
35 | !self.watchers.find(:first, |
|
35 | !self.watchers.find(:first, | |
36 | :conditions => ["#{Watcher.table_name}.user_id = ?", user.id]).nil? |
|
36 | :conditions => ["#{Watcher.table_name}.user_id = ?", user.id]).nil? | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def watcher_recipients |
|
39 | def watcher_recipients | |
40 |
self.watchers.collect { |w| w.user.mail |
|
40 | self.watchers.collect { |w| w.user.mail } | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | module ClassMethods |
|
43 | module ClassMethods | |
44 | def watched_by(user) |
|
44 | def watched_by(user) | |
45 | find(:all, |
|
45 | find(:all, | |
46 | :include => :watchers, |
|
46 | :include => :watchers, | |
47 | :conditions => ["#{Watcher.table_name}.user_id = ?", user.id]) |
|
47 | :conditions => ["#{Watcher.table_name}.user_id = ?", user.id]) | |
48 | end |
|
48 | end | |
49 | end |
|
49 | end | |
50 | end |
|
50 | end | |
51 | end |
|
51 | end | |
52 | end |
|
52 | end | |
53 | end No newline at end of file |
|
53 | end |
General Comments 0
You need to be logged in to leave comments.
Login now