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