]> source.dussan.org Git - redmine.git/commitdiff
Principal.not_member_of scope does not accept ActiveRecord::Relation (#28243).
authorGo MAEDA <maeda@farend.jp>
Thu, 8 Feb 2024 00:57:07 +0000 (00:57 +0000)
committerGo MAEDA <maeda@farend.jp>
Thu, 8 Feb 2024 00:57:07 +0000 (00:57 +0000)
git-svn-id: https://svn.redmine.org/redmine/trunk@22697 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/principal.rb
test/unit/principal_test.rb

index 1ef1917960d6088a9b6699b62e91821843b6f092..fe5ef35d2a56ee3a9d7f2051125e437e1c6755f4 100644 (file)
@@ -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)
index f36ace7bebe132720dac02c9ea708a62981eca3b..8e8a4bdc122e067bc96334bff36eab6d022ae592 100644 (file)
@@ -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