Commit 60eea82d7238abc13b045db0561144695fea09b4
1 parent
44a82ec3
Exists in
master
and in
29 other branches
ActionItem154: adding EnterprisesBlock + refactoring to centralize duplicated co…
…de. I think that maybe those finders are not needed at all git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1399 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
6 changed files
with
102 additions
and
27 deletions
Show diff stats
app/models/communities_block.rb
@@ -8,20 +8,20 @@ class CommunitiesBlock < ProfileListBlock | @@ -8,20 +8,20 @@ class CommunitiesBlock < ProfileListBlock | ||
8 | _('Communities') | 8 | _('Communities') |
9 | end | 9 | end |
10 | 10 | ||
11 | + def footer | ||
12 | + profile = self.owner | ||
13 | + lambda do | ||
14 | + link_to _('All communities'), :profile => profile.identifier, :controller => 'profile', :action => 'communities' | ||
15 | + end | ||
16 | + end | ||
17 | + | ||
11 | def profile_finder | 18 | def profile_finder |
12 | @profile_finder ||= CommunitiesBlock::Finder.new(self) | 19 | @profile_finder ||= CommunitiesBlock::Finder.new(self) |
13 | end | 20 | end |
14 | 21 | ||
15 | class Finder < ProfileListBlock::Finder | 22 | class Finder < ProfileListBlock::Finder |
16 | - def find | ||
17 | - ids = block.owner.community_memberships.map(&:id) | ||
18 | - result = [] | ||
19 | - [block.limit, ids.size].min.times do | ||
20 | - i = pick_random(ids.size) | ||
21 | - result << Profile.find(ids[i]) | ||
22 | - ids.delete_at(i) | ||
23 | - end | ||
24 | - result | 23 | + def ids |
24 | + block.owner.community_memberships.map(&:id) | ||
25 | end | 25 | end |
26 | end | 26 | end |
27 | 27 |
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
1 | +class EnterprisesBlock < ProfileListBlock | ||
2 | + | ||
3 | + def title | ||
4 | + _('Enterprises') | ||
5 | + end | ||
6 | + | ||
7 | + def self.description | ||
8 | + _('A block that displays your enterprises') | ||
9 | + end | ||
10 | + | ||
11 | + def footer | ||
12 | + profile = self.owner | ||
13 | + lambda do | ||
14 | + link_to _('All enterprises'), :profile => profile.identifier, :controller => 'profile', :action => 'enterprises' | ||
15 | + end | ||
16 | + end | ||
17 | + | ||
18 | + | ||
19 | + def profile_finder | ||
20 | + @profile_finder ||= EnterprisesBlock::Finder.new(self) | ||
21 | + end | ||
22 | + | ||
23 | + class Finder < ProfileListBlock::Finder | ||
24 | + def ids | ||
25 | + block.owner.enterprise_memberships.map(&:id) | ||
26 | + end | ||
27 | + end | ||
28 | + | ||
29 | +end |
app/models/members_block.rb
@@ -20,23 +20,10 @@ class MembersBlock < ProfileListBlock | @@ -20,23 +20,10 @@ class MembersBlock < ProfileListBlock | ||
20 | end | 20 | end |
21 | 21 | ||
22 | # Finds random members, up to the limit. | 22 | # Finds random members, up to the limit. |
23 | - class Finder | ||
24 | - def initialize(block) | ||
25 | - @block = block | 23 | + class Finder < ProfileListBlock::Finder |
24 | + def ids | ||
25 | + block.owner.members.map(&:id) | ||
26 | end | 26 | end |
27 | - attr_reader :block | ||
28 | - | ||
29 | - def find | ||
30 | - ids = block.owner.members.map(&:id) | ||
31 | - result = [] | ||
32 | - [block.limit, ids.size].min.times do | ||
33 | - i = pick_random(ids.size) | ||
34 | - result << Profile.find(ids[i]) | ||
35 | - ids.delete_at(i) | ||
36 | - end | ||
37 | - result | ||
38 | - end | ||
39 | - | ||
40 | end | 27 | end |
41 | 28 | ||
42 | 29 |
app/models/profile_list_block.rb
@@ -29,11 +29,21 @@ class ProfileListBlock < Block | @@ -29,11 +29,21 @@ class ProfileListBlock < Block | ||
29 | end | 29 | end |
30 | attr_reader :block | 30 | attr_reader :block |
31 | def find | 31 | def find |
32 | - Profile.find(:all, :limit => block.limit, :order => 'created_at desc') | 32 | + id_list = self.ids |
33 | + result = [] | ||
34 | + [block.limit, id_list.size].min.times do | ||
35 | + i = pick_random(id_list.size) | ||
36 | + result << Profile.find(id_list[i]) | ||
37 | + id_list.delete_at(i) | ||
38 | + end | ||
39 | + result | ||
33 | end | 40 | end |
34 | def pick_random(top) | 41 | def pick_random(top) |
35 | rand(top) | 42 | rand(top) |
36 | end | 43 | end |
44 | + def ids | ||
45 | + Profile.connection.select_all('select id from profiles').map { |entry| entry['id'].to_i } | ||
46 | + end | ||
37 | end | 47 | end |
38 | 48 | ||
39 | def profiles | 49 | def profiles |
@@ -50,7 +60,7 @@ class ProfileListBlock < Block | @@ -50,7 +60,7 @@ class ProfileListBlock < Block | ||
50 | title = self.title | 60 | title = self.title |
51 | lambda do | 61 | lambda do |
52 | block_title(title) + | 62 | block_title(title) + |
53 | - profiles.map {|item| content_tag('div', profile_image_link(item)) }.join("\n") | 63 | + profiles.map {|item| content_tag('div', profile_image_link(item), :class => 'profile-list-block-link') }.join("\n") |
54 | end | 64 | end |
55 | end | 65 | end |
56 | 66 |
@@ -0,0 +1,45 @@ | @@ -0,0 +1,45 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | + | ||
3 | +class EnterprisesBlockTest < Test::Unit::TestCase | ||
4 | + | ||
5 | + should 'inherit from ProfileListBlock' do | ||
6 | + assert_kind_of ProfileListBlock, EnterprisesBlock.new | ||
7 | + end | ||
8 | + | ||
9 | + should 'declare its title' do | ||
10 | + assert_not_equal ProfileListBlock.new.title, EnterprisesBlock.new.title | ||
11 | + end | ||
12 | + | ||
13 | + should 'describe itself' do | ||
14 | + assert_not_equal ProfileListBlock.description, EnterprisesBlock.description | ||
15 | + end | ||
16 | + | ||
17 | + should 'use its own finder' do | ||
18 | + assert_not_equal EnterprisesBlock::Finder, ProfileListBlock::Finder | ||
19 | + assert_kind_of EnterprisesBlock::Finder, EnterprisesBlock.new.profile_finder | ||
20 | + end | ||
21 | + | ||
22 | + should 'list owner communities' do | ||
23 | + | ||
24 | + block = EnterprisesBlock.new | ||
25 | + block.limit = 2 | ||
26 | + | ||
27 | + owner = mock | ||
28 | + block.expects(:owner).returns(owner) | ||
29 | + | ||
30 | + member1 = mock; member1.stubs(:id).returns(1) | ||
31 | + member2 = mock; member2.stubs(:id).returns(2) | ||
32 | + member3 = mock; member3.stubs(:id).returns(3) | ||
33 | + | ||
34 | + owner.expects(:enterprise_memberships).returns([member1, member2, member3]) | ||
35 | + | ||
36 | + block.profile_finder.expects(:pick_random).with(3).returns(2) | ||
37 | + block.profile_finder.expects(:pick_random).with(2).returns(0) | ||
38 | + | ||
39 | + Profile.expects(:find).with(3).returns(member3) | ||
40 | + Profile.expects(:find).with(1).returns(member1) | ||
41 | + | ||
42 | + assert_equal [member3, member1], block.profiles | ||
43 | + end | ||
44 | + | ||
45 | +end |
test/unit/profile_list_block_test.rb
@@ -54,5 +54,9 @@ class ProfileListBlockTest < Test::Unit::TestCase | @@ -54,5 +54,9 @@ class ProfileListBlockTest < Test::Unit::TestCase | ||
54 | block.profiles | 54 | block.profiles |
55 | end | 55 | end |
56 | 56 | ||
57 | + should 'provide random numbers' do | ||
58 | + assert_respond_to ProfileListBlock::Finder.new(nil), :pick_random | ||
59 | + end | ||
60 | + | ||
57 | 61 | ||
58 | end | 62 | end |