@@ -1,92 +1,91 | |||||
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 | scope :watched_by, lambda { |user_id| |
|
16 | scope :watched_by, lambda { |user_id| | |
17 | joins(:watchers). |
|
17 | joins(:watchers). | |
18 | where("#{Watcher.table_name}.user_id = ?", user_id) |
|
18 | where("#{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 |
|
|||
24 | end |
|
23 | end | |
25 | end |
|
24 | end | |
26 |
|
25 | |||
27 | module InstanceMethods |
|
26 | module InstanceMethods | |
28 | def self.included(base) |
|
27 | def self.included(base) | |
29 | base.extend ClassMethods |
|
28 | base.extend ClassMethods | |
30 | end |
|
29 | end | |
31 |
|
30 | |||
32 | # Returns an array of users that are proposed as watchers |
|
31 | # Returns an array of users that are proposed as watchers | |
33 | def addable_watcher_users |
|
32 | def addable_watcher_users | |
34 | users = self.project.users.sort - self.watcher_users |
|
33 | users = self.project.users.sort - self.watcher_users | |
35 | if respond_to?(:visible?) |
|
34 | if respond_to?(:visible?) | |
36 | users.reject! {|user| !visible?(user)} |
|
35 | users.reject! {|user| !visible?(user)} | |
37 | end |
|
36 | end | |
38 | users |
|
37 | users | |
39 | end |
|
38 | end | |
40 |
|
39 | |||
41 | # Adds user as a watcher |
|
40 | # Adds user as a watcher | |
42 | def add_watcher(user) |
|
41 | def add_watcher(user) | |
43 | # Rails does not reset the has_many :through association |
|
42 | # Rails does not reset the has_many :through association | |
44 | watcher_users.reset |
|
43 | watcher_users.reset | |
45 | self.watchers << Watcher.new(:user => user) |
|
44 | self.watchers << Watcher.new(:user => user) | |
46 | end |
|
45 | end | |
47 |
|
46 | |||
48 | # Removes user from the watchers list |
|
47 | # Removes user from the watchers list | |
49 | def remove_watcher(user) |
|
48 | def remove_watcher(user) | |
50 | return nil unless user && user.is_a?(User) |
|
49 | return nil unless user && user.is_a?(User) | |
51 | # Rails does not reset the has_many :through association |
|
50 | # Rails does not reset the has_many :through association | |
52 | watcher_users.reset |
|
51 | watcher_users.reset | |
53 | watchers.where(:user_id => user.id).delete_all |
|
52 | watchers.where(:user_id => user.id).delete_all | |
54 | end |
|
53 | end | |
55 |
|
54 | |||
56 | # Adds/removes watcher |
|
55 | # Adds/removes watcher | |
57 | def set_watcher(user, watching=true) |
|
56 | def set_watcher(user, watching=true) | |
58 | watching ? add_watcher(user) : remove_watcher(user) |
|
57 | watching ? add_watcher(user) : remove_watcher(user) | |
59 | end |
|
58 | end | |
60 |
|
59 | |||
61 | # Overrides watcher_user_ids= to make user_ids uniq |
|
60 | # Overrides watcher_user_ids= to make user_ids uniq | |
62 |
def watcher_user_ids |
|
61 | def watcher_user_ids=(user_ids) | |
63 | if user_ids.is_a?(Array) |
|
62 | if user_ids.is_a?(Array) | |
64 | user_ids = user_ids.uniq |
|
63 | user_ids = user_ids.uniq | |
65 | end |
|
64 | end | |
66 | send :watcher_user_ids_without_uniq_ids=, user_ids |
|
65 | super user_ids | |
67 | end |
|
66 | end | |
68 |
|
67 | |||
69 | # Returns true if object is watched by +user+ |
|
68 | # Returns true if object is watched by +user+ | |
70 | def watched_by?(user) |
|
69 | def watched_by?(user) | |
71 | !!(user && self.watcher_user_ids.detect {|uid| uid == user.id }) |
|
70 | !!(user && self.watcher_user_ids.detect {|uid| uid == user.id }) | |
72 | end |
|
71 | end | |
73 |
|
72 | |||
74 | def notified_watchers |
|
73 | def notified_watchers | |
75 | notified = watcher_users.active.to_a |
|
74 | notified = watcher_users.active.to_a | |
76 | notified.reject! {|user| user.mail.blank? || user.mail_notification == 'none'} |
|
75 | notified.reject! {|user| user.mail.blank? || user.mail_notification == 'none'} | |
77 | if respond_to?(:visible?) |
|
76 | if respond_to?(:visible?) | |
78 | notified.reject! {|user| !visible?(user)} |
|
77 | notified.reject! {|user| !visible?(user)} | |
79 | end |
|
78 | end | |
80 | notified |
|
79 | notified | |
81 | end |
|
80 | end | |
82 |
|
81 | |||
83 | # Returns an array of watchers' email addresses |
|
82 | # Returns an array of watchers' email addresses | |
84 | def watcher_recipients |
|
83 | def watcher_recipients | |
85 | notified_watchers.collect(&:mail) |
|
84 | notified_watchers.collect(&:mail) | |
86 | end |
|
85 | end | |
87 |
|
86 | |||
88 | module ClassMethods; end |
|
87 | module ClassMethods; end | |
89 | end |
|
88 | end | |
90 | end |
|
89 | end | |
91 | end |
|
90 | end | |
92 | end |
|
91 | end |
General Comments 0
You need to be logged in to leave comments.
Login now