Commit 451c0a9f61029d19738ae9a3f8add9ef57283aed

Authored by MoisesMachado
1 parent 3aa65efa

ActionItem616: promoting public_profile to column

 To be able to make some database optimizations in the blocks of profile
listing and still filter out the private profiles the public_profile
attribute had to be promotted from setting to column

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2394 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/communities_block.rb
... ... @@ -36,7 +36,11 @@ class CommunitiesBlock < ProfileListBlock
36 36 def ids
37 37 # FIXME when owner is an environment (i.e. listing communities globally
38 38 # this can become SLOW)
39   - block.owner.communities.select(&:public_profile).map(&:id)
  39 + if block.owner.kind_of?(Environment)
  40 + Community.find(:all, :conditions => {:environment_id => block.owner.id, :public_profile => true}, :limit => block.limit, :order => 'random()').map(&:id)
  41 + else
  42 + block.owner.communities.select(&:public_profile).map(&:id)
  43 + end
40 44 end
41 45 end
42 46  
... ...
app/models/enterprises_block.rb
... ... @@ -37,7 +37,11 @@ class EnterprisesBlock < ProfileListBlock
37 37 def ids
38 38 # FIXME when owner is an environment (i.e. listing enterprises globally
39 39 # this can become SLOW)
40   - block.owner.enterprises.map(&:id)
  40 + if block.owner.kind_of?(Environment)
  41 + Enterprise.find(:all, :conditions => {:environment_id => block.owner.id, :public_profile => true}, :limit => block.limit, :order => 'random()').map(&:id)
  42 + else
  43 + block.owner.enterprises.select(&:public_profile).map(&:id)
  44 + end
41 45 end
42 46 end
43 47  
... ...
app/models/people_block.rb
... ... @@ -18,7 +18,7 @@ class PeopleBlock < ProfileListBlock
18 18  
19 19 class Finder < ProfileListBlock::Finder
20 20 def ids
21   - Person.find(:all, :select => 'id', :conditions => { :environment_id => block.owner.id})
  21 + Person.find(:all, :select => 'id', :conditions => { :environment_id => block.owner.id, :public_profile => true}, :limit => block.limit, :order => 'random()')
22 22 end
23 23 end
24 24  
... ...
app/models/profile.rb
... ... @@ -57,7 +57,7 @@ class Profile &lt; ActiveRecord::Base
57 57  
58 58 acts_as_having_settings :field => :data
59 59  
60   - settings_items :public_profile, :public_content, :type => :boolean, :default => true
  60 + settings_items :public_content, :type => :boolean, :default => true
61 61  
62 62 acts_as_mappable :default_units => :kms
63 63  
... ...
app/models/profile_list_block.rb
... ... @@ -42,7 +42,7 @@ class ProfileListBlock &lt; Block
42 42 rand(top)
43 43 end
44 44 def ids
45   - Profile.connection.select_all('select id from profiles').map { |entry| entry['id'].to_i }
  45 + Profile.find(:all, :limit => block.limit, :order => 'random()', :conditions => {:environment_id => block.owner.id, :public_profile => true}).map(&:id)
46 46 end
47 47 end
48 48  
... ...
test/unit/communities_block_test.rb
... ... @@ -25,7 +25,7 @@ class CommunitiesBlockTest &lt; Test::Unit::TestCase
25 25 block.limit = 2
26 26  
27 27 owner = mock
28   - block.expects(:owner).returns(owner)
  28 + block.expects(:owner).at_least_once.returns(owner)
29 29  
30 30 member1 = mock; member1.stubs(:id).returns(1); member1.stubs(:public_profile).returns(true)
31 31 member2 = mock; member2.stubs(:id).returns(2); member2.stubs(:public_profile).returns(true)
... ... @@ -81,7 +81,7 @@ class CommunitiesBlockTest &lt; Test::Unit::TestCase
81 81 private_community.add_member(user)
82 82  
83 83 block = CommunitiesBlock.new
84   - block.expects(:owner).returns(user)
  84 + block.expects(:owner).at_least_once.returns(user)
85 85  
86 86 assert_equal [public_community], block.profiles
87 87 end
... ...
test/unit/enterprises_block_test.rb
... ... @@ -19,17 +19,16 @@ class EnterprisesBlockTest &lt; Test::Unit::TestCase
19 19 assert_kind_of EnterprisesBlock::Finder, EnterprisesBlock.new.profile_finder
20 20 end
21 21  
22   - should 'list owner communities' do
23   -
  22 + should 'list owner enterprises' do
24 23 block = EnterprisesBlock.new
25 24 block.limit = 2
26 25  
27 26 owner = mock
28   - block.expects(:owner).returns(owner)
  27 + block.expects(:owner).at_least_once.returns(owner)
29 28  
30   - member1 = mock; member1.stubs(:id).returns(1)
31   - member2 = mock; member2.stubs(:id).returns(2)
32   - member3 = mock; member3.stubs(:id).returns(3)
  29 + member1 = stub(:id => 1, :public_profile => true )
  30 + member2 = stub(:id => 2, :public_profile => true )
  31 + member3 = stub(:id => 3, :public_profile => true )
33 32  
34 33 owner.expects(:enterprises).returns([member1, member2, member3])
35 34  
... ... @@ -42,6 +41,33 @@ class EnterprisesBlockTest &lt; Test::Unit::TestCase
42 41 assert_equal [member3, member1], block.profiles
43 42 end
44 43  
  44 + should 'not list private enterprises in environment' do
  45 + env = Environment.create!(:name => 'test env')
  46 + p1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :environment_id => env.id, :public_profile => true)
  47 + p2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :environment_id => env.id, :public_profile => false) #private profile
  48 + block = EnterprisesBlock.new
  49 + env.boxes.first.blocks << block
  50 + block.save!
  51 + ids = block.profile_finder.ids
  52 + assert_includes ids, p1.id
  53 + assert_not_includes ids, p2.id
  54 + end
  55 +
  56 + should 'not list private enterprises in profile' do
  57 + person = create_user('test_user').person
  58 + role = Profile::Roles.member
  59 + e1 = Enterprise.create!(:name => 'test1', :identifier => 'test1', :public_profile => true)
  60 + e1.affiliate(person, role)
  61 + e2 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :public_profile => false) #private profile
  62 + e2.affiliate(person, role)
  63 + block = EnterprisesBlock.new
  64 + person.boxes.first.blocks << block
  65 + block.save!
  66 + ids = block.profile_finder.ids
  67 + assert_includes ids, e1.id
  68 + assert_not_includes ids, e2.id
  69 + end
  70 +
45 71 should 'link to all enterprises for profile' do
46 72 profile = Profile.new
47 73 profile.expects(:identifier).returns('theprofile')
... ...
test/unit/people_block_test.rb
... ... @@ -26,7 +26,7 @@ class PeopleBlockTest &lt; ActiveSupport::TestCase
26 26 should 'list people' do
27 27 owner = mock
28 28 owner.expects(:id).returns(99)
29   - Person.expects(:find).with(:all, :select => 'id', :conditions => { :environment_id => 99}).returns([])
  29 + Person.expects(:find).with(:all, :select => 'id', :conditions => { :environment_id => 99, :public_profile => true}, :limit => 6, :order => 'random()').returns([])
30 30 block = PeopleBlock.new
31 31 block.expects(:owner).returns(owner).at_least_once
32 32 block.content
... ...
test/unit/profile_list_block_test.rb
... ... @@ -42,6 +42,19 @@ class ProfileListBlockTest &lt; Test::Unit::TestCase
42 42 assert_kind_of String, instance_eval(&block.content)
43 43 end
44 44  
  45 + should 'not list private profiles' do
  46 + env = Environment.create!(:name => 'test env')
  47 + p1 = Profile.create!(:name => 'test1', :identifier => 'test1', :environment => env)
  48 + p2 = Profile.create!(:name => 'test2', :identifier => 'test2', :environment => env, :public_profile => false) # private profile
  49 + block = ProfileListBlock.new
  50 + env.boxes.first.blocks << block
  51 + block.save!
  52 +
  53 + ids = block.profile_finder.ids
  54 + assert_includes ids, p1.id
  55 + assert_not_includes ids, p2.id
  56 + end
  57 +
45 58 should 'use finders to find profiles to be listed' do
46 59 block = ProfileListBlock.new
47 60 finder = mock
... ...
test/unit/profile_test.rb
... ... @@ -480,6 +480,15 @@ class ProfileTest &lt; Test::Unit::TestCase
480 480 assert_equal false, p.public_profile
481 481 end
482 482  
  483 + should 'be able to find the public profiles but not private ones' do
  484 + p1 = Profile.create!(:name => 'test1', :identifier => 'test1', :public_profile => true)
  485 + p2 = Profile.create!(:name => 'test2', :identifier => 'test2', :public_profile => false)
  486 +
  487 + result = Profile.find(:all, :conditions => {:public_profile => true})
  488 + assert_includes result, p1
  489 + assert_not_includes result, p2
  490 + end
  491 +
483 492 should 'have public content by default' do
484 493 assert_equal true, Profile.new.public_content
485 494 end
... ...