To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / plugins / acts_as_watchable / lib / acts_as_watchable.rb @ 442:753f1380d6bc
History | View | Annotate | Download (2.43 KB)
| 1 | 0:513646585e45 | Chris | # ActsAsWatchable
|
|---|---|---|---|
| 2 | module Redmine |
||
| 3 | module Acts |
||
| 4 | module Watchable |
||
| 5 | def self.included(base) |
||
| 6 | base.extend ClassMethods
|
||
| 7 | end
|
||
| 8 | |||
| 9 | module ClassMethods |
||
| 10 | def acts_as_watchable(options = {}) |
||
| 11 | return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods) |
||
| 12 | send :include, Redmine::Acts::Watchable::InstanceMethods |
||
| 13 | |||
| 14 | class_eval do
|
||
| 15 | has_many :watchers, :as => :watchable, :dependent => :delete_all |
||
| 16 | 441:cbce1fd3b1b7 | Chris | has_many :watcher_users, :through => :watchers, :source => :user, :validate => false |
| 17 | 0:513646585e45 | Chris | |
| 18 | named_scope :watched_by, lambda { |user_id|
|
||
| 19 | { :include => :watchers,
|
||
| 20 | :conditions => ["#{Watcher.table_name}.user_id = ?", user_id] } |
||
| 21 | } |
||
| 22 | attr_protected :watcher_ids, :watcher_user_ids |
||
| 23 | end
|
||
| 24 | end
|
||
| 25 | end
|
||
| 26 | |||
| 27 | module InstanceMethods |
||
| 28 | def self.included(base) |
||
| 29 | base.extend ClassMethods
|
||
| 30 | end
|
||
| 31 | |||
| 32 | # Returns an array of users that are proposed as watchers
|
||
| 33 | def addable_watcher_users |
||
| 34 | 441:cbce1fd3b1b7 | Chris | users = self.project.users.sort - self.watcher_users |
| 35 | if respond_to?(:visible?) |
||
| 36 | users.reject! {|user| !visible?(user)}
|
||
| 37 | end
|
||
| 38 | users |
||
| 39 | 0:513646585e45 | Chris | end
|
| 40 | |||
| 41 | # Adds user as a watcher
|
||
| 42 | def add_watcher(user) |
||
| 43 | self.watchers << Watcher.new(:user => user) |
||
| 44 | end
|
||
| 45 | |||
| 46 | # Removes user from the watchers list
|
||
| 47 | def remove_watcher(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}" |
||
| 50 | end
|
||
| 51 | |||
| 52 | # Adds/removes watcher
|
||
| 53 | def set_watcher(user, watching=true) |
||
| 54 | watching ? add_watcher(user) : remove_watcher(user) |
||
| 55 | end
|
||
| 56 | |||
| 57 | # Returns true if object is watched by +user+
|
||
| 58 | def watched_by?(user) |
||
| 59 | !!(user && self.watcher_user_ids.detect {|uid| uid == user.id })
|
||
| 60 | end
|
||
| 61 | |||
| 62 | # Returns an array of watchers' email addresses
|
||
| 63 | def watcher_recipients |
||
| 64 | notified = watcher_users.active |
||
| 65 | 441:cbce1fd3b1b7 | Chris | notified.reject! {|user| user.mail_notification == 'none'}
|
| 66 | |||
| 67 | 0:513646585e45 | Chris | if respond_to?(:visible?) |
| 68 | notified.reject! {|user| !visible?(user)}
|
||
| 69 | end
|
||
| 70 | notified.collect(&:mail).compact
|
||
| 71 | end
|
||
| 72 | |||
| 73 | module ClassMethods; end |
||
| 74 | end
|
||
| 75 | end
|
||
| 76 | end
|
||
| 77 | end |