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