Commit 43cf61610c9326d0c0a0159248ca751296f1cf4f

Authored by AntonioTerceiro
1 parent 3d0bac28

ActionItem154: adding a "communities" block


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1396 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/communities_block.rb 0 → 100644
... ... @@ -0,0 +1,28 @@
  1 +class CommunitiesBlock < ProfileListBlock
  2 +
  3 + def self.description
  4 + _('A block that displays your communities')
  5 + end
  6 +
  7 + def title
  8 + _('Communities')
  9 + end
  10 +
  11 + def profile_finder
  12 + @profile_finder ||= CommunitiesBlock::Finder.new(self)
  13 + end
  14 +
  15 + 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
  25 + end
  26 + end
  27 +
  28 +end
... ...
test/unit/communities_block_test.rb 0 → 100644
... ... @@ -0,0 +1,45 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class CommunitiesBlockTest < Test::Unit::TestCase
  4 +
  5 + should 'inherit from ProfileListBlock' do
  6 + assert_kind_of ProfileListBlock, CommunitiesBlock.new
  7 + end
  8 +
  9 + should 'declare its title' do
  10 + assert_not_equal ProfileListBlock.new.title, CommunitiesBlock.new.title
  11 + end
  12 +
  13 + should 'describe itself' do
  14 + assert_not_equal ProfileListBlock.description, CommunitiesBlock.description
  15 + end
  16 +
  17 + should 'use its own finder' do
  18 + assert_not_equal CommunitiesBlock::Finder, ProfileListBlock::Finder
  19 + assert_kind_of CommunitiesBlock::Finder, CommunitiesBlock.new.profile_finder
  20 + end
  21 +
  22 + should 'list owner communities' do
  23 +
  24 + block = CommunitiesBlock.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(:community_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
... ...