diff --git a/app/models/communities_block.rb b/app/models/communities_block.rb index 420c830..15b1820 100644 --- a/app/models/communities_block.rb +++ b/app/models/communities_block.rb @@ -8,20 +8,20 @@ class CommunitiesBlock < ProfileListBlock _('Communities') end + def footer + profile = self.owner + lambda do + link_to _('All communities'), :profile => profile.identifier, :controller => 'profile', :action => 'communities' + end + end + def profile_finder @profile_finder ||= CommunitiesBlock::Finder.new(self) end class Finder < ProfileListBlock::Finder - def find - ids = block.owner.community_memberships.map(&:id) - result = [] - [block.limit, ids.size].min.times do - i = pick_random(ids.size) - result << Profile.find(ids[i]) - ids.delete_at(i) - end - result + def ids + block.owner.community_memberships.map(&:id) end end diff --git a/app/models/enterprises_block.rb b/app/models/enterprises_block.rb new file mode 100644 index 0000000..d089663 --- /dev/null +++ b/app/models/enterprises_block.rb @@ -0,0 +1,29 @@ +class EnterprisesBlock < ProfileListBlock + + def title + _('Enterprises') + end + + def self.description + _('A block that displays your enterprises') + end + + def footer + profile = self.owner + lambda do + link_to _('All enterprises'), :profile => profile.identifier, :controller => 'profile', :action => 'enterprises' + end + end + + + def profile_finder + @profile_finder ||= EnterprisesBlock::Finder.new(self) + end + + class Finder < ProfileListBlock::Finder + def ids + block.owner.enterprise_memberships.map(&:id) + end + end + +end diff --git a/app/models/members_block.rb b/app/models/members_block.rb index c5ddb13..8478bee 100644 --- a/app/models/members_block.rb +++ b/app/models/members_block.rb @@ -20,23 +20,10 @@ class MembersBlock < ProfileListBlock end # Finds random members, up to the limit. - class Finder - def initialize(block) - @block = block + class Finder < ProfileListBlock::Finder + def ids + block.owner.members.map(&:id) end - attr_reader :block - - def find - ids = block.owner.members.map(&:id) - result = [] - [block.limit, ids.size].min.times do - i = pick_random(ids.size) - result << Profile.find(ids[i]) - ids.delete_at(i) - end - result - end - end diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index 13cc735..a22f761 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -29,11 +29,21 @@ class ProfileListBlock < Block end attr_reader :block def find - Profile.find(:all, :limit => block.limit, :order => 'created_at desc') + id_list = self.ids + result = [] + [block.limit, id_list.size].min.times do + i = pick_random(id_list.size) + result << Profile.find(id_list[i]) + id_list.delete_at(i) + end + result end def pick_random(top) rand(top) end + def ids + Profile.connection.select_all('select id from profiles').map { |entry| entry['id'].to_i } + end end def profiles @@ -50,7 +60,7 @@ class ProfileListBlock < Block title = self.title lambda do block_title(title) + - profiles.map {|item| content_tag('div', profile_image_link(item)) }.join("\n") + profiles.map {|item| content_tag('div', profile_image_link(item), :class => 'profile-list-block-link') }.join("\n") end end diff --git a/test/unit/enterprises_block_test.rb b/test/unit/enterprises_block_test.rb new file mode 100644 index 0000000..4c8daa7 --- /dev/null +++ b/test/unit/enterprises_block_test.rb @@ -0,0 +1,45 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class EnterprisesBlockTest < Test::Unit::TestCase + + should 'inherit from ProfileListBlock' do + assert_kind_of ProfileListBlock, EnterprisesBlock.new + end + + should 'declare its title' do + assert_not_equal ProfileListBlock.new.title, EnterprisesBlock.new.title + end + + should 'describe itself' do + assert_not_equal ProfileListBlock.description, EnterprisesBlock.description + end + + should 'use its own finder' do + assert_not_equal EnterprisesBlock::Finder, ProfileListBlock::Finder + assert_kind_of EnterprisesBlock::Finder, EnterprisesBlock.new.profile_finder + end + + should 'list owner communities' do + + block = EnterprisesBlock.new + block.limit = 2 + + owner = mock + block.expects(:owner).returns(owner) + + member1 = mock; member1.stubs(:id).returns(1) + member2 = mock; member2.stubs(:id).returns(2) + member3 = mock; member3.stubs(:id).returns(3) + + owner.expects(:enterprise_memberships).returns([member1, member2, member3]) + + block.profile_finder.expects(:pick_random).with(3).returns(2) + block.profile_finder.expects(:pick_random).with(2).returns(0) + + Profile.expects(:find).with(3).returns(member3) + Profile.expects(:find).with(1).returns(member1) + + assert_equal [member3, member1], block.profiles + end + +end diff --git a/test/unit/profile_list_block_test.rb b/test/unit/profile_list_block_test.rb index 6855280..26f6ddd 100644 --- a/test/unit/profile_list_block_test.rb +++ b/test/unit/profile_list_block_test.rb @@ -54,5 +54,9 @@ class ProfileListBlockTest < Test::Unit::TestCase block.profiles end + should 'provide random numbers' do + assert_respond_to ProfileListBlock::Finder.new(nil), :pick_random + end + end -- libgit2 0.21.2