diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2013-06-11 18:25:05 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2013-06-11 18:25:05 +0000 |
commit | e0c7eb25a42e6b052c90b32014ef7dc96560f47f (patch) | |
tree | ff59fc0416667bed244d00569ebb48390a2dfceb /app/models/watcher.rb | |
parent | 2113b88db34bef9ef4af18e766f2a882505ee789 (diff) | |
download | redmine-e0c7eb25a42e6b052c90b32014ef7dc96560f47f.tar.gz redmine-e0c7eb25a42e6b052c90b32014ef7dc96560f47f.zip |
Adds Watcher.any_watched? to check if at least one object of a collection is watched.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11959 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/watcher.rb')
-rw-r--r-- | app/models/watcher.rb | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/app/models/watcher.rb b/app/models/watcher.rb index 1cea711b7..3a37aa323 100644 --- a/app/models/watcher.rb +++ b/app/models/watcher.rb @@ -23,6 +23,19 @@ class Watcher < ActiveRecord::Base validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id] validate :validate_user + # Returns true if at least one object among objects is watched by user + def self.any_watched?(objects, user) + objects = objects.reject(&:new_record?) + if objects.any? + objects.group_by {|object| object.class.base_class}.each do |base_class, objects| + if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists? + return true + end + end + end + false + end + # Unwatch things that users are no longer allowed to view def self.prune(options={}) if options.has_key?(:user) |