From e3a5e91d48f0d64bf25f2a7bd6ed2d5ce447ac61 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 2 Feb 2015 19:49:20 -0300 Subject: [PATCH] person: add not_friend_of scope --- app/models/person.rb | 5 +++++ test/unit/person_test.rb | 11 +++++++++++ 2 files changed, 16 insertions(+), 0 deletions(-) diff --git a/app/models/person.rb b/app/models/person.rb index b40ff98..9a8310d 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -28,6 +28,11 @@ class Person < Profile { :select => 'DISTINCT profiles.*', :conditions => ['"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "role_assignments" ON "role_assignments"."accessor_id" = "profiles"."id" AND "role_assignments"."accessor_type" = (\'Profile\') WHERE "profiles"."type" IN (\'Person\') AND (%s))' % conditions] } } + scope :not_friends_of, lambda { |resources| + resources = Array(resources) + { :select => 'DISTINCT profiles.*', :conditions => ['"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "friendships" ON "friendships"."person_id" = "profiles"."id" WHERE "friendships"."friend_id" IN (%s))' % resources.map(&:id)] } + } + def has_permission_with_admin?(permission, profile) return true if profile.admins.include?(self) || profile.environment.admins.include?(self) has_permission_without_admin?(permission, profile) diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 67aa450..69640e4 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -1139,6 +1139,17 @@ class PersonTest < ActiveSupport::TestCase assert_equal (Person.all - Person.members_of(community)).sort, Person.not_members_of(community).sort end + should 'return unique non-friends of a person' do + friend = fast_create(Person) + not_friend = fast_create(Person) + person = fast_create(Person) + person.add_friend(friend) + friend.add_friend(person) + + assert_includes Person.not_friends_of(person), not_friend + assert_not_includes Person.not_friends_of(person), friend + end + should 'be able to pass array to members_of' do person1 = fast_create(Person) community = fast_create(Community) -- libgit2 0.21.2