summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/principal.rb4
-rw-r--r--test/unit/principal_test.rb6
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