Commit 7aad6583d03e4abce51830786c27c76fecd8a6f9

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent 31c4004d

adding named scope visible_for_person

app/models/profile.rb
... ... @@ -145,6 +145,14 @@ class Profile < ActiveRecord::Base
145 145 scope :public, :conditions => { :visible => true, :public_profile => true, :secret => false }
146 146 scope :enabled, :conditions => { :enabled => true }
147 147  
  148 + scope :visible_for_person, lambda { |person|
  149 + joins('LEFT JOIN "role_assignments" ON "role_assignments"."resource_id" = "profiles"."id" AND "role_assignments"."resource_type" = \'Profile\'')
  150 + .where(
  151 + ['( ( role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? ) OR
  152 + (profiles.public_profile = ?) )', Profile.name, person.id, true]
  153 + )
  154 + }
  155 +
148 156 # Subclasses must override this method
149 157 scope :more_popular
150 158  
... ...
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)
... ...