2007-04-21 16:08:31 +04:00
|
|
|
# ActsAsWatchable
|
|
|
|
module Redmine
|
|
|
|
module Acts
|
|
|
|
module Watchable
|
2012-02-26 08:28:12 +04:00
|
|
|
def self.included(base)
|
2007-04-21 16:08:31 +04:00
|
|
|
base.extend ClassMethods
|
2012-02-26 08:28:12 +04:00
|
|
|
end
|
2007-04-21 16:08:31 +04:00
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def acts_as_watchable(options = {})
|
2012-02-26 08:28:12 +04:00
|
|
|
return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods)
|
2007-04-21 16:08:31 +04:00
|
|
|
class_eval do
|
|
|
|
has_many :watchers, :as => :watchable, :dependent => :delete_all
|
2011-05-22 14:30:26 +04:00
|
|
|
has_many :watcher_users, :through => :watchers, :source => :user, :validate => false
|
2012-02-26 08:28:12 +04:00
|
|
|
|
2010-03-10 08:10:43 +03:00
|
|
|
named_scope :watched_by, lambda { |user_id|
|
|
|
|
{ :include => :watchers,
|
|
|
|
:conditions => ["#{Watcher.table_name}.user_id = ?", user_id] }
|
|
|
|
}
|
2008-12-22 22:21:02 +03:00
|
|
|
attr_protected :watcher_ids, :watcher_user_ids
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
2012-03-25 23:52:24 +04:00
|
|
|
send :include, Redmine::Acts::Watchable::InstanceMethods
|
|
|
|
alias_method_chain :watcher_user_ids=, :uniq_ids
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module InstanceMethods
|
|
|
|
def self.included(base)
|
|
|
|
base.extend ClassMethods
|
|
|
|
end
|
2012-02-26 08:28:12 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
# Returns an array of users that are proposed as watchers
|
|
|
|
def addable_watcher_users
|
2011-05-22 13:14:36 +04:00
|
|
|
users = self.project.users.sort - self.watcher_users
|
|
|
|
if respond_to?(:visible?)
|
|
|
|
users.reject! {|user| !visible?(user)}
|
|
|
|
end
|
|
|
|
users
|
2008-08-03 13:14:43 +04:00
|
|
|
end
|
2012-02-26 08:28:12 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
# Adds user as a watcher
|
2007-04-21 16:08:31 +04:00
|
|
|
def add_watcher(user)
|
|
|
|
self.watchers << Watcher.new(:user => user)
|
|
|
|
end
|
2012-02-26 08:28:12 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
# Removes user from the watchers list
|
2007-04-21 16:08:31 +04:00
|
|
|
def remove_watcher(user)
|
|
|
|
return nil unless user && user.is_a?(User)
|
|
|
|
Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}"
|
|
|
|
end
|
2012-02-26 08:28:12 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
# Adds/removes watcher
|
|
|
|
def set_watcher(user, watching=true)
|
|
|
|
watching ? add_watcher(user) : remove_watcher(user)
|
|
|
|
end
|
2012-02-26 08:28:12 +04:00
|
|
|
|
2012-03-25 23:52:24 +04:00
|
|
|
# Overrides watcher_user_ids= to make user_ids uniq
|
|
|
|
def watcher_user_ids_with_uniq_ids=(user_ids)
|
|
|
|
if user_ids.is_a?(Array)
|
|
|
|
user_ids = user_ids.uniq
|
|
|
|
end
|
|
|
|
send :watcher_user_ids_without_uniq_ids=, user_ids
|
|
|
|
end
|
|
|
|
|
2010-04-30 15:02:27 +04:00
|
|
|
# Returns true if object is watched by +user+
|
2007-04-21 16:08:31 +04:00
|
|
|
def watched_by?(user)
|
2010-04-30 15:02:27 +04:00
|
|
|
!!(user && self.watcher_user_ids.detect {|uid| uid == user.id })
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
2012-02-26 08:28:12 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
# Returns an array of watchers' email addresses
|
2007-04-21 16:08:31 +04:00
|
|
|
def watcher_recipients
|
2010-06-20 22:41:30 +04:00
|
|
|
notified = watcher_users.active
|
2011-03-04 17:43:53 +03:00
|
|
|
notified.reject! {|user| user.mail_notification == 'none'}
|
2012-02-26 08:28:12 +04:00
|
|
|
|
2009-12-13 16:06:57 +03:00
|
|
|
if respond_to?(:visible?)
|
|
|
|
notified.reject! {|user| !visible?(user)}
|
|
|
|
end
|
|
|
|
notified.collect(&:mail).compact
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
|
|
|
|
2010-03-10 08:10:43 +03:00
|
|
|
module ClassMethods; end
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-08-03 13:14:43 +04:00
|
|
|
end
|