comparison app/models/watcher.rb @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents 433d4f72a19b
children e248c7af89ec
comparison
equal deleted inserted replaced
1296:038ba2d95de8 1464:261b3d9a4903
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
21 21
22 validates_presence_of :user 22 validates_presence_of :user
23 validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id] 23 validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id]
24 validate :validate_user 24 validate :validate_user
25 25
26 # Returns true if at least one object among objects is watched by user
27 def self.any_watched?(objects, user)
28 objects = objects.reject(&:new_record?)
29 if objects.any?
30 objects.group_by {|object| object.class.base_class}.each do |base_class, objects|
31 if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists?
32 return true
33 end
34 end
35 end
36 false
37 end
38
26 # Unwatch things that users are no longer allowed to view 39 # Unwatch things that users are no longer allowed to view
27 def self.prune(options={}) 40 def self.prune(options={})
28 if options.has_key?(:user) 41 if options.has_key?(:user)
29 prune_single_user(options[:user], options) 42 prune_single_user(options[:user], options)
30 else 43 else
31 pruned = 0 44 pruned = 0
32 User.find(:all, :conditions => "id IN (SELECT DISTINCT user_id FROM #{table_name})").each do |user| 45 User.where("id IN (SELECT DISTINCT user_id FROM #{table_name})").all.each do |user|
33 pruned += prune_single_user(user, options) 46 pruned += prune_single_user(user, options)
34 end 47 end
35 pruned 48 pruned
36 end 49 end
37 end 50 end
45 private 58 private
46 59
47 def self.prune_single_user(user, options={}) 60 def self.prune_single_user(user, options={})
48 return unless user.is_a?(User) 61 return unless user.is_a?(User)
49 pruned = 0 62 pruned = 0
50 find(:all, :conditions => {:user_id => user.id}).each do |watcher| 63 where(:user_id => user.id).all.each do |watcher|
51 next if watcher.watchable.nil? 64 next if watcher.watchable.nil?
52 65
53 if options.has_key?(:project) 66 if options.has_key?(:project)
54 next unless watcher.watchable.respond_to?(:project) && watcher.watchable.project == options[:project] 67 next unless watcher.watchable.respond_to?(:project) && watcher.watchable.project == options[:project]
55 end 68 end