]> source.dussan.org Git - redmine.git/commitdiff
Use scope assignable_watchers (#4511).
authorGo MAEDA <maeda@farend.jp>
Sat, 25 Apr 2020 08:01:59 +0000 (08:01 +0000)
committerGo MAEDA <maeda@farend.jp>
Sat, 25 Apr 2020 08:01:59 +0000 (08:01 +0000)
Patch by Marius BALTEANU.

git-svn-id: http://svn.redmine.org/redmine/trunk@19726 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/watchers_controller.rb
app/helpers/issues_helper.rb
app/models/principal.rb
lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb

index 630566dcd7670f3f3747c1941fb7a92f715a6203..d68614876d3013c314ff4719b6091986746bbd49 100644 (file)
@@ -43,7 +43,7 @@ class WatchersController < ApplicationController
       user_ids << params[:user_id]
     end
     user_ids = user_ids.flatten.compact.uniq
-    users = Principal.active.visible.where(:id => user_ids).where(:users => {:type => ['User', 'Group']}).to_a
+    users = Principal.assignable_watchers.where(:id => user_ids).to_a
     users.each do |user|
       @watchables.each do |watchable|
         Watcher.create(:watchable => watchable, :user => user)
@@ -59,7 +59,7 @@ class WatchersController < ApplicationController
   def append
     if params[:watcher]
       user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
-      @users = Principal.active.visible.where(:id => user_ids).where(:users => {:type => ['User', 'Group']}).to_a
+      @users = Principal.assignable_watchers.where(:id => user_ids).to_a
     end
     if @users.blank?
       head 200
@@ -122,11 +122,11 @@ class WatchersController < ApplicationController
   def users_for_new_watcher
     scope = nil
     if params[:q].blank? && @project.present?
-      scope = @project.principals.where(:users => {:type => ['User', 'Group']})
+      scope = @project.principals.assignable_watchers
     else
-      scope = Principal.where(:users => {:type => ['User', 'Group']}).limit(100)
+      scope = Principal.assignable_watchers.limit(100)
     end
-    users = scope.active.visible.sorted.like(params[:q]).to_a
+    users = scope.sorted.like(params[:q]).to_a
     if @watchables && @watchables.size == 1
       users -= @watchables.first.watcher_users
     end
index c43646174172cdf178da29455629b0e158e0dc61..6b63f7e11efbc5ac140b98ad04fc32515c058d64 100644 (file)
@@ -365,9 +365,9 @@ module IssuesHelper
   # on the new issue form
   def users_for_new_issue_watchers(issue)
     users = issue.watcher_users.select{|u| u.status == User::STATUS_ACTIVE}
-    project_principals = issue.project.principals.where(:users => {:type => ['User', 'Group']}).limit(21)
-    if project_principals.size <= 20
-      users += project_principals.sort
+    assignable_watchers = issue.project.principals.assignable_watchers.limit(21)
+    if assignable_watchers.size <= 20
+      users += assignable_watchers.sort
     end
     users.uniq
   end
index 0c4f239dd711d96be058a4cda8b3a357c02e169d..47fa62ba5dc25ba43147b4015eb72c9a29dbafb0 100644 (file)
@@ -114,6 +114,9 @@ class Principal < ActiveRecord::Base
   }
   scope :sorted, lambda { order(*Principal.fields_for_order_statement)}
 
+  # Principals that can be added as watchers
+  scope :assignable_watchers, lambda { active.visible.where(:type => ['User', 'Group']) }
+
   before_create :set_default_empty_values
   before_destroy :nullify_projects_default_assigned_to
 
index bb04eb7f082a4aed8c72f1f308c0aeac04bdb625..6b16e18a502fa0752b33d2819fca777b74230c7d 100644 (file)
@@ -31,7 +31,7 @@ module Redmine
 
         # Returns an array of users that are proposed as watchers
         def addable_watcher_users
-          users = self.project.principals.where(:users => {:type => ['User', 'Group']}).sort - self.watcher_users
+          users = self.project.principals.assignable_watchers.sort - self.watcher_users
           if respond_to?(:visible?)
             users.reject! {|user| user.is_a?(User) && !visible?(user)}
           end