annotate .svn/pristine/00/006e32915b2ea0756e649e1fb61801b416e2d647.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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