From 505dcc7487e82141d56e219ecdb62513f55fde55 Mon Sep 17 00:00:00 2001 From: Marius BALTEANU Date: Tue, 21 Apr 2020 00:56:21 +0300 Subject: [PATCH 4/4] Use scope assignable_watchers --- app/controllers/watchers_controller.rb | 10 +++++----- app/helpers/issues_helper.rb | 6 +++--- app/models/principal.rb | 3 +++ lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index 630566dcd..d68614876 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -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 diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 0208e53ad..46f44af2c 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -364,9 +364,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 diff --git a/app/models/principal.rb b/app/models/principal.rb index 0c4f239dd..47fa62ba5 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -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 diff --git a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb index bb04eb7f0..6b16e18a5 100644 --- a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb +++ b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb @@ -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 -- 2.22.0