Commit 8c86f90416308cd0af022a4e5f7d82ce5108f1f9
1 parent
f8cf2a2a
Exists in
master
and in
22 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
@@ -21,6 +21,12 @@ class Person < Profile | @@ -21,6 +21,12 @@ class Person < Profile | ||
21 | { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => [conditions] } | 21 | { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => [conditions] } |
22 | } | 22 | } |
23 | 23 | ||
24 | + scope :not_members_of, lambda { |resources| | ||
25 | + resources = [resources] if !resources.kind_of?(Array) | ||
26 | + conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ') | ||
27 | + { :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] } | ||
28 | + } | ||
29 | + | ||
24 | def has_permission_with_plugins?(permission, profile) | 30 | def has_permission_with_plugins?(permission, profile) |
25 | permissions = [has_permission_without_plugins?(permission, profile)] | 31 | permissions = [has_permission_without_plugins?(permission, profile)] |
26 | permissions += plugins.map do |plugin| | 32 | permissions += plugins.map do |plugin| |
test/unit/person_test.rb
@@ -1118,6 +1118,15 @@ class PersonTest < ActiveSupport::TestCase | @@ -1118,6 +1118,15 @@ class PersonTest < ActiveSupport::TestCase | ||
1118 | assert_equal [person], Person.members_of(community) | 1118 | assert_equal [person], Person.members_of(community) |
1119 | end | 1119 | end |
1120 | 1120 | ||
1121 | + should 'return unique non-members of a community' do | ||
1122 | + member = fast_create(Person) | ||
1123 | + person = fast_create(Person) | ||
1124 | + community = fast_create(Community) | ||
1125 | + community.add_member(member) | ||
1126 | + | ||
1127 | + assert_equal (Person.all - Person.members_of(community)).sort, Person.not_members_of(community).sort | ||
1128 | + end | ||
1129 | + | ||
1121 | should 'be able to pass array to members_of' do | 1130 | should 'be able to pass array to members_of' do |
1122 | person1 = fast_create(Person) | 1131 | person1 = fast_create(Person) |
1123 | community = fast_create(Community) | 1132 | community = fast_create(Community) |
@@ -1130,6 +1139,20 @@ class PersonTest < ActiveSupport::TestCase | @@ -1130,6 +1139,20 @@ class PersonTest < ActiveSupport::TestCase | ||
1130 | assert_includes Person.members_of([community, enterprise]), person2 | 1139 | assert_includes Person.members_of([community, enterprise]), person2 |
1131 | end | 1140 | end |
1132 | 1141 | ||
1142 | + should 'be able to pass array to not_members_of' do | ||
1143 | + person1 = fast_create(Person) | ||
1144 | + community = fast_create(Community) | ||
1145 | + community.add_member(person1) | ||
1146 | + person2 = fast_create(Person) | ||
1147 | + enterprise = fast_create(Enterprise) | ||
1148 | + enterprise.add_member(person2) | ||
1149 | + person3 = fast_create(Person) | ||
1150 | + | ||
1151 | + assert_not_includes Person.not_members_of([community, enterprise]), person1 | ||
1152 | + assert_not_includes Person.not_members_of([community, enterprise]), person2 | ||
1153 | + assert_includes Person.not_members_of([community, enterprise]), person3 | ||
1154 | + end | ||
1155 | + | ||
1133 | should 'find more active people' do | 1156 | should 'find more active people' do |
1134 | Person.destroy_all | 1157 | Person.destroy_all |
1135 | p1 = fast_create(Person) | 1158 | p1 = fast_create(Person) |