Commit ecc6a6313cff6a31a510be411c0fcd614dbc2541
1 parent
32ea7f36
Exists in
staging
and in
42 other branches
Allowing members_of to receive an array of organization
Showing
2 changed files
with
17 additions
and
1 deletions
Show diff stats
app/models/person.rb
@@ -5,7 +5,11 @@ class Person < Profile | @@ -5,7 +5,11 @@ class Person < Profile | ||
5 | acts_as_accessor | 5 | acts_as_accessor |
6 | acts_as_having_hotspots | 6 | acts_as_having_hotspots |
7 | 7 | ||
8 | - named_scope :members_of, lambda { |resource| { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => ['role_assignments.resource_type = ? AND role_assignments.resource_id = ?', resource.class.base_class.name, resource.id ] } } | 8 | + named_scope :members_of, lambda { |resources| |
9 | + resources = [resources] if !resources.kind_of?(Array) | ||
10 | + conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ') | ||
11 | + { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => [conditions] } | ||
12 | + } | ||
9 | 13 | ||
10 | def has_permission_with_plugins?(permission, profile) | 14 | def has_permission_with_plugins?(permission, profile) |
11 | permissions = [has_permission_without_plugins?(permission, profile)] | 15 | permissions = [has_permission_without_plugins?(permission, profile)] |
test/unit/person_test.rb
@@ -1151,6 +1151,18 @@ class PersonTest < ActiveSupport::TestCase | @@ -1151,6 +1151,18 @@ class PersonTest < ActiveSupport::TestCase | ||
1151 | assert_equal [person], Person.members_of(community) | 1151 | assert_equal [person], Person.members_of(community) |
1152 | end | 1152 | end |
1153 | 1153 | ||
1154 | + should 'be able to pass array to members_of' do | ||
1155 | + person1 = fast_create(Person) | ||
1156 | + community = fast_create(Community) | ||
1157 | + community.add_member(person1) | ||
1158 | + person2 = fast_create(Person) | ||
1159 | + enterprise = fast_create(Enterprise) | ||
1160 | + enterprise.add_member(person2) | ||
1161 | + | ||
1162 | + assert_includes Person.members_of([community, enterprise]), person1 | ||
1163 | + assert_includes Person.members_of([community, enterprise]), person2 | ||
1164 | + end | ||
1165 | + | ||
1154 | should 'find more active people' do | 1166 | should 'find more active people' do |
1155 | Person.destroy_all | 1167 | Person.destroy_all |
1156 | p1 = fast_create(Person) | 1168 | p1 = fast_create(Person) |