annotate .svn/pristine/6c/6cea03c76318e826b03e4d334daa05b149e4780d.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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