]> source.dussan.org Git - redmine.git/commitdiff
Merged r21661 to 5.0-stable (#37065).
authorMarius Balteanu <marius.balteanu@zitec.com>
Mon, 20 Jun 2022 15:18:57 +0000 (15:18 +0000)
committerMarius Balteanu <marius.balteanu@zitec.com>
Mon, 20 Jun 2022 15:18:57 +0000 (15:18 +0000)
git-svn-id: https://svn.redmine.org/redmine/branches/5.0-stable@21667 e93f8b46-1217-0410-a6f0-8f06a7374b81

lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
test/unit/watcher_test.rb

index bb71a2884bb7765aea8e66aca08669de288fa9a5..907535ec7a117c9ef6d012066f3bf3b653cd5454 100644 (file)
@@ -15,9 +15,13 @@ module Redmine
             has_many :watchers, :as => :watchable, :dependent => :delete_all
             has_many :watcher_users, :through => :watchers, :source => :user, :validate => false
 
-            scope :watched_by, lambda { |user_id|
+            scope :watched_by, lambda { |principal|
+              user_ids = Array(principal.id)
+              user_ids |= principal.group_ids if principal.is_a?(User)
+              user_ids.compact!
+
               joins(:watchers).
-              where("#{Watcher.table_name}.user_id = ?", user_id)
+              where("#{Watcher.table_name}.user_id IN (?)", user_ids)
             }
           end
           send :include, Redmine::Acts::Watchable::InstanceMethods
@@ -66,9 +70,17 @@ module Redmine
           super user_ids
         end
 
-        # Returns true if object is watched by +user+
-        def watched_by?(user)
-          !!(user && self.watcher_user_ids.detect {|uid| uid == user.id })
+        # Returns true if object is watched by +principal+, that is
+        # either by a given group,
+        # or by a given user or any of their groups
+        def watched_by?(principal)
+          return false unless principal
+
+          user_ids = Array(principal.id)
+          user_ids |= principal.group_ids if principal.is_a?(User)
+          user_ids.compact!
+
+          (self.watcher_user_ids & user_ids).any?
         end
 
         def notified_watchers
index 1638f4fcbbe8780b12773c827b9b3444d3979328..a2de4c19201931984bddf31829abb4a16f2945d8 100644 (file)
@@ -20,7 +20,7 @@
 require File.expand_path('../../test_helper', __FILE__)
 
 class WatcherTest < ActiveSupport::TestCase
-  fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :enabled_modules,
+  fixtures :projects, :groups_users, :users, :email_addresses, :members, :member_roles, :roles, :enabled_modules,
            :issues, :issue_statuses, :enumerations, :trackers, :projects_trackers,
            :boards, :messages,
            :wikis, :wiki_pages,
@@ -60,6 +60,19 @@ class WatcherTest < ActiveSupport::TestCase
     assert Issue.watched_by(@user).include?(@issue)
   end
 
+  def test_watched_by_group
+    group = Group.find(10)
+    user = User.find(8)
+    assert @issue.add_watcher(group)
+    @issue.reload
+
+    assert @issue.watched_by?(group)
+    assert Issue.watched_by(group).include?(@issue)
+
+    assert @issue.watched_by?(user)
+    assert Issue.watched_by(user).include?(@issue)
+  end
+
   def test_watcher_users
     watcher_users = Issue.find(2).watcher_users
     assert_kind_of Array, watcher_users.collect{|w| w}