annotate app/models/watcher.rb @ 1477:f2ad2199b49a bibplugin_integration

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