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,6 +145,14 @@ class Profile < ActiveRecord::Base
145 scope :public, :conditions => { :visible => true, :public_profile => true, :secret => false } 145 scope :public, :conditions => { :visible => true, :public_profile => true, :secret => false }
146 scope :enabled, :conditions => { :enabled => true } 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 # Subclasses must override this method 156 # Subclasses must override this method
149 scope :more_popular 157 scope :more_popular
150 158
lib/api/v1/communities.rb
@@ -17,7 +17,7 @@ module API @@ -17,7 +17,7 @@ module API
17 # GET /communities?reference_id=10&limit=10&oldest 17 # GET /communities?reference_id=10&limit=10&oldest
18 get do 18 get do
19 communities = select_filtered_collection_of(environment, 'communities', params) 19 communities = select_filtered_collection_of(environment, 'communities', params)
20 - communities = communities.visible 20 + communities = communities.visible_for_person(current_person)
21 present communities, :with => Entities::Community 21 present communities, :with => Entities::Community
22 end 22 end
23 23
test/unit/api/communities_test.rb
@@ -7,7 +7,7 @@ class CommunitiesTest < ActiveSupport::TestCase @@ -7,7 +7,7 @@ class CommunitiesTest < ActiveSupport::TestCase
7 end 7 end
8 8
9 should 'list all communities' do 9 should 'list all communities' do
10 - community1 = fast_create(Community) 10 + community1 = fast_create(Community, :public_profile => true)
11 community2 = fast_create(Community) 11 community2 = fast_create(Community)
12 get "/api/v1/communities?#{params.to_query}" 12 get "/api/v1/communities?#{params.to_query}"
13 json = JSON.parse(last_response.body) 13 json = JSON.parse(last_response.body)