annotate app/models/watcher.rb @ 1459:cf78a7ade302 luisf

Merge from branch "bug_794"
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Mon, 11 Nov 2013 18:25:50 +0000
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 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@1115 24 validate :validate_user
Chris@0 25
Chris@0 26 # Unwatch things that users are no longer allowed to view
Chris@0 27 def self.prune(options={})
Chris@0 28 if options.has_key?(:user)
Chris@0 29 prune_single_user(options[:user], options)
Chris@0 30 else
Chris@0 31 pruned = 0
Chris@0 32 User.find(:all, :conditions => "id IN (SELECT DISTINCT user_id FROM #{table_name})").each do |user|
Chris@0 33 pruned += prune_single_user(user, options)
Chris@0 34 end
Chris@0 35 pruned
Chris@0 36 end
Chris@0 37 end
Chris@909 38
Chris@0 39 protected
Chris@909 40
Chris@1115 41 def validate_user
Chris@0 42 errors.add :user_id, :invalid unless user.nil? || user.active?
Chris@0 43 end
Chris@909 44
Chris@0 45 private
Chris@909 46
Chris@0 47 def self.prune_single_user(user, options={})
Chris@0 48 return unless user.is_a?(User)
Chris@0 49 pruned = 0
Chris@0 50 find(:all, :conditions => {:user_id => user.id}).each do |watcher|
Chris@0 51 next if watcher.watchable.nil?
Chris@909 52
Chris@0 53 if options.has_key?(:project)
Chris@0 54 next unless watcher.watchable.respond_to?(:project) && watcher.watchable.project == options[:project]
Chris@0 55 end
Chris@909 56
Chris@0 57 if watcher.watchable.respond_to?(:visible?)
Chris@0 58 unless watcher.watchable.visible?(user)
Chris@0 59 watcher.destroy
Chris@0 60 pruned += 1
Chris@0 61 end
Chris@0 62 end
Chris@0 63 end
Chris@0 64 pruned
Chris@0 65 end
Chris@0 66 end