annotate app/models/watcher.rb @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents dffacf8a6908
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1464 26 # Returns true if at least one object among objects is watched by user
Chris@1464 27 def self.any_watched?(objects, user)
Chris@1464 28 objects = objects.reject(&:new_record?)
Chris@1464 29 if objects.any?
Chris@1464 30 objects.group_by {|object| object.class.base_class}.each do |base_class, objects|
Chris@1464 31 if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists?
Chris@1464 32 return true
Chris@1464 33 end
Chris@1464 34 end
Chris@1464 35 end
Chris@1464 36 false
Chris@1464 37 end
Chris@1464 38
Chris@0 39 # Unwatch things that users are no longer allowed to view
Chris@0 40 def self.prune(options={})
Chris@0 41 if options.has_key?(:user)
Chris@0 42 prune_single_user(options[:user], options)
Chris@0 43 else
Chris@0 44 pruned = 0
Chris@1517 45 User.where("id IN (SELECT DISTINCT user_id FROM #{table_name})").each do |user|
Chris@0 46 pruned += prune_single_user(user, options)
Chris@0 47 end
Chris@0 48 pruned
Chris@0 49 end
Chris@0 50 end
Chris@909 51
Chris@0 52 protected
Chris@909 53
Chris@1115 54 def validate_user
Chris@0 55 errors.add :user_id, :invalid unless user.nil? || user.active?
Chris@0 56 end
Chris@909 57
Chris@0 58 private
Chris@909 59
Chris@0 60 def self.prune_single_user(user, options={})
Chris@0 61 return unless user.is_a?(User)
Chris@0 62 pruned = 0
Chris@1517 63 where(:user_id => user.id).each do |watcher|
Chris@0 64 next if watcher.watchable.nil?
Chris@0 65 if options.has_key?(:project)
Chris@1517 66 unless watcher.watchable.respond_to?(:project) &&
Chris@1517 67 watcher.watchable.project == options[:project]
Chris@1517 68 next
Chris@1517 69 end
Chris@0 70 end
Chris@0 71 if watcher.watchable.respond_to?(:visible?)
Chris@0 72 unless watcher.watchable.visible?(user)
Chris@0 73 watcher.destroy
Chris@0 74 pruned += 1
Chris@0 75 end
Chris@0 76 end
Chris@0 77 end
Chris@0 78 pruned
Chris@0 79 end
Chris@0 80 end