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