Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: class Watcher < ActiveRecord::Base Chris@1494: belongs_to :watchable, :polymorphic => true Chris@1494: belongs_to :user Chris@1494: Chris@1494: validates_presence_of :user Chris@1494: validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id] Chris@1494: validate :validate_user Chris@1494: Chris@1494: # Returns true if at least one object among objects is watched by user Chris@1494: def self.any_watched?(objects, user) Chris@1494: objects = objects.reject(&:new_record?) Chris@1494: if objects.any? Chris@1494: objects.group_by {|object| object.class.base_class}.each do |base_class, objects| Chris@1494: if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists? Chris@1494: return true Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: false Chris@1494: end Chris@1494: Chris@1494: # Unwatch things that users are no longer allowed to view Chris@1494: def self.prune(options={}) Chris@1494: if options.has_key?(:user) Chris@1494: prune_single_user(options[:user], options) Chris@1494: else Chris@1494: pruned = 0 Chris@1494: User.where("id IN (SELECT DISTINCT user_id FROM #{table_name})").all.each do |user| Chris@1494: pruned += prune_single_user(user, options) Chris@1494: end Chris@1494: pruned Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: protected Chris@1494: Chris@1494: def validate_user Chris@1494: errors.add :user_id, :invalid unless user.nil? || user.active? Chris@1494: end Chris@1494: Chris@1494: private Chris@1494: Chris@1494: def self.prune_single_user(user, options={}) Chris@1494: return unless user.is_a?(User) Chris@1494: pruned = 0 Chris@1494: where(:user_id => user.id).all.each do |watcher| Chris@1494: next if watcher.watchable.nil? Chris@1494: Chris@1494: if options.has_key?(:project) Chris@1494: next unless watcher.watchable.respond_to?(:project) && watcher.watchable.project == options[:project] Chris@1494: end Chris@1494: Chris@1494: if watcher.watchable.respond_to?(:visible?) Chris@1494: unless watcher.watchable.visible?(user) Chris@1494: watcher.destroy Chris@1494: pruned += 1 Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: pruned Chris@1494: end Chris@1494: end