annotate .svn/pristine/c0/c018b127c704967128c1e8321562c39a30eb71d1.svn-base @ 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@1517 1 # Redmine - project management software
Chris@1517 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 3 #
Chris@1517 4 # This program is free software; you can redistribute it and/or
Chris@1517 5 # modify it under the terms of the GNU General Public License
Chris@1517 6 # as published by the Free Software Foundation; either version 2
Chris@1517 7 # of the License, or (at your option) any later version.
Chris@1517 8 #
Chris@1517 9 # This program is distributed in the hope that it will be useful,
Chris@1517 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 12 # GNU General Public License for more details.
Chris@1517 13 #
Chris@1517 14 # You should have received a copy of the GNU General Public License
Chris@1517 15 # along with this program; if not, write to the Free Software
Chris@1517 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 17
Chris@1517 18 class Watcher < ActiveRecord::Base
Chris@1517 19 belongs_to :watchable, :polymorphic => true
Chris@1517 20 belongs_to :user
Chris@1517 21
Chris@1517 22 validates_presence_of :user
Chris@1517 23 validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id]
Chris@1517 24 validate :validate_user
Chris@1517 25
Chris@1517 26 # Returns true if at least one object among objects is watched by user
Chris@1517 27 def self.any_watched?(objects, user)
Chris@1517 28 objects = objects.reject(&:new_record?)
Chris@1517 29 if objects.any?
Chris@1517 30 objects.group_by {|object| object.class.base_class}.each do |base_class, objects|
Chris@1517 31 if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists?
Chris@1517 32 return true
Chris@1517 33 end
Chris@1517 34 end
Chris@1517 35 end
Chris@1517 36 false
Chris@1517 37 end
Chris@1517 38
Chris@1517 39 # Unwatch things that users are no longer allowed to view
Chris@1517 40 def self.prune(options={})
Chris@1517 41 if options.has_key?(:user)
Chris@1517 42 prune_single_user(options[:user], options)
Chris@1517 43 else
Chris@1517 44 pruned = 0
Chris@1517 45 User.where("id IN (SELECT DISTINCT user_id FROM #{table_name})").each do |user|
Chris@1517 46 pruned += prune_single_user(user, options)
Chris@1517 47 end
Chris@1517 48 pruned
Chris@1517 49 end
Chris@1517 50 end
Chris@1517 51
Chris@1517 52 protected
Chris@1517 53
Chris@1517 54 def validate_user
Chris@1517 55 errors.add :user_id, :invalid unless user.nil? || user.active?
Chris@1517 56 end
Chris@1517 57
Chris@1517 58 private
Chris@1517 59
Chris@1517 60 def self.prune_single_user(user, options={})
Chris@1517 61 return unless user.is_a?(User)
Chris@1517 62 pruned = 0
Chris@1517 63 where(:user_id => user.id).each do |watcher|
Chris@1517 64 next if watcher.watchable.nil?
Chris@1517 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@1517 70 end
Chris@1517 71 if watcher.watchable.respond_to?(:visible?)
Chris@1517 72 unless watcher.watchable.visible?(user)
Chris@1517 73 watcher.destroy
Chris@1517 74 pruned += 1
Chris@1517 75 end
Chris@1517 76 end
Chris@1517 77 end
Chris@1517 78 pruned
Chris@1517 79 end
Chris@1517 80 end