From 43cf61610c9326d0c0a0159248ca751296f1cf4f Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 15 Feb 2008 21:16:33 +0000 Subject: [PATCH] ActionItem154: adding a "communities" block --- app/models/communities_block.rb | 28 ++++++++++++++++++++++++++++ test/unit/communities_block_test.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 0 deletions(-) create mode 100644 app/models/communities_block.rb create mode 100644 test/unit/communities_block_test.rb diff --git a/app/models/communities_block.rb b/app/models/communities_block.rb new file mode 100644 index 0000000..420c830 --- /dev/null +++ b/app/models/communities_block.rb @@ -0,0 +1,28 @@ +class CommunitiesBlock < ProfileListBlock + + def self.description + _('A block that displays your communities') + end + + def title + _('Communities') + 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 + end + end + +end diff --git a/test/unit/communities_block_test.rb b/test/unit/communities_block_test.rb new file mode 100644 index 0000000..1dbc053 --- /dev/null +++ b/test/unit/communities_block_test.rb @@ -0,0 +1,45 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class CommunitiesBlockTest < Test::Unit::TestCase + + should 'inherit from ProfileListBlock' do + assert_kind_of ProfileListBlock, CommunitiesBlock.new + end + + should 'declare its title' do + assert_not_equal ProfileListBlock.new.title, CommunitiesBlock.new.title + end + + should 'describe itself' do + assert_not_equal ProfileListBlock.description, CommunitiesBlock.description + end + + should 'use its own finder' do + assert_not_equal CommunitiesBlock::Finder, ProfileListBlock::Finder + assert_kind_of CommunitiesBlock::Finder, CommunitiesBlock.new.profile_finder + end + + should 'list owner communities' do + + block = CommunitiesBlock.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(:community_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 -- libgit2 0.21.2