From b61ee85e4d15c7b2d3f90143731ab9f0e2acf4b7 Mon Sep 17 00:00:00 2001 From: Go MAEDA Date: Thu, 8 Feb 2024 00:57:07 +0000 Subject: [PATCH] Principal.not_member_of scope does not accept ActiveRecord::Relation (#28243). git-svn-id: https://svn.redmine.org/redmine/trunk@22697 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/principal.rb | 4 ++-- test/unit/principal_test.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/principal.rb b/app/models/principal.rb index 1ef191796..fe5ef35d2 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -103,8 +103,8 @@ class Principal < ApplicationRecord end) # Principals that are not members of projects scope :not_member_of, (lambda do |projects| - projects = [projects] unless projects.is_a?(Array) - if projects.empty? + projects = [projects] if projects.is_a?(Project) + if projects.blank? where("1=0") else ids = projects.map(&:id) diff --git a/test/unit/principal_test.rb b/test/unit/principal_test.rb index f36ace7be..8e8a4bdc1 100644 --- a/test/unit/principal_test.rb +++ b/test/unit/principal_test.rb @@ -76,6 +76,12 @@ class PrincipalTest < ActiveSupport::TestCase end end + def test_not_member_of_scope_should_accept_active_record_relation + projects = Project.where(id: [1, 2]) + expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort + assert_equal expected, Principal.not_member_of(projects).sort + end + def test_not_member_of_scope_should_be_empty_for_no_projects assert_equal [], Principal.not_member_of([]).sort end -- 2.39.5