You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watcher.rb 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  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. #
  9. # 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. #
  14. # 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. class Watcher < ActiveRecord::Base
  18. belongs_to :watchable, :polymorphic => true
  19. belongs_to :user
  20. validates_presence_of :user
  21. validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id]
  22. validate :validate_user
  23. # Returns true if at least one object among objects is watched by user
  24. def self.any_watched?(objects, user)
  25. objects = objects.reject(&:new_record?)
  26. if objects.any?
  27. objects.group_by {|object| object.class.base_class}.each do |base_class, objects|
  28. if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists?
  29. return true
  30. end
  31. end
  32. end
  33. false
  34. end
  35. # Unwatch things that users are no longer allowed to view
  36. def self.prune(options={})
  37. if options.has_key?(:user)
  38. prune_single_user(options[:user], options)
  39. else
  40. pruned = 0
  41. User.where("id IN (SELECT DISTINCT user_id FROM #{table_name})").each do |user|
  42. pruned += prune_single_user(user, options)
  43. end
  44. pruned
  45. end
  46. end
  47. protected
  48. def validate_user
  49. errors.add :user_id, :invalid unless user.nil? || user.active?
  50. end
  51. private
  52. def self.prune_single_user(user, options={})
  53. return unless user.is_a?(User)
  54. pruned = 0
  55. where(:user_id => user.id).each do |watcher|
  56. next if watcher.watchable.nil?
  57. if options.has_key?(:project)
  58. unless watcher.watchable.respond_to?(:project) &&
  59. watcher.watchable.project == options[:project]
  60. next
  61. end
  62. end
  63. if watcher.watchable.respond_to?(:visible?)
  64. unless watcher.watchable.visible?(user)
  65. watcher.destroy
  66. pruned += 1
  67. end
  68. end
  69. end
  70. pruned
  71. end
  72. end