annotate vendor/plugins/acts_as_watchable/lib/acts_as_watchable.rb @ 812:b5474c68c433 luisf

Merge from branch "live"
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Wed, 23 Nov 2011 15:29:25 +0000
parents cbce1fd3b1b7
children
rev   line source
Chris@0 1 # ActsAsWatchable
Chris@0 2 module Redmine
Chris@0 3 module Acts
Chris@0 4 module Watchable
Chris@0 5 def self.included(base)
Chris@0 6 base.extend ClassMethods
Chris@0 7 end
Chris@0 8
Chris@0 9 module ClassMethods
Chris@0 10 def acts_as_watchable(options = {})
Chris@0 11 return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods)
Chris@0 12 send :include, Redmine::Acts::Watchable::InstanceMethods
Chris@0 13
Chris@0 14 class_eval do
Chris@0 15 has_many :watchers, :as => :watchable, :dependent => :delete_all
Chris@441 16 has_many :watcher_users, :through => :watchers, :source => :user, :validate => false
Chris@0 17
Chris@0 18 named_scope :watched_by, lambda { |user_id|
Chris@0 19 { :include => :watchers,
Chris@0 20 :conditions => ["#{Watcher.table_name}.user_id = ?", user_id] }
Chris@0 21 }
Chris@0 22 attr_protected :watcher_ids, :watcher_user_ids
Chris@0 23 end
Chris@0 24 end
Chris@0 25 end
Chris@0 26
Chris@0 27 module InstanceMethods
Chris@0 28 def self.included(base)
Chris@0 29 base.extend ClassMethods
Chris@0 30 end
Chris@0 31
Chris@0 32 # Returns an array of users that are proposed as watchers
Chris@0 33 def addable_watcher_users
Chris@441 34 users = self.project.users.sort - self.watcher_users
Chris@441 35 if respond_to?(:visible?)
Chris@441 36 users.reject! {|user| !visible?(user)}
Chris@441 37 end
Chris@441 38 users
Chris@0 39 end
Chris@0 40
Chris@0 41 # Adds user as a watcher
Chris@0 42 def add_watcher(user)
Chris@0 43 self.watchers << Watcher.new(:user => user)
Chris@0 44 end
Chris@0 45
Chris@0 46 # Removes user from the watchers list
Chris@0 47 def remove_watcher(user)
Chris@0 48 return nil unless user && user.is_a?(User)
Chris@0 49 Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}"
Chris@0 50 end
Chris@0 51
Chris@0 52 # Adds/removes watcher
Chris@0 53 def set_watcher(user, watching=true)
Chris@0 54 watching ? add_watcher(user) : remove_watcher(user)
Chris@0 55 end
Chris@0 56
Chris@0 57 # Returns true if object is watched by +user+
Chris@0 58 def watched_by?(user)
Chris@0 59 !!(user && self.watcher_user_ids.detect {|uid| uid == user.id })
Chris@0 60 end
Chris@0 61
Chris@0 62 # Returns an array of watchers' email addresses
Chris@0 63 def watcher_recipients
Chris@0 64 notified = watcher_users.active
Chris@441 65 notified.reject! {|user| user.mail_notification == 'none'}
Chris@441 66
Chris@0 67 if respond_to?(:visible?)
Chris@0 68 notified.reject! {|user| !visible?(user)}
Chris@0 69 end
Chris@0 70 notified.collect(&:mail).compact
Chris@0 71 end
Chris@0 72
Chris@0 73 module ClassMethods; end
Chris@0 74 end
Chris@0 75 end
Chris@0 76 end
Chris@0 77 end