Commit 33871f095597ec2f0413456f8116e2647e04ff45
1 parent
3bd78a8f
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
adding named scope visible_for_person
Showing
3 changed files
with
10 additions
and
2 deletions
Show diff stats
app/models/profile.rb
| ... | ... | @@ -134,6 +134,14 @@ class Profile < ActiveRecord::Base |
| 134 | 134 | scope :public, :conditions => { :visible => true, :public_profile => true, :secret => false } |
| 135 | 135 | scope :enabled, :conditions => { :enabled => true } |
| 136 | 136 | |
| 137 | + scope :visible_for_person, lambda { |person| | |
| 138 | + joins('LEFT JOIN "role_assignments" ON "role_assignments"."resource_id" = "profiles"."id" AND "role_assignments"."resource_type" = \'Profile\'') | |
| 139 | + .where( | |
| 140 | + ['( ( role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? ) OR | |
| 141 | + (profiles.public_profile = ?) )', Profile.name, person.id, true] | |
| 142 | + ) | |
| 143 | + } | |
| 144 | + | |
| 137 | 145 | # Subclasses must override this method |
| 138 | 146 | scope :more_popular |
| 139 | 147 | ... | ... |
lib/api/v1/communities.rb
| ... | ... | @@ -17,7 +17,7 @@ module API |
| 17 | 17 | # GET /communities?reference_id=10&limit=10&oldest |
| 18 | 18 | get do |
| 19 | 19 | communities = select_filtered_collection_of(environment, 'communities', params) |
| 20 | - communities = communities.visible | |
| 20 | + communities = communities.visible_for_person(current_person) | |
| 21 | 21 | present communities, :with => Entities::Community |
| 22 | 22 | end |
| 23 | 23 | ... | ... |
test/unit/api/communities_test.rb
| ... | ... | @@ -7,7 +7,7 @@ class CommunitiesTest < ActiveSupport::TestCase |
| 7 | 7 | end |
| 8 | 8 | |
| 9 | 9 | should 'list all communities' do |
| 10 | - community1 = fast_create(Community) | |
| 10 | + community1 = fast_create(Community, :public_profile => true) | |
| 11 | 11 | community2 = fast_create(Community) |
| 12 | 12 | get "/api/v1/communities?#{params.to_query}" |
| 13 | 13 | json = JSON.parse(last_response.body) | ... | ... |