Commit e3a5e91d48f0d64bf25f2a7bd6ed2d5ce447ac61

Authored by Rodrigo Souto
1 parent 67d95730

person: add not_friend_of scope

Showing 2 changed files with 16 additions and 0 deletions   Show diff stats
app/models/person.rb
... ... @@ -28,6 +28,11 @@ class Person < Profile
28 28 { :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] }
29 29 }
30 30  
  31 + scope :not_friends_of, lambda { |resources|
  32 + resources = Array(resources)
  33 + { :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)] }
  34 + }
  35 +
31 36 def has_permission_with_admin?(permission, profile)
32 37 return true if profile.admins.include?(self) || profile.environment.admins.include?(self)
33 38 has_permission_without_admin?(permission, profile)
... ...
test/unit/person_test.rb
... ... @@ -1139,6 +1139,17 @@ class PersonTest < ActiveSupport::TestCase
1139 1139 assert_equal (Person.all - Person.members_of(community)).sort, Person.not_members_of(community).sort
1140 1140 end
1141 1141  
  1142 + should 'return unique non-friends of a person' do
  1143 + friend = fast_create(Person)
  1144 + not_friend = fast_create(Person)
  1145 + person = fast_create(Person)
  1146 + person.add_friend(friend)
  1147 + friend.add_friend(person)
  1148 +
  1149 + assert_includes Person.not_friends_of(person), not_friend
  1150 + assert_not_includes Person.not_friends_of(person), friend
  1151 + end
  1152 +
1142 1153 should 'be able to pass array to members_of' do
1143 1154 person1 = fast_create(Person)
1144 1155 community = fast_create(Community)
... ...