@@ -1,80 +1,80 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Watcher < ActiveRecord::Base |
|
19 | 19 | belongs_to :watchable, :polymorphic => true |
|
20 | 20 | belongs_to :user |
|
21 | 21 | |
|
22 | 22 | validates_presence_of :user |
|
23 | 23 | validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id] |
|
24 | 24 | validate :validate_user |
|
25 | 25 | |
|
26 | 26 | # Returns true if at least one object among objects is watched by user |
|
27 | 27 | def self.any_watched?(objects, user) |
|
28 | 28 | objects = objects.reject(&:new_record?) |
|
29 | 29 | if objects.any? |
|
30 | 30 | objects.group_by {|object| object.class.base_class}.each do |base_class, objects| |
|
31 | 31 | if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists? |
|
32 | 32 | return true |
|
33 | 33 | end |
|
34 | 34 | end |
|
35 | 35 | end |
|
36 | 36 | false |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | # Unwatch things that users are no longer allowed to view |
|
40 | 40 | def self.prune(options={}) |
|
41 | 41 | if options.has_key?(:user) |
|
42 | 42 | prune_single_user(options[:user], options) |
|
43 | 43 | else |
|
44 | 44 | pruned = 0 |
|
45 | 45 | User.where("id IN (SELECT DISTINCT user_id FROM #{table_name})").all.each do |user| |
|
46 | 46 | pruned += prune_single_user(user, options) |
|
47 | 47 | end |
|
48 | 48 | pruned |
|
49 | 49 | end |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | protected |
|
53 | 53 | |
|
54 | 54 | def validate_user |
|
55 | 55 | errors.add :user_id, :invalid unless user.nil? || user.active? |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | private |
|
59 | 59 | |
|
60 | 60 | def self.prune_single_user(user, options={}) |
|
61 | 61 | return unless user.is_a?(User) |
|
62 | 62 | pruned = 0 |
|
63 |
where(:user_id => user.id). |
|
|
63 | where(:user_id => user.id).each do |watcher| | |
|
64 | 64 | next if watcher.watchable.nil? |
|
65 | 65 | if options.has_key?(:project) |
|
66 | 66 | unless watcher.watchable.respond_to?(:project) && |
|
67 | 67 | watcher.watchable.project == options[:project] |
|
68 | 68 | next |
|
69 | 69 | end |
|
70 | 70 | end |
|
71 | 71 | if watcher.watchable.respond_to?(:visible?) |
|
72 | 72 | unless watcher.watchable.visible?(user) |
|
73 | 73 | watcher.destroy |
|
74 | 74 | pruned += 1 |
|
75 | 75 | end |
|
76 | 76 | end |
|
77 | 77 | end |
|
78 | 78 | pruned |
|
79 | 79 | end |
|
80 | 80 | end |
General Comments 0
You need to be logged in to leave comments.
Login now