To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / watcher.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (2.02 KB)
| 1 | 909:cbb26bc654de | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 4 | # This program is free software; you can redistribute it and/or
|
||
| 5 | # modify it under the terms of the GNU General Public License
|
||
| 6 | # as published by the Free Software Foundation; either version 2
|
||
| 7 | # of the License, or (at your option) any later version.
|
||
| 8 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful,
|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 12 | # GNU General Public License for more details.
|
||
| 13 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License
|
| 15 | # along with this program; if not, write to the Free Software
|
||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
| 17 | |||
| 18 | class Watcher < ActiveRecord::Base |
||
| 19 | belongs_to :watchable, :polymorphic => true |
||
| 20 | belongs_to :user
|
||
| 21 | 909:cbb26bc654de | Chris | |
| 22 | 0:513646585e45 | Chris | validates_presence_of :user
|
| 23 | validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id] |
||
| 24 | 1115:433d4f72a19b | Chris | validate :validate_user
|
| 25 | 0:513646585e45 | Chris | |
| 26 | # Unwatch things that users are no longer allowed to view
|
||
| 27 | def self.prune(options={}) |
||
| 28 | if options.has_key?(:user) |
||
| 29 | prune_single_user(options[:user], options)
|
||
| 30 | else
|
||
| 31 | pruned = 0
|
||
| 32 | 1295:622f24f53b42 | Chris | User.where("id IN (SELECT DISTINCT user_id FROM #{table_name})").all.each do |user| |
| 33 | 0:513646585e45 | Chris | pruned += prune_single_user(user, options) |
| 34 | end
|
||
| 35 | pruned |
||
| 36 | end
|
||
| 37 | end
|
||
| 38 | 909:cbb26bc654de | Chris | |
| 39 | 0:513646585e45 | Chris | protected |
| 40 | 909:cbb26bc654de | Chris | |
| 41 | 1115:433d4f72a19b | Chris | def validate_user |
| 42 | 0:513646585e45 | Chris | errors.add :user_id, :invalid unless user.nil? || user.active? |
| 43 | end
|
||
| 44 | 909:cbb26bc654de | Chris | |
| 45 | 0:513646585e45 | Chris | private |
| 46 | 909:cbb26bc654de | Chris | |
| 47 | 0:513646585e45 | Chris | def self.prune_single_user(user, options={}) |
| 48 | return unless user.is_a?(User) |
||
| 49 | pruned = 0
|
||
| 50 | 1295:622f24f53b42 | Chris | where(:user_id => user.id).all.each do |watcher| |
| 51 | 0:513646585e45 | Chris | next if watcher.watchable.nil? |
| 52 | 909:cbb26bc654de | Chris | |
| 53 | 0:513646585e45 | Chris | if options.has_key?(:project) |
| 54 | next unless watcher.watchable.respond_to?(:project) && watcher.watchable.project == options[:project] |
||
| 55 | end
|
||
| 56 | 909:cbb26bc654de | Chris | |
| 57 | 0:513646585e45 | Chris | if watcher.watchable.respond_to?(:visible?) |
| 58 | unless watcher.watchable.visible?(user)
|
||
| 59 | watcher.destroy |
||
| 60 | pruned += 1
|
||
| 61 | end
|
||
| 62 | end
|
||
| 63 | end
|
||
| 64 | pruned |
||
| 65 | end
|
||
| 66 | end |