Commit 54a0ef949fd54689a3d58d59126441c617d0b722
1 parent
c282a3de
Exists in
master
and in
29 other branches
invite-members: uses a scope to filter out non members of a community
Showing
3 changed files
with
30 additions
and
1 deletions
Show diff stats
app/controllers/public/invite_controller.rb
@@ -78,7 +78,7 @@ class InviteController < PublicController | @@ -78,7 +78,7 @@ class InviteController < PublicController | ||
78 | 78 | ||
79 | def search_friend | 79 | def search_friend |
80 | fields = %w[name identifier email] + plugins_options.map {|field| field[:field].to_s } | 80 | fields = %w[name identifier email] + plugins_options.map {|field| field[:field].to_s } |
81 | - render :text => find_by_contents(:people, environment.people, params['q'], {:page => 1}, {:fields => fields, :joins => :user})[:results].select {|person| !profile.members.include?(person) }.map {|person| {:id => person.id, :name => person.name} }.to_json | 81 | + render :text => find_by_contents(:people, environment.people.not_members_of(profile), params['q'], {:page => 1}, {:fields => fields, :joins => :user})[:results].map {|person| {:id => person.id, :name => person.name} }.to_json |
82 | end | 82 | end |
83 | 83 | ||
84 | protected | 84 | protected |
app/models/person.rb
@@ -33,6 +33,12 @@ class Person < Profile | @@ -33,6 +33,12 @@ class Person < Profile | ||
33 | { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => [conditions] } | 33 | { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => [conditions] } |
34 | } | 34 | } |
35 | 35 | ||
36 | + scope :not_members_of, lambda { |resources| | ||
37 | + resources = [resources] if !resources.kind_of?(Array) | ||
38 | + conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ') | ||
39 | + { :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] } | ||
40 | + } | ||
41 | + | ||
36 | def has_permission_with_plugins?(permission, profile) | 42 | def has_permission_with_plugins?(permission, profile) |
37 | permissions = [has_permission_without_plugins?(permission, profile)] | 43 | permissions = [has_permission_without_plugins?(permission, profile)] |
38 | permissions += plugins.map do |plugin| | 44 | permissions += plugins.map do |plugin| |
test/unit/person_test.rb
@@ -1114,6 +1114,15 @@ class PersonTest < ActiveSupport::TestCase | @@ -1114,6 +1114,15 @@ class PersonTest < ActiveSupport::TestCase | ||
1114 | assert_equal [person], Person.members_of(community) | 1114 | assert_equal [person], Person.members_of(community) |
1115 | end | 1115 | end |
1116 | 1116 | ||
1117 | + should 'return unique non-members of a community' do | ||
1118 | + member = fast_create(Person) | ||
1119 | + person = fast_create(Person) | ||
1120 | + community = fast_create(Community) | ||
1121 | + community.add_member(member) | ||
1122 | + | ||
1123 | + assert_equal (Person.all - Person.members_of(community)).sort, Person.not_members_of(community).sort | ||
1124 | + end | ||
1125 | + | ||
1117 | should 'be able to pass array to members_of' do | 1126 | should 'be able to pass array to members_of' do |
1118 | person1 = fast_create(Person) | 1127 | person1 = fast_create(Person) |
1119 | community = fast_create(Community) | 1128 | community = fast_create(Community) |
@@ -1126,6 +1135,20 @@ class PersonTest < ActiveSupport::TestCase | @@ -1126,6 +1135,20 @@ class PersonTest < ActiveSupport::TestCase | ||
1126 | assert_includes Person.members_of([community, enterprise]), person2 | 1135 | assert_includes Person.members_of([community, enterprise]), person2 |
1127 | end | 1136 | end |
1128 | 1137 | ||
1138 | + should 'be able to pass array to not_members_of' do | ||
1139 | + person1 = fast_create(Person) | ||
1140 | + community = fast_create(Community) | ||
1141 | + community.add_member(person1) | ||
1142 | + person2 = fast_create(Person) | ||
1143 | + enterprise = fast_create(Enterprise) | ||
1144 | + enterprise.add_member(person2) | ||
1145 | + person3 = fast_create(Person) | ||
1146 | + | ||
1147 | + assert_not_includes Person.not_members_of([community, enterprise]), person1 | ||
1148 | + assert_not_includes Person.not_members_of([community, enterprise]), person2 | ||
1149 | + assert_includes Person.not_members_of([community, enterprise]), person3 | ||
1150 | + end | ||
1151 | + | ||
1129 | should 'find more active people' do | 1152 | should 'find more active people' do |
1130 | Person.destroy_all | 1153 | Person.destroy_all |
1131 | p1 = fast_create(Person) | 1154 | p1 = fast_create(Person) |