annotate .svn/pristine/6c/6cea03c76318e826b03e4d334daa05b149e4780d.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # ActsAsWatchable
Chris@1296 2 module Redmine
Chris@1296 3 module Acts
Chris@1296 4 module Watchable
Chris@1296 5 def self.included(base)
Chris@1296 6 base.extend ClassMethods
Chris@1296 7 end
Chris@1296 8
Chris@1296 9 module ClassMethods
Chris@1296 10 def acts_as_watchable(options = {})
Chris@1296 11 return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods)
Chris@1296 12 class_eval do
Chris@1296 13 has_many :watchers, :as => :watchable, :dependent => :delete_all
Chris@1296 14 has_many :watcher_users, :through => :watchers, :source => :user, :validate => false
Chris@1296 15
Chris@1296 16 scope :watched_by, lambda { |user_id|
Chris@1296 17 { :include => :watchers,
Chris@1296 18 :conditions => ["#{Watcher.table_name}.user_id = ?", user_id] }
Chris@1296 19 }
Chris@1296 20 attr_protected :watcher_ids, :watcher_user_ids
Chris@1296 21 end
Chris@1296 22 send :include, Redmine::Acts::Watchable::InstanceMethods
Chris@1296 23 alias_method_chain :watcher_user_ids=, :uniq_ids
Chris@1296 24 end
Chris@1296 25 end
Chris@1296 26
Chris@1296 27 module InstanceMethods
Chris@1296 28 def self.included(base)
Chris@1296 29 base.extend ClassMethods
Chris@1296 30 end
Chris@1296 31
Chris@1296 32 # Returns an array of users that are proposed as watchers
Chris@1296 33 def addable_watcher_users
Chris@1296 34 users = self.project.users.sort - self.watcher_users
Chris@1296 35 if respond_to?(:visible?)
Chris@1296 36 users.reject! {|user| !visible?(user)}
Chris@1296 37 end
Chris@1296 38 users
Chris@1296 39 end
Chris@1296 40
Chris@1296 41 # Adds user as a watcher
Chris@1296 42 def add_watcher(user)
Chris@1296 43 self.watchers << Watcher.new(:user => user)
Chris@1296 44 end
Chris@1296 45
Chris@1296 46 # Removes user from the watchers list
Chris@1296 47 def remove_watcher(user)
Chris@1296 48 return nil unless user && user.is_a?(User)
Chris@1296 49 Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}"
Chris@1296 50 end
Chris@1296 51
Chris@1296 52 # Adds/removes watcher
Chris@1296 53 def set_watcher(user, watching=true)
Chris@1296 54 watching ? add_watcher(user) : remove_watcher(user)
Chris@1296 55 end
Chris@1296 56
Chris@1296 57 # Overrides watcher_user_ids= to make user_ids uniq
Chris@1296 58 def watcher_user_ids_with_uniq_ids=(user_ids)
Chris@1296 59 if user_ids.is_a?(Array)
Chris@1296 60 user_ids = user_ids.uniq
Chris@1296 61 end
Chris@1296 62 send :watcher_user_ids_without_uniq_ids=, user_ids
Chris@1296 63 end
Chris@1296 64
Chris@1296 65 # Returns true if object is watched by +user+
Chris@1296 66 def watched_by?(user)
Chris@1296 67 !!(user && self.watcher_user_ids.detect {|uid| uid == user.id })
Chris@1296 68 end
Chris@1296 69
Chris@1296 70 def notified_watchers
Chris@1296 71 notified = watcher_users.active
Chris@1296 72 notified.reject! {|user| user.mail.blank? || user.mail_notification == 'none'}
Chris@1296 73 if respond_to?(:visible?)
Chris@1296 74 notified.reject! {|user| !visible?(user)}
Chris@1296 75 end
Chris@1296 76 notified
Chris@1296 77 end
Chris@1296 78
Chris@1296 79 # Returns an array of watchers' email addresses
Chris@1296 80 def watcher_recipients
Chris@1296 81 notified_watchers.collect(&:mail)
Chris@1296 82 end
Chris@1296 83
Chris@1296 84 module ClassMethods; end
Chris@1296 85 end
Chris@1296 86 end
Chris@1296 87 end
Chris@1296 88 end