diff --git a/app/models/communities_block.rb b/app/models/communities_block.rb index 06779c9..a1954d6 100644 --- a/app/models/communities_block.rb +++ b/app/models/communities_block.rb @@ -36,7 +36,11 @@ class CommunitiesBlock < ProfileListBlock def ids # FIXME when owner is an environment (i.e. listing communities globally # this can become SLOW) - block.owner.communities.select(&:public_profile).map(&:id) + if block.owner.kind_of?(Environment) + Community.find(:all, :conditions => {:environment_id => block.owner.id, :public_profile => true}, :limit => block.limit, :order => 'random()').map(&:id) + else + block.owner.communities.select(&:public_profile).map(&:id) + end end end diff --git a/app/models/enterprises_block.rb b/app/models/enterprises_block.rb index e6fc46e..cec27f9 100644 --- a/app/models/enterprises_block.rb +++ b/app/models/enterprises_block.rb @@ -37,7 +37,11 @@ class EnterprisesBlock < ProfileListBlock def ids # FIXME when owner is an environment (i.e. listing enterprises globally # this can become SLOW) - block.owner.enterprises.map(&:id) + if block.owner.kind_of?(Environment) + Enterprise.find(:all, :conditions => {:environment_id => block.owner.id, :public_profile => true}, :limit => block.limit, :order => 'random()').map(&:id) + else + block.owner.enterprises.select(&:public_profile).map(&:id) + end end end diff --git a/app/models/people_block.rb b/app/models/people_block.rb index 0f57ecb..f2f6e0f 100644 --- a/app/models/people_block.rb +++ b/app/models/people_block.rb @@ -18,7 +18,7 @@ class PeopleBlock < ProfileListBlock class Finder < ProfileListBlock::Finder def ids - Person.find(:all, :select => 'id', :conditions => { :environment_id => block.owner.id}) + Person.find(:all, :select => 'id', :conditions => { :environment_id => block.owner.id, :public_profile => true}, :limit => block.limit, :order => 'random()') end end diff --git a/app/models/profile.rb b/app/models/profile.rb index b8c00b6..6db0a78 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -57,7 +57,7 @@ class Profile < ActiveRecord::Base acts_as_having_settings :field => :data - settings_items :public_profile, :public_content, :type => :boolean, :default => true + settings_items :public_content, :type => :boolean, :default => true acts_as_mappable :default_units => :kms diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index 8fd970d..e01f8e3 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -42,7 +42,7 @@ class ProfileListBlock < Block rand(top) end def ids - Profile.connection.select_all('select id from profiles').map { |entry| entry['id'].to_i } + Profile.find(:all, :limit => block.limit, :order => 'random()', :conditions => {:environment_id => block.owner.id, :public_profile => true}).map(&:id) end end diff --git a/test/unit/communities_block_test.rb b/test/unit/communities_block_test.rb index b8442bb..6ea2f96 100644 --- a/test/unit/communities_block_test.rb +++ b/test/unit/communities_block_test.rb @@ -25,7 +25,7 @@ class CommunitiesBlockTest < Test::Unit::TestCase block.limit = 2 owner = mock - block.expects(:owner).returns(owner) + block.expects(:owner).at_least_once.returns(owner) member1 = mock; member1.stubs(:id).returns(1); member1.stubs(:public_profile).returns(true) member2 = mock; member2.stubs(:id).returns(2); member2.stubs(:public_profile).returns(true) @@ -81,7 +81,7 @@ class CommunitiesBlockTest < Test::Unit::TestCase private_community.add_member(user) block = CommunitiesBlock.new - block.expects(:owner).returns(user) + block.expects(:owner).at_least_once.returns(user) assert_equal [public_community], block.profiles end diff --git a/test/unit/enterprises_block_test.rb b/test/unit/enterprises_block_test.rb index 5366cca..874ca9f 100644 --- a/test/unit/enterprises_block_test.rb +++ b/test/unit/enterprises_block_test.rb @@ -19,17 +19,16 @@ class EnterprisesBlockTest < Test::Unit::TestCase assert_kind_of EnterprisesBlock::Finder, EnterprisesBlock.new.profile_finder end - should 'list owner communities' do - + should 'list owner enterprises' do block = EnterprisesBlock.new block.limit = 2 owner = mock - block.expects(:owner).returns(owner) + block.expects(:owner).at_least_once.returns(owner) - member1 = mock; member1.stubs(:id).returns(1) - member2 = mock; member2.stubs(:id).returns(2) - member3 = mock; member3.stubs(:id).returns(3) + member1 = stub(:id => 1, :public_profile => true ) + member2 = stub(:id => 2, :public_profile => true ) + member3 = stub(:id => 3, :public_profile => true ) owner.expects(:enterprises).returns([member1, member2, member3]) @@ -42,6 +41,33 @@ class EnterprisesBlockTest < Test::Unit::TestCase assert_equal [member3, member1], block.profiles end + should 'not list private enterprises in environment' do + env = Environment.create!(:name => 'test env') + p1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :environment_id => env.id, :public_profile => true) + p2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :environment_id => env.id, :public_profile => false) #private profile + block = EnterprisesBlock.new + env.boxes.first.blocks << block + block.save! + ids = block.profile_finder.ids + assert_includes ids, p1.id + assert_not_includes ids, p2.id + end + + should 'not list private enterprises in profile' do + person = create_user('test_user').person + role = Profile::Roles.member + e1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :public_profile => true) + e1.affiliate(person, role) + e2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :public_profile => false) #private profile + e2.affiliate(person, role) + block = EnterprisesBlock.new + person.boxes.first.blocks << block + block.save! + ids = block.profile_finder.ids + assert_includes ids, e1.id + assert_not_includes ids, e2.id + end + should 'link to all enterprises for profile' do profile = Profile.new profile.expects(:identifier).returns('theprofile') diff --git a/test/unit/people_block_test.rb b/test/unit/people_block_test.rb index 5109ae7..7e02ce5 100644 --- a/test/unit/people_block_test.rb +++ b/test/unit/people_block_test.rb @@ -26,7 +26,7 @@ class PeopleBlockTest < ActiveSupport::TestCase should 'list people' do owner = mock owner.expects(:id).returns(99) - Person.expects(:find).with(:all, :select => 'id', :conditions => { :environment_id => 99}).returns([]) + Person.expects(:find).with(:all, :select => 'id', :conditions => { :environment_id => 99, :public_profile => true}, :limit => 6, :order => 'random()').returns([]) block = PeopleBlock.new block.expects(:owner).returns(owner).at_least_once block.content diff --git a/test/unit/profile_list_block_test.rb b/test/unit/profile_list_block_test.rb index 000a749..6475aec 100644 --- a/test/unit/profile_list_block_test.rb +++ b/test/unit/profile_list_block_test.rb @@ -42,6 +42,19 @@ class ProfileListBlockTest < Test::Unit::TestCase assert_kind_of String, instance_eval(&block.content) end + should 'not list private profiles' do + env = Environment.create!(:name => 'test env') + p1 = Profile.create!(:name => 'test1', :identifier => 'test1', :environment => env) + p2 = Profile.create!(:name => 'test2', :identifier => 'test2', :environment => env, :public_profile => false) # private profile + block = ProfileListBlock.new + env.boxes.first.blocks << block + block.save! + + ids = block.profile_finder.ids + assert_includes ids, p1.id + assert_not_includes ids, p2.id + end + should 'use finders to find profiles to be listed' do block = ProfileListBlock.new finder = mock diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index eeaf3d2..c3da50b 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -480,6 +480,15 @@ class ProfileTest < Test::Unit::TestCase assert_equal false, p.public_profile end + should 'be able to find the public profiles but not private ones' do + p1 = Profile.create!(:name => 'test1', :identifier => 'test1', :public_profile => true) + p2 = Profile.create!(:name => 'test2', :identifier => 'test2', :public_profile => false) + + result = Profile.find(:all, :conditions => {:public_profile => true}) + assert_includes result, p1 + assert_not_includes result, p2 + end + should 'have public content by default' do assert_equal true, Profile.new.public_content end -- libgit2 0.21.2